Beruflich Dokumente
Kultur Dokumente
Algorithms operate on data, which can be thought of as forming a set S. The data sets manipulated by algorithms are dynamic, meaning they can grow, shrink, or otherwise change over time.! Data Structures are structured ways to represent nite dynamic sets. Different data structures support different kinds of data manipulations, e.g.,! !! dictionary: insert, delete, test for membership! !! priority queue: insert, extract-max!
Running Time of a dynamic set operation is usually measured in terms of the size of the set (i.e., number of elements currently in the set).!
BST Insert!
Insert(T, z) 1. y ! NIL 2. x ! root[T] 3. while x ! NIL 4. do y ! x 5. if key[z] < key[x] 6. then x ! left[x] 7. else x ! right[x] 8. parent[z] ! y 9. if y = NIL 10. then root[T] ! z 11. else if key[z] < key [y] 12. then left[y] ! z 13. else right[y] ! z
Input is a BST T and a node z such that left[z] = right[z] = NIL (z is a node with NIL children)
BST Traversals!
! The BST property allows us to print out all keys in sorted order using a simple recursive algorithm called an inorder tree walk. Strategy: visit left(x), visit x, visit right(x) Inorder-Tree-Walk(x) /** start at root **/ 1. if x " NIL 2.! then Inorder-Tree-Walk(left(x)) 3.! print key[x] 4.! Inorder-Tree-Walk(right(x)) Running time = #(n) (each node must be visited at least once) The iterative version is more efficient, in terms of space used, on most computers.
BST Search!
Recursive-Tree-Search(x, k) 1. if (x = NIL) or (k = key[x]) 2.! then return x 3.! if (k < key[x]) 4.! then return Tree-Search (left[x], k) 5.! else return Tree-Search (right[x], k) Iterative-Tree-Search(x, k) 1. while (x " NIL) and (k " key[x]) 2.! do if k < key[x] then x ! left[x] 3.! else x ! right[x] 4.! return x
Both have running times of O(h), where h is the height of the tree.
BST Successor!
o! If x has a right child, then successor(x) is the smallest node in the subtree rooted at right(x). o! If x has no right child, then successor(x) is the nearest ancestor of x whose left child is either an ancestor of x or x itself. Tree-Successor(x) 1. if right[x] " NIL 2.! then return Tree-Minimum(right[x]) 3.! temp ! parent(x) 4.! while temp " NIL and x = right[temp] 5.! do x ! temp 6.! temp ! parent(temp) 7.! return temp
Both have running times of O(h), where h is the height of the tree.
BST Predecessor!
Tree-Predecessor(x) 1. if left[x] " NIL then 2.! then return Tree-Maximum(left[x]) 3.! temp ! parent(x) 4.! while temp " NIL and x = left[temp] 5.! do x ! temp 6.! temp ! parent(temp) 7.! return temp o! If x has a leftchild, then predecessor(x) is the largest node in the subtree rooted at left[x]. o! If x has no leftchild, then predecessor(x) is the nearest ancestor of x whose right child is either an ancestor of x or x itself
Delete(T, z) 1. if left[z] = NIL or right[z] = NIL 2. then y ! z 3. else y ! TREE-SUCCESSOR(z) Input: BST T containing node z to be! 4. if left[y] ! NIL deleted. Three cases:! 5. then x ! left[y] 1) ! z has no children. Just remove it.! 6. else x ! right[y] 2)! z has only one child. Splice out z.! 3)! z has two children. Splice out zs ! 7. if x ! NIL successor y, which has at most one! 8. then parent[x] ! parent[y] ! ! child, swap z and ys data, and ! 9. if parent[y] = NIL ! return y.! 10. then root[T] ! x 11.! else What is running time of:! Successor(x)?! 12.! if y = left[parent[y]] Predecessor(x)?! 13. then left[parent[y]] ! x Insert(T, x)?! 14. else right[parent[y]] ! x Delete(T, x)?! 15. if y ! z 16. then key[z] ! key[y] 17. return y
BST Delete!