Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.

Similar presentations


Presentation on theme: "1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len."— Presentation transcript:

1 1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

2 2 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Tree Has a Root Node ROOT NODE

3 3 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Leaf nodes have no children LEAF NODES

4 4 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Tree Has Levels LEVEL 0

5 5 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Level One LEVEL 1

6 6 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Level Two LEVEL 2

7 7 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Subtree LEFT SUBTREE OF ROOT NODE

8 8 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Another Subtree RIGHT SUBTREE OF ROOT NODE

9 9 A binary tree is a structure in which: Each node can have at most two children, and in which a unique path exists from the root to every other node. The two children of a node are called the left child and the right child, if they exist. Binary Tree

10 10 A Binary Tree Q V T K S A E L

11 11 How many leaf nodes? Q V T K S A E L

12 12 How many descendants of Q? Q V T K S A E L

13 13 How many ancestors of K? Q V T K S A E L

14 14 Implementing a Binary Tree with Pointers and Dynamic Data Q V T K S A E L

15 15 Each node contains two pointers struct TreeNode { ItemType info; // Data member TreeNode* left; // Pointer to left child TreeNode* right; // Pointer to right child };. left. info. right NULL ‘A’ 6000

16 // BINARY SEARCH TREE SPECIFICATION class TreeType { public: TreeType ( ); // constructor ~TreeType ( ); // destructor bool IsEmpty ( ) const; bool IsFull ( ) const; int NumberOfNodes ( ) const; void InsertItem ( ItemType item ); void DeleteItem (ItemType item ); void RetrieveItem ( ItemType& item, bool& found ); void PrintTree (ofstream& outFile) const;... private: TreeNode* root; }; 16

17 17 TreeType CharBST; ‘J’ ‘E’ ‘A’ ‘S’ ‘H’ TreeType ~TreeType IsEmpty InsertItem Private data: root RetrieveItem PrintTree.

18 18 A special kind of binary tree in which: 1. Each node contains a distinct data value, 2. The key values in the tree can be compared using “greater than” and “less than”, and 3. The key value of each node in the tree is less than every key value in its right subtree, and greater than every key value in its left subtree. A Binary Search Tree (BST) is...

19 19 Depends on its key values and their order of insertion. Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order. The first value to be inserted is put into the root node. Shape of a binary search tree... ‘J’

20 20 Thereafter, each value to be inserted begins by comparing itself to the value in the root node, moving left it is less, or moving right if it is greater. This continues at each level until it can be inserted as a new leaf. Inserting ‘E’ into the BST ‘J’ ‘E’

21 21 Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘F’ into the BST ‘J’ ‘E’ ‘F’

22 22 Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘T’ into the BST ‘J’ ‘E’ ‘F’ ‘T’

23 23 Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘A’ into the BST ‘J’ ‘E’ ‘F’ ‘T’ ‘A’

24 24 is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order? What binary search tree... ‘A’

25 25 obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order. Binary search tree... ‘A’ ‘E’ ‘F’ ‘J’ ‘T’

26 26 Another binary search tree Add nodes containing these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’ ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘P’

27 27 Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’

28 // BINARY SEARCH TREE SPECIFICATION class TreeType { public: TreeType ( ) ; // constructor ~TreeType ( ) ; // destructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int NumberOfNodes ( ) const ; void InsertItem ( ItemType item ) ; void DeleteItem (ItemType item ) ; void RetrieveItem ( ItemType& item, bool& found ) ; void PrintTree (ofstream& outFile) const ;... private: TreeNode * root ; }; 28

29 // SPECIFICATION (continued) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // RECURSIVE PARTNERS OF MEMBER FUNCTIONS void PrintHelper ( TreeNode * ptr, ofstream& outFile ) ; void InsertHelper ( TreeNode * & ptr, ItemType item ) ; void RetrieveHelper ( TreeNode * ptr, ItemType& item, bool& found ) ; void DestroyHelper ( TreeNode * ptr ) ; 29

30 // BINARY SEARCH TREE IMPLEMENTATION // OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - TreeType :: TreeType ( ) // constructor { root = NULL ; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool TreeType :: IsEmpty( ) const { return ( root == NULL ) ; } 30

31 void TreeType :: RetrieveItem ( ItemType& item, bool& found ) { RetrieveHelper ( root, item, found ) ; } void RetrieveHelper ( TreeNode * ptr, ItemType& item, bool& found) { if ( ptr == NULL ) found = false ; else if ( item info )// GO LEFT RetrieveHelper( ptr->left, item, found ) ; else if ( item > ptr->info ) // GO RIGHT RetrieveHelper( ptr->right, item, found ) ; else { item = ptr->info ; found = true ; } 31

32 void TreeType :: InsertItem ( ItemType item ) { InsertHelper ( root, item ) ; } void InsertHelper ( TreeNode * & ptr, ItemType item ) { if ( ptr == NULL ) { // INSERT item HERE AS LEAF ptr = new TreeNode ; ptr->right = NULL ; ptr->left = NULL ; ptr->info = item ; } else if ( item info )// GO LEFT InsertHelper( ptr->left, item ) ; else if ( item > ptr->info ) // GO RIGHT InsertHelper( ptr->right, item ) ; } 32

33 33 Inorder Traversal: A E H J M T Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree last Print second

34 // INORDER TRAVERSAL void TreeType :: PrintTree ( ofstream& outFile ) const { PrintHelper ( root, outFile ) ; } void PrintHelper ( TreeNode * ptr, ofstream& outFile ) { if ( ptr != NULL ) { PrintHelper( ptr->left, outFile ) ;// Print left subtree outFile info ; PrintHelper( ptr->right, outFile ) ;// Print right subtree } 34

35 35 Preorder Traversal: J E A H T M Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree secondPrint right subtree last Print first

36 36 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree second Print last Postorder Traversal: A H E M Y T J

37 TreeType :: ~TreeType ( )// DESTRUCTOR { DestroyHelper ( root ) ; } void DestroyHelper ( TreeNode * ptr ) // Post: All nodes of the tree pointed to by ptr are deallocated. { if ( ptr != NULL ) { DestroyHelper ( ptr->left ) ; DestroyHelper ( ptr->right ) ; delete ptr ; } 37

38 38 Deleting a Node l Find node (return parent and node) l Four Cases: l Case 1 leaf node l Case 2 no left subtree l Case 3 no right subtree l Case 4 left and right subtrees

39 39 Case 1 Leaf Node l Point parent to NULL l Delete node

40 40 Case 2 No Left Subtree l Point parent to node right subtree l Delete node

41 41 Case 3 No Right Subtree l Point parent to left subtree l Delete node

42 42 Case 4 Left and Right Subtrees l Find node with greatest key in left subtree (or smallest key in right subtree) l Copy greatest (or smallest) key node data to delete node data l Delete greatest (or smallest) key node


Download ppt "1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len."

Similar presentations


Ads by Google