Binary Search Trees 2 1 2014 Goodrich Tamassia
Binary Search Trees < 2 1 © 2014 Goodrich, Tamassia, Goldwasser 6 9 > 4 = Binary Search Trees 8 1
Ordered Maps Keys are assumed to come from a total order. Items are stored in order by their keys This allows us to support nearest neighbor queries (floor and ceiling): Item with largest key less than or equal to k Item with smallest key greater than or equal to k © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 2
Ordered/Sorted Maps Sorted Array n Insertion and deletion could move a lot of entries Skip Lists n n Keys could be duplicated in multiple levels Expected O(log n) time for search/get w Not worst-case O(log n) time © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 3
Binary Search Trees A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes: n n Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) An inorder traversal of a binary search trees visits the keys in increasing order 6 2 1 9 4 8 External nodes do not store items © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 4
Search To search for a key k, we trace a downward path starting at the root The next node visited depends on the comparison of k with the key of the current node If we reach a leaf, the key is not found Example: get(4): n Algorithm Tree. Search(k, v) if T. is. External (v) return v if k < key(v) return Tree. Search(k, left(v)) else if k = key(v) return v else { k > key(v) } return Tree. Search(k, right(v)) < 2 Call Tree. Search(4, root) The algorithms for nearest neighbor queries are similar © 2014 Goodrich, Tamassia, Goldwasser 1 Binary Search Trees 6 9 > 4 = 8 5
Insertion To perform operation put(k, o), we search for key k (using Tree. Search) Assume k is not already in the tree, and let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5 6 < 2 9 > 1 4 > w 6 2 1 9 4 8 5 © 2014 Goodrich, Tamassia, Goldwasser 8 Binary Search Trees w 6
Deletion (one child is a leaf) To perform operation remove(k), we search for key k Assume key k is in the tree, and let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation remove. External(w), which removes w and its parent Example: remove 4 © 2014 Goodrich, Tamassia, Goldwasser 6 < 2 9 > 4 v 1 w 8 5 6 2 1 Binary Search Trees 9 5 8 7
Deletion (both children are not leaves) find the internal node w that follows v in an inorder traversal copy key(w) into node v remove node w and its left child z (which must be a leaf) by means of operation remove. External(z) 1 3 v 2 8 6 w 5 z 1 Example: remove 3 5 v 2 8 6 © 2014 Goodrich, Tamassia, Goldwasser 9 Binary Search Trees 9 8
Deletion (both children are not leaves) find the internal node w that follows v in an inorder traversal Why? copy key(w) into node v remove node w and its left child z (which must be a leaf) by means of operation remove. External(z) 1 3 v 2 8 6 w 5 z 1 Example: remove 3 5 v 2 8 6 © 2014 Goodrich, Tamassia, Goldwasser 9 Binary Search Trees 9 9
Performance Consider an ordered map with n items BST of height h n n the space used is O(n) methods get, put and remove take O(h) time The height h is O(n) in the worst case and O(log n) in the best case © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 10
- Slides: 10