1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.
Advertisements


Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Gordon College Prof. Brinton
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Data Structures Using Java1 Chapter 10 Binary Trees.
Discrete Mathematics Chapter 5 Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Binary Search Trees (BST)
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Chapter 11 Binary Trees Dr. Youssef Harrath
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Data Structure and Algorithms
Binary Trees and Binary Search Trees
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Data Structures & Algorithm Design
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Data Structures Using Java
Chapter 21: Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees

2 Objectives Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree Discover how to insert and delete items in a binary search tree

3 What is a tree? Root Leaf

4 What is a tree? Root Leaf

5 What is a tree? Root Leaf

6 What is a tree? Root Leaf

7 Root Node What is a tree?

8 Family Tree Terms Root

: Root

: Root 5, 9, 10, 12, 13: Leaf (no children)

: Root 5, 9, 10, 12, 13: Leaf (no children) 7 : child of 4 (accessed directly)

: Root 5, 9, 10, 12, 13: Leaf (no children) 7 : child of 4 (accessed directly) 4 : parent of 7, parent of 8 (accessing node)

: Root 5, 9, 10, 12, 13: Leaf (no children) 7 : child of 4 (accessed directly) 4 : parent of 7, parent of 8 (accessing node) 7 and 8: siblings (same parent)

: Root 5, 9, 10, 12, 13: Leaf (no children) 7 : child of 4 (accessed directly) 4 : parent of 7, parent of 8 (accessing node) 7 and 8: siblings (same parent) : stem/branch

15 Binary Trees Condition: each node must not have More than 2 children

16 Binary Trees Condition: Each node must not have more than 2 children

17 Binary Trees Definition: A binary tree, T, is either empty or such that: –T has a special node called the root node; –T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively; –LT and RT are binary trees

18 Binary Tree

19 Binary Tree With One Node The root node of the binary tree = A L A = empty R A = empty A

20 Binary Trees With Two Nodes A B Binary tree with two nodes; the right sub-tree of the rood node is empty. A C Binary tree with two nodes; the left sub-tree of the rood node is empty.

21 Various Binary Trees With Three Nodes A B D A B E A C G A C F (i)(ii)(iii)(iv)

22 Binary Trees Following struct defines the node of a binary tree: template struct nodeType { elemType info; nodeType *llink; nodeType *rlink; };

23 Nodes For each node: –Data is stored in info –The pointer to the left child is stored in llink –The pointer to the right child is stored in rlink

24 General Binary Tree

25 Binary Tree Definitions Leaf: node that has no left and right children Parent: node with at least one child node Level of a node: number of branches on the path from root to node Height of a binary tree: number of nodes on the longest path from root to node Width of a binary tree: the maximum number of elements on one level of the tree

26 Binary Trees Height of tree = 5 Level Width of tree = 4

27 Height of a Binary Tree Recursive algorithm to find height of binary tree: if(p is NULL) height(p) = 0 else height(p) = 1 + max(height(p->llink), height(p->rlink)) (height(p) denotes height of binary tree with root p):

28 Height of a Binary Tree Function to implement above algorithm: template int height(nodeType *p) { if(p == NULL) return 0; else return 1 + max(height(p->llink), height(p->rlink)); }

29 Binary Tree Traversal Must start with the root, then –Visit the node first or –Visit the subtrees first Three different traversals –Inorder –Preorder –Postorder

30 Traversals Inorder –Traverse the left subtree –Visit the node –Traverse the right subtree Preorder –Visit the node –Traverse the left subtree –Traverse the right subtree Postorder –Traverse the left subtree –Traverse the right subtree –Visit the node

31 The order of traversal being discussed is as follows: N : visit node L : Traverse left subtree R : Traverse right subtree Traverse BST

32 Traverse BST If NLR (Preorder): ??? If LNR (Inorder): ??? If LRN (Postorder): ???

