Download presentation
Presentation is loading. Please wait.
1
Chapter 10.1 Binary Search Trees
6 9 2 4 1 8 < > = Chapter 10.1 Binary Search Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Nancy M. Amato And Jory Denny 1 1
2
Binary Search Trees A binary search tree is a binary tree storing entries (๐, ๐) (i.e., key-value pairs) at its internal nodes and satisfying the following property: Let ๐ข, ๐ฃ, and ๐ค be three nodes such that ๐ข is in the left subtree of ๐ฃ and ๐ค is in the right subtree of ๐ฃ. Then ๐๐๐ฆ ๐ข โค๐๐๐ฆ ๐ฃ โค๐๐๐ฆ ๐ค External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8
3
Search Dictionaries 9/11/2018 11:31 PM < > = 6 9 2 4 1 8
To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found Example: find(4) Algorithms for floorEntry and ceilingEntry are similar Algorithm Search(๐, ๐ฃ) if ๐ฃ.isExternal return ๐ฃ if ๐<๐ฃ.key return Search ๐, ๐ฃ.left else if ๐=๐ฃ.key return v else //๐>๐ฃ.key() return Search ๐, ๐ฃ.right
4
Exercise Show the search paths for the following keys: 8,3,2 6 9 2 4 1
5
Insertion 6 9 2 4 1 8 < > w To perform operation put(๐, ๐ฃ), we search for key ๐ (using Search(๐)) Assume ๐ is not already in the tree, and let ๐ค be the leaf reached by the search We insert ๐ at node ๐ค and expand ๐ค into an internal node Example: insert 5 6 9 2 4 1 8 5 w
6
Exercise Binary Search Trees
Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree 55, 68, 12, 9, 30, 59, 73
7
Deletion < To perform operation erase ๐ , we search for key ๐ >
6 9 2 4 1 8 5 v w < > To perform operation erase ๐ , we search for key ๐ Assume key ๐ is in the tree, and let ๐ฃ be the node storing ๐ If node ๐ฃ has a leaf child ๐ค, we remove ๐ฃ and ๐ค from the tree with operation removeAboveExternal ๐ค , which removes ๐ค and its parent Example: remove 4 6 9 2 5 1 8
8
Deletion (cont.) 3 1 8 6 9 5 v w z 2 We consider the case where the key ๐ to be removed is stored at a node ๐ฃ whose children are both internal we find the internal node ๐ค that follows ๐ฃ in an inorder traversal we copy ๐ค.key into node ๐ฃ we remove node ๐ค and its left child ๐ง (which must be a leaf) by means of operation removeAboveExternal(๐ง) Example: remove 3 5 1 8 6 9 v 2
9
Exercise Binary Search Trees
Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree 30, 40, 24, 58, 48, 26, 11, 13 Now, remove the item with key 30. Draw the resulting tree Now remove the item with key 48. Draw the resulting tree.
10
Performance Consider an ordered map with ๐ items implemented by means of a binary search tree of height โ Space used is ๐ ๐ Methods find ๐ , floorEntry(๐), ceilingEntry ๐ , put ๐, ๐ฃ , and erase ๐ take ๐ โ time The height โ is ๐(๐) in the worst case and ๐ log ๐ in the best case
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.