Download presentation
Presentation is loading. Please wait.
1
Chapter 11 Binary Trees Dr. Youssef Harrath yharrath@uob.edu.bh
2
Outline 1. Why Binary Trees? 2. Binary Trees: Introduction 3. Binary Tree Traversal a. Inorder Traversal b. Preorder Traversal c. Postorder Traversal 4. Implementing Binary Trees 5. Binary Search Trees 6. Binary Tree Traversal and Functions as Parameters 2 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
3
1. Why Binary Trees? When organizing data, a highest priority is to insert, delete, and lookup (search) data as fast as possible. Arrays: Random access data structure (by index). Item search can be fast (mainly if the data is sorted: binary search). Item insertion and deletion are time consuming (data shifting). Linked Lists: Access by pointers Faster in insertion and deletion (update some pointers). Sequential search (must begin with the first item) Binary trees are more efficient in item insertion, deletion and lookup. 3 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
4
2. Binary Trees: Introduction Definition: A binary tree, T, is either empty or such that: i. T has a special node called the root node; ii. T has two sets of nodes, L T and R T, called left subtree and right subtree of T, respectively; and iii. L T and R T are binary trees 4 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
5
2. Binary Trees: Introduction A BC DEF HG Node Empty Directed edge 5 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
6
2. Binary Trees: Introduction A BC DEF HG A is the root of T B is the left child of A (C is the right child of A) A is the parent of B and C L A = {B, D, E, G}, R A = {C, F, H}, B is the root of L A L B = {D, G}, R B = {E}, L E = Empty, R E = Empty 6 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
7
2. Binary Trees: Introduction A Binary tree with one node A is the root of T L A = Empty R A = Empty 7 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
8
2. Binary Trees: Introduction A is the root of T L A = Empty R A = {C} The root of R A is C L C = Empty R C = Empty A C A B Binary trees with two nodes A is the root of T L A = {B} R A = Empty The root of L A is B L B = Empty R B = Empty 8 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
9
2. Binary Trees: Introduction A BC A B E A F C A B D A G C Binary trees with three nodes 9 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
10
2. Binary Trees: Introduction A info = A *llink*rlink template struct nodeType { elemType info; nodeType *llink; nodeType *rlink; }; 10 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
11
2. Binary Trees: Introduction A BC DEF HG ABCDEGHF root 11 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
12
2. Binary Trees: Introduction - Definitions Leaf: a node in a tree with no left and right children. Parent: a node U is called the parent of a node V if there is an edge (branch) from U to V. path: a path from a node X to a node Y in a binary tree is a sequence of nodes X 0, X 1, …, X n such that: X = X 0 and X n = Y X i-1 is the parent of X i for all i = 1, 2, …, n. Level: the level of a node in a binary tree is the number of branches on the path from the root to the node. Height: the height of a binary tree is the number of nodes on the longest path from the root to a leaf. 12 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
13
2. Binary Trees: Introduction - height Suppose that p is a pointer to the root node of a binary tree T. Define a recursive function named height with p as parameter to return the height of T. Hint1: base step: if(empty tree) height = 0 Hint2: recursive step: if(! empty tree), height = 1 + max(height(left subtree), height(right subtree)) 13 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
14
2. Binary Trees: Introduction - height template int binaryTreeType ::height(nodeType *p) { if(p == NULL) return 0; else return 1 + max(height(p->llink), height(p->rlink)); } //max is a function that returns the maximum of two numbers 14 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
15
2. Binary Trees: Introduction – Copy Tree Algorithm to copy otherTree into copiedTree - Function prototype: - void copyTree(pointer to the root of copiedTree, pointer to the root of otherTree) - if(otherTree is empty) - copiedTree is empty. - else - Create a new node for copiedTree. - Fill the info of the new node of copiedTree from otherTree. - Call recursively copyTree to create the left subtree. - Call recursively copyTree to create the right subtree. 15 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
16
2. Binary Trees: Introduction – Copy Tree template void binaryTreeType ::copyTree(nodeType * &copiedTreeRoot, nodeType * otherTreeRoot) { if(otherTreeRoot == NULL) copiedTreeRoot = NULL; else { copiedTreeRoot = new nodeType ; copiedTreeRoot->info = otherTreeRoot->info; copyTree(copiedTreeRoot->llink, otherTreeRoot->llink); copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink); } 16 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
17
3. Binary Tree Traversal The item insertion, deletion, and lookup (search) operations require that the binary tree be traversed. The traversal must start from the root (as we have pointer to it) For each node, we have two choices: Visit the node first. Visit the subtrees first. The above two choices lead to three different traversals: Inorder traversal Preorder traversal Postorder traversal 17 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
18
3. Binary Tree Traversal: Inorder In an inorder traversal, the binary tree is traversed as follows: Traverse the left subtree (recursive call). Visit the current root. Traverse the right subtree (recursive call). Inorder Traversal B, C, A, F, E, G, D, I, H D G B CE FI H A 18 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
19
3. Binary Tree Traversal: Inorder template void inorder(nodeType *p) { if(p != NULL) { inorder(p->llink); cout info<<" "; inorder(p->rlink); } } 19 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
20
3. Binary Tree Traversal: Preorder In a preorder traversal, the binary tree is traversed as follows: Visit the current root. Traverse the left subtree (recursive call). Traverse the right subtree (recursive call). Preorder Traversal A,B, C, D, E, F, G, H, I D G B CE FI H A 20 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
21
3. Binary Tree Traversal: Preorder template void preorder(nodeType *p) { if(p != NULL) { cout info<<" "; preorder(p->llink); preorder(p->rlink); } } 21 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
22
3. Binary Tree Traversal: Postorder In a postorder traversal, the binary tree is traversed as follows: Traverse the left subtree (recursive call). Traverse the right subtree (recursive call). Visit the current root. Postorder Traversal C, B, F, G, E, I, H, D, A D G B CE FI H A 22 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
23
3. Binary Tree Traversal: Postorder template void postorder(nodeType *p) { if(p != NULL) { postorder(p->llink); postorder(p->rlink); cout info<<" "; } } 23 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
24
3. Binary Tree Traversal: How to memorize? Root is first Root-Left-Right Root is in the middleRoot is last Left-Root-RightLeft-Right-Root PreorderInorderPostorder 24 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
25
4. Implementing Binary Trees The operations (functions) which will be implemented are: Determine whether the binary tree is empty. Find the height of the binary tree. Find the number of nodes in the binary tree. Find the number of leaves in the binary tree. Traverse the binary tree. Copy the binary tree. 25 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
26
4. Implementing Binary Trees template struct nodeType { elemType info; nodeType *llink; nodeType *rlink; }; template class binaryTreeType { public: const binaryTreeType & operator=(const binaryTreeType &); bool isEmpty(); 26 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
27
4. Implementing Binary Trees void inorderTraversal(); void preorderTraversal(); void postorderTraversal(); int treeHeight(); int treeNodeCount(); int treeLeavesCount(); void destroyTree(); binaryTreeType(const binaryTreeType & otherTree); binaryTreeType(); ~binaryTreeType(); 27 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
28
4. Implementing Binary Trees protected: nodeType *root; private: void copyTree(nodeType * &copiedTreeRoot, nodeType * otherTreeRoot); void destroyTree(nodeType * &p); void inorder(nodeType *p); void preorder(nodeType *p); void postorder(nodeType *p); int height(nodeType *p); int max(int x, int y); int nodeCount(nodeType *p); int leavesCount(nodeType *p); }; 28 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
29
4. Implementing Binary Trees: Remarks The class binaryTreeType contains the = overloading, a copy constructor, and a destructor because of the use of pointers. The data members of the class are divided into public, protected, and private. The public members are to be used outside the class to handle the binary tree as an ADT. The protected member (the pointer to the root of the binary tree) is to be used to derive special binary trees. The private members are used to implement the public functions. The user of the class doesn’t need to access them. All the public functions has no parameter. The parameters are specified in the private functions. 29 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
30
4. Implementing Binary Trees: isEmpty & constructor template bool binaryTreeType ::isEmpty() { return(root == NULL); } template binaryTreeType ::binaryTreeType() { root = NULL; } 30 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
31
4. Implementing Binary Trees: inorderTraversal, preorderTraversal, and postorderTraversal template void binaryTreeType ::inorderTraversal() { inorder(root); } template void binaryTreeType ::preorderTraversal() { preorder(root); } template void binaryTreeType ::postorderTraversal() { postorder(root); } 31 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
32
4. Implementing Binary Trees: treeHeight, treeNodeCount, and treeLeavesCount template int binaryTreeType ::treeHeight() { return height(root); } template int binaryTreeType ::treeNodeCount() { return nodeCount(root); } template int binaryTreeType ::treeLeavesCount() { return leavesCount(root); } 32 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
33
4. Implementing Binary Trees: inorder template void binaryTreeType ::inorder(nodeType *p) { if(p != NULL) { inorder(p->llink); cout info<<" "; inorder(p->rlink); } 33 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
34
4. Implementing Binary Trees: preorder template void binaryTreeType :: preorder(nodeType *p) { if(p != NULL) { cout info<<" "; preorder(p->llink); preorder(p->rlink); } 34 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
35
4. Implementing Binary Trees: postorder template void binaryTreeType :: postorder(nodeType *p) { if(p != NULL) { postorder(p->llink); postorder(p->rlink); cout info<<" "; } 35 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
36
4. Implementing Binary Trees: height template int binaryTreeType ::height(nodeType *p) { if(p == NULL) return 0; else return 1 + max(height(p->llink), height(p->rlink)); } 36 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
37
4. Implementing Binary Trees: max template int binaryTreeType ::max(int x, int y) { if(x >= y) return x; else return y; } 37 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
38
4. Implementing Binary Trees: nodeCount template int binaryTreeType ::nodeCount(nodeType *p) { if(p == NULL) return 0; else return 1 + nodeCount(p->llink) + nodeCount(p->rlink)); } 38 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
39
4. Implementing Binary Trees: leavesCount Algorithm Basic step: If the tree is empty, return 0 If the tree contains only one node, return 1 Recursive step: return the number of leaves of the left subtree + the number of leaves of the right subtree. 39 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
40
4. Implementing Binary Trees: leavesCount template int binaryTreeType ::leavesCount(nodeType *p) { if(p == NULL) return 0; if(p->llink == NULL && p->rlink == NULL) return 1; else return leavesCount(p->llink) + leavesCount(p->rlink); } 40 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
41
4. Implementing Binary Trees: destroy Algorithm destroy(nodeType * &p) if the pointer p to the root of the tree is not null destroy the left subtree (recursively) destroy the right subtree (recursively) delete p p = null 41 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
42
4. Implementing Binary Trees: destroy template void binaryTreeType ::destroy(nodeType * &p) { if(p != NULL) { destroy(p->llink); destroy(p->rlink); delete p; p = NULL; } } 42 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
43
4. Implementing Binary Trees: destroyTree & copy constructor template void binaryTreeType ::destroyTree() { destroy(root); } template binaryTreeType ::binaryTreeType(const binaryTreeType & otherTree) { if(otherTree.root == NULL) root = NULL; else copyTree(root, otherTree.root); } 43 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
44
4. Implementing Binary Trees: destructor 44 template binaryTreeType ::~binaryTreeType() { destroy(root); } Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
45
4. Implementing Binary Trees: operator = 45 template const binaryTreeType & binaryTreeType : : operator=(const binaryTreeType & otherTree) { if(this != &otherTree) { if(root != NULL) destroy(root); if(otherTree.root == NULL) root = NULL; else copyTree(root, otherTree.root); } return *this; } Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
46
5. Binary Search Trees 46 78 3260 894698 5328 Arbitrary binary tree How to search for 50? Apply one of the binary tree traversals. This may require to traverse the hole tree. root Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
47
5. Binary Search Trees 47 60 5070 305880 7746 Binary search tree The data in each node is: Larger than the data in its left child. Smaller than the data in its right child. root Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
48
5. Binary Search Trees 48 Binary search tree 60 5070 305880 7746 root How to search for 58? a.Compare 58 with the value of the root (60). b.58 < 60 ignore the right subtree of the root 60. c.Compare 58 with the value of the root of the left subtree (50). d.58 > 50 ignore the left subtree of the root 50. e.… Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
49
5. Binary Search Trees 49 Definition: A binary search tree T is either empty or: i. T has a special node called the root node; ii. T has two sets of nodes, L T and R T, called left subtree and right subtree of T, respectively; and iii. The key (value) in the root node is larger than every key in the left subtree and smaller than every key in the right subtree; and iv. L T and R T are binary search trees. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
50
5. Binary Search Trees 50 The functions needed to implement a binary search tree are: 1.Determine whether the binary tree is empty. 2.Search the binary search tree for a particular item. 3.Insert an item in the binary search tree. 4.Delete an item from the binary search tree. 5.Find the height of the binary search tree. 6.Find the number of nodes in the binary search tree. 7.Find the number of leaves in the binary search tree. 8.Traverse the binary search tree. 9.Copy the binary search tree. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
51
5. Binary Search Trees 51 Clearly every binary search tree is a binary tree. The following functions are common between a binary tree and a binary search tree: 1.empty 2.height 3.number of nodes 4.number of leaves. 5.Traverse. 6.Copy. For these reasons, a new class to implement the binary search tree will be derived from the binary tree class. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
52
5. Binary Search Trees 52 template class bSearchTreeType: public binaryTreeType { public: bool search(const elemType& searchItem); void insert(const elemType& insertItem); void deleteNode(const elemType& deleteItem); private: void deleteFromTree(nodeType * &p); }; Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
53
5. Binary Search Trees: search algorithm 53 if root is NULL cannot search an empty tree, returns false else { current = root; while(current is not NULL and not found) if(current ->info is the same as the search item) set found to true else if(current ->info is greater than the search item) follow the left link of current else follow the right link of current } Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
54
5. Binary Search Trees: search algorithm 54 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template bool bSearchTreeType ::search(const elemType& searchItem) { nodeType *current; bool found = false; if(root == NULL) cout<<"Emtpy tree!"<<endl; else { current = root; while(current != NULL && !found) { if(current->info == searchItem) found = true; else if(current->info > searchItem) current = current->llink; else current = current->rlink; }//end while }//end else return found; }//end search
55
5. Binary Search Trees: insert 55 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Create a new node and copy insertItem into the new node. Also set llink and rlink of the new node to NULL. if root is NULL, the tree is empty so make root point to the new node. else current = root while(current is not NULL) trailCurrent = current if(current->info is the same as the insertItem) Error and exit else if(current->info > insertItem) follow llink of current else follow rlink of current if (trailCurrent->info > insertItem) make the new node the left child of trailCurrent else make the new node the rightchild of trailCurrent
56
5. Binary Search Trees: insert 56 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 60 5070 305880 7746 root Insert 52 52
57
5. Binary Search Trees: insert 57 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 60 5070 305880 7746 root Insert 82 82
58
5. Binary Search Trees: insert 58 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void bSearchTreeType ::insert(const elemType& insertItem) { nodeType *current; nodeType *trailCurrent; nodeType *newNode; newNode = new nodeType ; assert(newNode != NULL); newNode->info = insertItem; newNode->llink = NULL; newNode->rlink = NULL; if(root == NULL) root = newNode; else { // next slide }
59
5. Binary Search Trees: insert 59 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 current = root; while(current != NULL) { trailCurrent = current; if(current->info == insertItem) { cout<<"Item is already in the list!"<<endl; return; } else if(current->info > insertItem) current = current->llink; else current = current->rlink; }//end while if(trailCurrent->info > insertItem) trailCurrent->llink = newNode; else trailCurrent->rlink = newNode;
60
5. Binary Search Trees: delete: Case1 60 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 root 60 5070 305880 7746 Delete 46: Search the node 46 Delete the node 46. Set the right link of 30 to NULL Delete 77: Search the node 77 Delete the node 77. Set the left link of 80 to NULL
61
5. Binary Search Trees: delete: Case2 61 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 root 60 5070 305880 7746 Delete 70: Search the node 70 Set the right link of 60 to point the node 80 Delete the node 70. Delete 30: Search the node 30 Set the llink link of 50 to point the node 46 Delete the node 30.
62
5. Binary Search Trees: delete: Case3 62 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 root 60 5070 305880 7746 Delete 80: Search the node 80 Set the right (sometimes left) link of 70 to point the node 77 Delete the node 80.
63
5. Binary Search Trees: delete: Case4 63 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Delete 50: Search the node 50 Find the first predecessor of 50 (the largest number less than 50) Follow the left link of 50 Locate the rightmost node of the left subtree of 50 (48) Swap 50 and 48 Delete the node with info 50 root 60 5070 305880 7735 48 33
64
5. Binary Search Trees: delete: Case4 64 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Delete 50 root 60 4870 305880 7735 33
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.