Chapter 11 Binary Trees Dr. Youssef Harrath

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

DATA STRUCTURES USING C++ Chapter 5
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Chapter 8 Binary Search Tree 1 Fall Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Gordon College Prof. Brinton
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Linked Lists Dr. Youssef Harrath
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 6.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
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.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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, Binary Trees, and Binary Search Trees COMP171.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Data Structures Using Java1 Chapter 10 Binary 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.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Binary Search Trees (BST)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Data Structure and Algorithms
Binary Search Tree (BST)
Trees.
Data Structures Using C++ 2E
Chapter 20: Binary Trees.
Chapter 7 TREES.
Data Structures Using Java
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Binary Trees, Binary Search Trees
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Chapter 11 Binary Trees Dr. Youssef Harrath

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

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

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

2. Binary Trees: Introduction A BC DEF HG Node Empty Directed edge 5 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

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

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

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

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

2. Binary Trees: Introduction A BC DEF HG ABCDEGHF root 11 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4. Implementing Binary Trees: destructor 44 template binaryTreeType ::~binaryTreeType() { destroy(root); } Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

5. Binary Search Trees 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

5. Binary Search Trees 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

5. Binary Search Trees 48 Binary search tree 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

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

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

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

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

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

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

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

5. Binary Search Trees: insert 56 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/ root Insert 52 52

5. Binary Search Trees: insert 57 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/ root Insert 82 82

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 }

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;

5. Binary Search Trees: delete: Case1 60 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 root 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

5. Binary Search Trees: delete: Case2 61 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 root 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.

5. Binary Search Trees: delete: Case3 62 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 root Delete 80:  Search the node 80  Set the right (sometimes left) link of 70 to point the node 77  Delete the node 80.

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

5. Binary Search Trees: delete: Case4 64 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Delete 50 root