33 Binary Tree: Inorder Traversal template void inorder(nodeType *p) { if(p != NULL) { inorder(p->llink); cout info<<” “; inorder(p->rlink); }

34 Binary Tree: preorder Traversal template void preorder(nodeType *p) { if(p != NULL) { cout info<<” “; preorder(p->llink); preorder(p->rlink); }

35 Binary Tree: postorder Traversals template void postorder(nodeType *p) { if(p != NULL) { postorder(p->llink); postorder(p->rlink); cout info<<” “; } }1

36 Implementing Binary Trees: class binaryTreeType Functions Public –isEmpty –inorderTraversal –preorderTraversal –postorderTraversal –treeHeight –treeNodeCount –treeLeavesCount –destroyTree Private copyTree Destroy Inorder, preorder, postorder Height Max nodeCount leavesCount

37 Binary Search Trees Data in each node –Larger than the data in its left child –Smaller than the data in its right child A binary search tree,t, is either empty or: –T has a special node called the root node –T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively –Key in root node larger than every key in left subtree and smaller than every key in right subtree –LT and RT are binary search trees

38 Binary Search Trees

39 Operations Performed on Binary Search Trees Determine whether the binary search tree is empty Search the binary search tree for a particular item Insert an item in the binary search tree Delete an item from the binary search tree

40 Search BST Find 33

41 Search BST Find 33

42 Search BST Find = 64?

43 Search BST Find < 64?

44 Search BST Find 33

45 Search BST Find = 10?

46 Search BST Find < 10?

47 Search BST Find = 33?

48 Search BST Find 33

49 Search BST Find 6

50 Search BST Find 6 6 = 64?

51 Search BST Find 6 6 < 64?

52 Search BST Find 6 6 = 10?

53 Search BST Find 6 6 < 10?

54 Search BST Find 6 6 = 7?

55 Search BST Find 6 6 < 7?

56 Search BST Find 6 NULL

57 Insert Node WHAT ARE THE FEATURES OF A SORTED LIST???? FEATURES OF A SORTED LIST IS PRESERVED Given a sorted list: If 14 is inserted, the list become:

58 Insert Node How does the new BST look like ? How do you do it? Given a BST: If 14 to be inserted :

59 Insert Node Are the features of a BST preserved??? Given a BST:

60 Insert Node < 64? Algorithm to insert 14:

61 Insert Node < 10? Algorithm to insert 14:

62 Insert Node < 33? Algorithm to insert 14:

63 Insert Node NULL Algorithm to insert 14:

64 Insert Node Insert here 14 Algorithm to insert 14:

65 Insert Node Algorithm to insert 14:

66 Insert Node Try insert 99:

67 Insert Node < 64? Try insert 99:

68 Insert Node < 88? Try insert 99:

69 Insert Node < 99? Try insert 99:

70 Insert Node This insert algorithm is not very effective Try building a BST using the same algorithm for the following sequence: K N G I C U K U C I N G C G I K N U It is difficult to produce a balanced Tree  one way is using the AVL Tree algorithm

71 4 cases node deletion : The node to be deleted is a leaf Case 1: The node to be deleted has no left and right subtrees The node to be deleted has 1 child Case 2: The node to be deleted has no left subtree Case 3: The node to be deleted has no right subtree The node to be deleted has 2 children Case 4: The node to be deleted has nonempty left and right subtrees Delete Node

72 M E D P BVN TZ A To delete D: Delete Node: First Case Case 1: The node to be deleted has no left and right subtrees parent

73 M E D P BVN TZ A To delete D parent Set the right child of parent as null x Delete Node: First Case

74 M E D P BVN TZ A To delete D parent x Delete Node: First Case Set the right child of parent as null

Delete Node: First Case 75 To delete D Delete x M E D P BVN TZ A parent x Set the right child of parent as null

76 M EP BVN TZ A To delete D parent Delete Node: First Case

77 To delete E Delete Node: Second Case Case 3: The node to be deleted has no right subtree M EP BVN TZ AD

78 Delete Node: Second Case M EP BVN TZ A To delete E D x parent

79 Delete Node: Second Case M EP BVN TZ A To delete E D x parent Set the Lchild Rchild) of x as the Lchild Rchild) of parent

80 Delete Node: Second Case M EP BVN TZ A To delete E D x parent Set the Lchild Rchild) of x as the Lchild Rchild) of parent

81 Delete Node: Second Case M EP BVN TZ A To delete E D x parent Set the Lchild Rchild) of x as the Lchild Rchild) of parent Free x

82 Delete Node: Second Case M P BVN TZ A To delete E D parent Set the Lchild Rchild) of x as the Lchild Rchild) of parent Free x

83 Delete Node: Second Case M P B VN TZ A To delete E D parent Set the Lchild Rchild) of x as the Lchild Rchild) of parent Free x

84 Delete Node: Third Case M EP BVN TZ AD To delete P Case 4: The node to be deleted has nonempty left and right subtrees

85 Delete Node: Third Case To delete P Case 4: The node to be deleted has nonempty left and right subtrees M EP BVN TZ AD parent x

86 Delete Node: Third Case M EP BVN TZ AD To delete P Determine the next node based on Inorder(LNR) parent x *Usually, the Inorder node has only one child or no children at all

87 Delete Node: Third Case M EP BVN TZ AD To delete P Determine the next node based on Inorder(LNR) parent x y parent of y

88 Delete Node: Third Case M EP BVN TZ AD To delete P Copy parent x y parent of y

89 Delete Node: Third Case M EP BVN TZ AD To delete P Copy x->data = y->data parent x y parent of y

90 Delete Node: Third Case M ET BVN TZ AD To delete P parent x y Copy x->data = y->data parent of y

91 Delete Node: Third Case M ET BVN TZ AD To delete P parent x y Copy x->data = y->data x = y parent of y

92 Delete Node: Third Case M ET BVN TZ AD To delete P Delete T parent y x parent of y

93 Delete Node: Third Case M ET BVN TZ AD To delete P Delete T (as in case 1) parent_y->Lchild = NULL parent y parent of y

94 Delete Node: Third Case M ET BVN Z AD To delete P Delete T parent parent of y

95 Operations Performed on Binary Search Trees Find the height of the binary search tree Find the number of nodes in the binary search tree Find the number of leaves in the binary search tree Traverse the binary search tree Copy the binary search tree

96 Summary Binary trees Binary search trees Recursive traversal algorithms