Sie sind auf Seite 1von 3

Data Structures for Dynamic Sets!

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!

Operations on Dynamic Sets!


! ! ! ! ! ! ! INSERT(S, x) adds element pointed to by x to S! DELETE(S, x) removes element pointed to by x from S! SEARCH(S, k) returns pointer to element x with key[x] = k (or nil)! MINIMUM(S) returns element with smallest key! MAXIMUM(S) returns element with largest key! SUCCESSOR(S, x) returns element with next key larger than key[x]! PREDECESSOR(S, x) returns element with next key smaller than key[x]!

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).!

Elementary Data Structures (Ch. 10)!


These elementary data structures should be a review from ! CS102:! o! arrays and linked lists (singly linked, doubly linked)! o! stacks (e.g., implemented with arrays and lists)! o! queues (e.g., implemented with arrays and lists)! o! rooted trees (e.g., arbitrary trees using pointers, complete d-ary trees using arrays)! "

Hash Tables (Ch. 11)!


Rely on distribution of data, amount of data, and storage mechanism to provide expected O(1) time for many dynamic set operations.! We will not cover this chapter until possibly later in the semester.!

Binary Search Trees (Ch. 12)!


Every tree node (internal or leaf) contains a key.

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

Binary Search Tree Property: For every node x in tree,


o! key[y] < key[x] for every y in left(x) (left subtree of x) o! key[y] > key[x] for every y in right(x) (right subtree of x) All dynamic set operations are supported on BSTs o! INSERT(S, x), DELETE(S, x) o! SEARCH(S, k), MINIMUM(S), MAXIMUM(S) o! SUCCESSOR(S, x), PREDESSOR(S, x)

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 Min & Max!


The minimum element in a BST can always be found by following left child pointers to a leaf (until a NIL left child pointer is encountered). Likewise, the maximum element can be found by following right child pointers to a leaf.
Tree-Minimum(x) 1. while left[x] ! NIL 2.! do x ! left[x] 3.! return x Tree-Maximum(x) 1. while right[x] ! NIL 2.! do x ! right[x] 3.! return x

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 Successor (nd successor of x)!


x! 20! 18! 17! 19! 22! 21! 25! 16! temp! 21! temp! 14! 15! x! 20! 18! 17! 19! 25!

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

Case where right[x] not ! equal to NIL !


17!

x! 20! 18! 19!

Cases where right[x] equal to NIL !

BST Predecessor (nd pred of node x)!


x! 20! 18! 17! 19! 22! 21! 25! 25! 20! temp! 22! 18! x! 21! 24! 19! 20! 16! temp! 23!

Case where left[x] not ! equal to NIL !

21! x! 23! 22!

Cases where left[x] equal to NIL !

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!

Minimizing Running Time!


Problem: worst case for binary search tree height is #(n) - no better than a linked list. Solution: Guarantee tree has small height (balanced tree) so that h = O(lgn). Method: restructure the tree if necessary. No extra work for searching, but requires extra work when inserting or deleting. Red-black trees: a special case of binary trees that avoids the worst-case behavior by ensuring that the tree is nearly balanced at all times.

Das könnte Ihnen auch gefallen