CSCS-200 Data Structure and Algorithms Lecture-19-20.

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Trees, Binary Trees, and Binary Search Trees COMP171.
BST Data Structure A BST node contains: A BST contains
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Trees CS 202 – Fundamental Structures of Computer Science II Bilkent.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary search tree. Removing a node
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Lecture No.15 Data Structures Dr. Sohail Aslam
Binary Search Trees.
Binary Search Trees Why this is a useful data structure. Terminology
Chapter 20: Binary Trees.
Binary Search Trees.
CMSC 341 Lecture 10 Binary Search Trees
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 341 Binary Search Trees 11/19/2018.
Lecture No.16 Data Structures Dr. Sohail Aslam.
CMSC 341 Binary Search Trees.
CMSC 341 Binary Search Trees 1/3/2019.
Operations on Binary Tree
CMSC 341 Binary Search Trees 2/23/2019.
Pointers & Dynamic Data Structures
CMSC 341 Binary Search Trees.
Tree.
CMSC 341 Binary Search Trees 5/8/2019.
Trees BST, AVL, SPLAY TREES
CMSC 341 Binary Search Trees 5/28/2019.
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
CMSC 341 Binary Search Trees 2/21/2006.
Presentation transcript:

CSCS-200 Data Structure and Algorithms Lecture-19-20

2 Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left- to-right order.

3 Level-order Traversal Level-order:

4 Level-order Traversal How do we do level-order traversal? Surprisingly, if we use a queue instead of a stack, we can visit the nodes in level- order. Here is the code for level-order traversal:

5  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

6  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

7  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

8  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

9  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

10  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

11  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

12  Level-order Traversal void levelorder(TreeNode * treeNode) { Queue * > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }

13 Level-order Traversal Queue: 14 Output:

14 Level-order Traversal Queue: 4 15 Output:

15 Level-order Traversal Queue: Output:

16 Level-order Traversal Queue: Output:

Level-order Traversal Queue: 9 18 Output:

18 Level-order Traversal Queue: 18 7 Output:

19 Level-order Traversal Queue: Output:

20 Level-order Traversal Queue: Output:

21 Level-order Traversal Queue: Output:

22 Level-order Traversal Queue: 5 17 Output:

23 Level-order Traversal Queue: 17 Output:

24 Level-order Traversal Queue: Output:

25 Storing other Type of Data The examples of binary trees so far have been storing integer data in the tree node. This is surely not a requirement. Any type of data can be stored in a tree node. Here, for example, is the C++ code to build a tree with character strings.

26  Binary Search Tree with Strings void wordTree() { TreeNode * root = new TreeNode (); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }

27  Binary Search Tree with Strings void wordTree() { TreeNode * root = new TreeNode (); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }

28  Binary Search Tree with Strings void wordTree() { TreeNode * root = new TreeNode (); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }

29  Binary Search Tree with Strings void wordTree() { TreeNode * root = new TreeNode (); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }

30  Binary Search Tree with Strings void wordTree() { TreeNode * root = new TreeNode (); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }

