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