Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees."— Presentation transcript:

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

2 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 3 What is a tree? Root Leaf

4 4 What is a tree? Root Leaf

5 5 What is a tree? Root Leaf

6 6 What is a tree? Root Leaf

7 7 Root Node What is a tree?

8 8 Family Tree Terms Root 1 23 6 4 7 109 8 12 11 13 5

9 9 1 23 6 4 7 109 8 12 11 13 5 1: Root

10 10 1 23 6 4 7 9 8 12 11 13 5 1: Root 5, 9, 10, 12, 13: Leaf (no children)

11 11 1 23 6 4 7 109 8 12 11 13 5 1: Root 5, 9, 10, 12, 13: Leaf (no children) 7 : child of 4 (accessed directly)

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

13 13 1 23 6 4 7 109 8 12 11 13 5 1: 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)

14 14 1 23 6 4 7 109 8 12 11 13 5 1: 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 15 Binary Trees 1 23 6 4 7 109 8 12 11 13 5 Condition: each node must not have More than 2 children

16 16 Binary Trees Condition: Each node must not have more than 2 children 1 2 5 3 6 8 7 10 9 11 4 1 2 3 4 5

17 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 18 Binary Tree 1 2 5 3 6 8 7 10 9 11 4 1 2 3 4 5

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

20 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 21 Various Binary Trees With Three Nodes A B D A B E A C G A C F (i)(ii)(iii)(iv)

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

23 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 24 General Binary Tree

25 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 26 Binary Trees 1 2 5 3 6 8 7 10 9 11 4 Height of tree = 5 Level 1 2 3 4 5 Width of tree = 4

27 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 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 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 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 31 The order of traversal being discussed is as follows: N : visit node L : Traverse left subtree R : Traverse right subtree Traverse BST

32 32 Traverse BST 64 10 33 88 799 If NLR (Preorder): ??? If LNR (Inorder): ??? If LRN (Postorder): ??? 64 10 7 33 88 99 7 10 33 64 88 99 7 33 10 99 88 64

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

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

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

36 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 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 38 Binary Search Trees 64 10 7 33 88 99 64 10 33 88 997

39 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 40 Search BST 64 10 33 88 799 Find 33

41 41 Search BST 64 10 33 88 799 Find 33

42 42 Search BST 64 10 33 88 799 Find 33 33 = 64?

43 43 Search BST 64 10 33 88 799 Find 33 33 < 64?

44 44 Search BST 64 10 33 88 799 Find 33

45 45 Search BST 64 10 33 88 799 Find 33 33 = 10?

46 46 Search BST 64 10 33 88 799 Find 33 33 < 10?

47 47 Search BST 64 10 33 88 799 Find 33 33 = 33?

48 48 Search BST 64 10 33 88 799 Find 33

49 49 Search BST 64 10 33 88 799 Find 6

50 50 Search BST 64 10 33 88 799 Find 6 6 = 64?

51 51 Search BST 64 10 33 88 799 Find 6 6 < 64?

52 52 Search BST 64 10 33 88 799 Find 6 6 = 10?

53 53 Search BST 64 10 33 88 799 Find 6 6 < 10?

54 54 Search BST 64 10 33 88 799 Find 6 6 = 7?

55 55 Search BST 64 10 33 88 799 Find 6 6 < 7?

56 56 Search BST 64 10 33 88 799 Find 6 NULL

57 57 Insert Node 5 8 9 13 21 44 45 46 5 8 9 13 14 21 44 45 46 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 58 Insert Node 64 10 33 88 799 How does the new BST look like ? How do you do it? Given a BST: If 14 to be inserted :

59 59 Insert Node 64 10 33 88 799 14 Are the features of a BST preserved??? Given a BST:

60 60 Insert Node 64 10 33 88 799 14 < 64? Algorithm to insert 14:

61 61 Insert Node 64 10 33 88 799 14 < 10? Algorithm to insert 14:

62 62 Insert Node 64 10 33 88 799 14 < 33? Algorithm to insert 14:

63 63 Insert Node 64 10 33 88 799 NULL Algorithm to insert 14:

64 64 Insert Node 64 10 33 88 799 Insert here 14 Algorithm to insert 14:

65 65 Insert Node 64 10 33 88 799 14 Algorithm to insert 14:

66 66 Insert Node Try insert 99: 64 10 33 88 799 14

67 67 Insert Node 64 10 33 88 799 14 99 < 64? Try insert 99:

68 68 Insert Node 64 10 33 88 799 14 99 < 88? Try insert 99:

69 69 Insert Node 64 10 33 88 799 14 99 < 99? Try insert 99:

70 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 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 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 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 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

75 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 76 M EP BVN TZ A To delete D parent Delete Node: First Case

77 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 78 Delete Node: Second Case M EP BVN TZ A To delete E D x parent

79 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 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 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 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 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 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 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 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 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 88 Delete Node: Third Case M EP BVN TZ AD To delete P Copy parent x y parent of y

89 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 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 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 92 Delete Node: Third Case M ET BVN TZ AD To delete P Delete T parent y x parent of y

93 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 94 Delete Node: Third Case M ET BVN Z AD To delete P Delete T parent parent of y

95 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 96 Summary Binary trees Binary search trees Recursive traversal algorithms


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

Similar presentations


Ads by Google