31  Binary Search Tree with Strings void insert(TreeNode * root, char* info) { TreeNode * node = new TreeNode (info); TreeNode *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

32  Binary Search Tree with Strings void insert(TreeNode * root, char* info) { TreeNode * node = new TreeNode (info); TreeNode *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

33  Binary Search Tree with Strings void insert(TreeNode * root, char* info) { TreeNode * node = new TreeNode (info); TreeNode *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

34  Binary Search Tree with Strings void insert(TreeNode * root, char* info) { TreeNode * node = new TreeNode (info); TreeNode *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

35  Binary Search Tree with Strings void insert(TreeNode * root, char* info) { TreeNode * node = new TreeNode (info); TreeNode *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

36  Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }

37  Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }

38  Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }

39 Binary Search Tree with Strings Output: abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

40 Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket  Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.

41 Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket  Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.  This should not come as a surprise if you consider how we built the BST.

42 Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket  Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.  This should not come as a surprise if you consider how we built the BST.  For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.

43 Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket  Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.  This should not come as a surprise if you consider how we built the BST.  For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.  Inorder prints the left subtree, then the node finally the right subtree.

44 Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket  Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.  This should not come as a surprise if you consider how we built the BST.  For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.  Inorder prints the left subtree, then the node finally the right subtree.  Building a BST and doing an inorder traversal leads to a sorting algorithm.

45 Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket  Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.  This should not come as a surprise if you consider how we built the BST.  For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.  Inorder prints the left subtree, then the node finally the right subtree.  Building a BST and doing an inorder traversal leads to a sorting algorithm.

46 Deleting a node in BST As is common with many data structures, the hardest operation is deletion. Once we have found the node to be deleted, we need to consider several possibilities. If the node is a leaf, it can be deleted immediately.

47 Deleting a node in BST If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor

48 Deleting a node in BST The inorder traversal order has to be maintained after the delete 

49 Deleting a node in BST The inorder traversal order has to be maintained after the delete 

50 Deleting a node in BST The complicated case is when the node to be deleted has both left and right subtrees. The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node.

51 Deleting a node in BST Delete(2): locate inorder successor Inorder successor

52 Deleting a node in BST Delete(2): locate inorder successor Inorder successor  Inorder successor will be the left-most node in the right subtree of 2.  The inorder successor will not have a left child because if it did, that child would be the left-most node.

53 Deleting a node in BST Delete(2): copy data from inorder successor 

54 Deleting a node in BST Delete(2): remove the inorder successor  

55 Deleting a node in BST Delete(2)  

56 C++ code for delete ‘delete’ is C++ keyword. We will call our deleteNode routine remove. Here is the C++ code for remove.

57  C++ code for delete TreeNode * remove(TreeNode * tree, int info) { TreeNode * t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

58  C++ code for delete TreeNode * remove(TreeNode * tree, int info) { TreeNode * t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

59  C++ code for delete TreeNode * remove(TreeNode * tree, int info) { TreeNode * t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

60  C++ code for delete TreeNode * remove(TreeNode * tree, int info) { TreeNode * t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

61  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode * minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

62  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode * minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

63  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode * minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

64  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode * minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

65  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode * minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

66  C++ code for delete else { // case 1 TreeNode * nodeToDelete = tree; if( tree->getLeft() == NULL ) // will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }

67  C++ code for delete else { // case 1 TreeNode * nodeToDelete = tree; if( tree->getLeft() == NULL ) // will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }

68  C++ code for delete else { // case 1 TreeNode * nodeToDelete = tree; if( tree->getLeft() == NULL ) // will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }

69  C++ code for delete TreeNode * findMin(TreeNode * tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }

70  C++ code for delete TreeNode * findMin(TreeNode * tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }

71  C++ code for delete TreeNode * findMin(TreeNode * tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }

72 BinarySearchTree.h Let us design the BinarySearchTree class (factory).

73  BinarySearchTree.h #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #include // For NULL // Binary node and forward declaration template class BinarySearchTree;

74  BinarySearchTree.h #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #include // For NULL // Binary node and forward declaration template class BinarySearchTree;

75  BinarySearchTree.h template class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree ; };

76  BinarySearchTree.h template class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree ; };

77  BinarySearchTree.h template class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree ; };

78  BinarySearchTree.h template class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree ; };

79 BinarySearchTree.h template class BinarySearchTree { public: BinarySearchTree( const EType& notFound ); BinarySearchTree( const BinarySearchTree& rhs ); ~BinarySearchTree( ); const EType& findMin( ) const; const EType& findMax( ) const; const EType& find( const EType & x ) const; bool isEmpty( ) const; void printInorder( ) const;

80 BinarySearchTree.h void insert( const EType& x ); void remove( const EType& x ); const BinarySearchTree & operator= ( const BinarySearchTree & rhs );

81 BinarySearchTree.h private: BinaryNode * root; // ITEM_NOT_FOUND object used to signal failed finds const EType ITEM_NOT_FOUND; const EType& elementAt( BinaryNode * t ); void insert(const EType& x, BinaryNode * & t); void remove(const EType& x, BinaryNode * & t); BinaryNode * findMin(BinaryNode * t); BinaryNode * findMax(BinaryNode * t); BinaryNode * find(const EType& x, BinaryNode * t ); void makeEmpty(BinaryNode * & t); void printInorder(BinaryNode * t); }; #endif