Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10.1 Binary Search Trees

Similar presentations


Presentation on theme: "Chapter 10.1 Binary Search Trees"โ€” Presentation transcript:

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


Download ppt "Chapter 10.1 Binary Search Trees"

Similar presentations


Ads by Google