1 / 55

Nell Dale Chapter 8 Binary Search Trees

C++ Plus Data Structures. Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus. Binary search. for an element in a sorted list stored sequentially in an array: O(Log 2 N) in a linked list: ? (midpoint = ?).

Download Presentation

Nell Dale Chapter 8 Binary Search Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Plus Data Structures Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

  2. Binary search for an element in a sorted list stored sequentially • in an array: O(Log2N) • in a linked list: ? (midpoint = ?)

  3. Goals of this chapter • Introduce some basic tree vocabulary • Develop algorithms • Implement operations needed to use a binary search tree

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

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

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

  7. A Tree Has Levels Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 0 Level of a node: its distance from the root

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

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

  10. A binary tree with N nodes: • Maximum number of levels: N • Minimum number of levels: logN + 1 e.g., N=8, 16, …, 1000

  11. The height of a tree: the maximum level in a tree -- a critical factor in determining how efficiently we can search for elements

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

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

  14. Binary Tree 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.

  15. A Binary Tree V Q L T A E K S

  16. A Binary Tree ? V Q L T A E K S

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

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

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

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

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

  22. // BINARY SEARCH TREE SPECIFICATION template< class ItemType > 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<ItemType>* root; }; 22

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

  24. A Binary Tree V Q L T A E K S Search for ‘S’?

  25. A Binary Search Tree (BST) is . . . 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.

  26. ‘J’ Shape of a binary search tree . . . 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.

  27. ‘J’ ‘E’ Inserting ‘E’ into the BST 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.

  28. ‘J’ ‘E’ ‘F’ Inserting ‘F’ into the BST 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.

  29. ‘J’ ‘T’ ‘E’ ‘F’ Inserting ‘T’ into the BST 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.

  30. ‘J’ ‘T’ ‘E’ ‘A’ ‘F’ Inserting ‘A’ into the BST 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.

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

  32. ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ Binary search tree . . . obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order. A “degenerate” tree!

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

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

  35. // BINARY SEARCH TREE SPECIFICATION template< class ItemType > 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<ItemType>* root ; }; 35

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

  37. // BINARY SEARCH TREE IMPLEMENTATION // OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - template< class ItemType > TreeType<ItemType> :: TreeType ( ) // constructor { root = NULL ; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - template< class ItemType > bool TreeType<ItemType> :: IsEmpty( ) const { return ( root == NULL ) ; } 37

  38. template< class ItemType > void TreeType<ItemType> :: RetrieveItem ( ItemType& item, bool& found ) { RetrieveHelper ( root, item, found ) ; } template< class ItemType > void RetrieveHelper ( TreeNode<ItemType>* ptr, ItemType& item, bool& found) { if ( ptr == NULL ) found = false ; else if ( item < ptr->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 ; } } 38

  39. template< class ItemType, class KF > void TreeType<ItemType, KF> :: RetrieveItem ( ItemType& item, KF key, bool& found ) // Searches for an element with the same key as item’s key. // If found, store it into item { RetrieveHelper ( root, item, key, found ) ; } template< class ItemType > void RetrieveHelper ( TreeNode<ItemType,KF>* ptr, ItemType& item, KF key, bool& found) { if ( ptr == NULL ) found = false ; else if ( key < ptr->info.key() ) // GO LEFT RetrieveHelper( ptr->left , item, key,found ) ; else if ( key > ptr->info.key() ) // GO RIGHT RetrieveHelper( ptr->right , item, key,found ) ; else { item = ptr->info ; found = true ; } } 39

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

  41. Delete() • Find the node in the tree • Delete the node from the tree • Three cases: 1. Deleting a leaf 2. Deleting a node with only one child 3. Deleting a node with two children Note: the tree must remain a binary tree and the search property must remain intact

  42. template< class ItemType > void TreeType<ItemType> :: DeleteItem ( ItemType& item ) { DeleteHelper ( root, item ) ; } template< class ItemType > void DeleteHelper ( TreeNode<ItemType>* ptr, ItemType& item) { if ( item < ptr->info ) // GO LEFT DeleteHelper( ptr->left , item ) ; else if ( item > ptr->info ) // GO RIGHT DeleteHelper( ptr->right , item ) ; else { DeleteNode(ptr); // Node is found: call DeleteNode } } 42

  43. template< class ItemType > void DeleteNode ( TreeNode<ItemType>*& tree ) { ItemType data; TreeNode< ItemType>* tempPtr; tempPtr = tree; if ( tree->left == NULL ) { tree = tree->right; delete tempPtr; } else if (tree->right == NULL ) { tree = tree->left; delete tempPtr; } else { // have two children GetPredecessor(tree->left, item); tree->info = item; DeleteHelper(tree->left, item);// Delete predecessor node (rec) } } 43

  44. template< class ItemType > void GetPredecessor( TreeNode<ItemType>* tree, ItemType& data ) // Sets data to the info member of the rightmost node in tree { while (tree->right != NULL) tree = tree->right; data = tree->info; } 44

  45. PrintTree() Traverse a list: -- forward -- backward Traverse a tree: -- there are many ways!

  46. ‘J’ Inorder Traversal: A E H J M T Y Print second tree ‘T’ ‘E’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree first Print right subtree last

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

  48. ‘J’ Preorder Traversal: J E A H T M Y Print first tree ‘T’ ‘E’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree second Print right subtree last

  49. ‘J’ Postorder Traversal: A H E M Y T J Print last tree ‘T’ ‘E’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree first Print right subtree second

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

More Related