Sie sind auf Seite 1von 2

Operations on Dynamic Sets

Data Structures for Dynamic Sets • INSERT (S, x) adds element pointed to by x to S
Algorithms operate on data, which can be thought of as forming a • DELETE (S, x) removes element pointed to by x from S
set S. The data sets manipulated by algorithms are dynamic, • SEARCH (S, k) returns pointer to element x with key[x] = k (or
meaning they can grow, shrink, or otherwise change over time. returns nil)
• MINIMUM (S) returns element with the smallest key
Data Structures are structured ways to represent finite dynamic • MAXIMUM (S) returns element with the largest key
sets. Different data structures support different kinds of data • SUCCESSOR (S, x) returns element with the next key larger than
key[x]
manipulations, e.g., • PREDECESSOR (S, x) returns element with the next key smaller
§ dictionary: insert, delete, test for membership than key[x]
§ 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).

1 2

Binary Search Trees (Ch. 12)


Elementary Data Structures (Ch. 10) Binary Search Trees should also be review from CS102. Recall
These elementary data structures should be a review from CS102: that every tree node (internal or leaf) contains a key.
o arrays and linked lists (singly linked, doubly linked) Binary Search Tree Property: For every node x in tree,
o stacks (e.g., implemented with arrays and lists) o key[y] ≤ key[x] for every y in left(x) (left subtree of node x)
o queues (e.g., implemented with arrays and lists) o key[y] ≥ key[x] for every y in right(x) (right subtree of node x)
o rooted trees (e.g., arbitrary trees using pointers, complete
Operations on BSTs (all dynamic set operations are supported)
d-ary trees using arrays)
o INSERT (S, x), DELETE (S, x)
o SEARCH (S, k), MINIMUM (S), MAXIMUM (S)
o SUCCESSOR (S, x), P REDESSOR (S, x)

3 4

BST Traversals BST Search


• The BST property allows us to print out all keys in sorted order
using a simple recursive algorithm called an inorder tree walk. The iterative version Recursive-Tree-Search(x, k)
Strategy: visit left(x), visit x, visit right(x) is more efficient, in 1. if (x = NIL) or (k = key[x]) then
terms of space used, 2. return x
Inorder-Tree-Walk(x) /** start at root **/ on most computers. 3. if (k < key[x] then
1. if x ≠ NIL then 4. return Tree-Search (left(x), k)
2. Inorder-Tree-Walk(left(x)) 5. else return Tree-Search (right(x), k)
3. print key[x]
4. Inorder-Tree-Walk(right(x)) Both have running Iterative-Tree-Search(x, k)
times of O(h), where 1. while (x ≠ NIL) and (k ≠ key[x]) do
Running time = θ(n) • The algorithms for pre-order h is the height of the 2. if k < key[x] then x ← left(x)
(each node must be and post-order traversals of tree. 3. else x ← right(x)
visited at least once) BSTs are covered in a 4. return x
homework problem.
5 6

1
BST Succssor & Predecessor
BST Min & Max Tree-Successor( x)
o If x has a rightchild, then
The minimum element in a BST can always be found by successor(x) is the smallest 1. if right(x) ≠ NIL then
following left child pointers to a leaf (until a NIL left child node in the subtree rooted at 2. return Tree-Minimum(right[x])
pointer is encountered). Likewise, the maximum element can be
right(x). 3. temp ← parent[x]
o If x has no rightchild, then
found by following right child pointers to a leaf . successor(x) is the nearest
4. while (temp ≠ NIL and x = right(temp))
ancestor of x whose left 5. x ← temp
Tree-Minimum( x) Tree-Maximum( x) child is also an ancestor of x 6. temp ← parent[temp]
(or x itself) 7. return temp
1. while left[x] ≠ NIL do 1. while right[x] ≠ NIL do
2. x ← left[x] 2. x ← right[x] Tree-Predecessorr(x)
o If x has a leftchild, then
3. return x 3. return x 1. if left[x]≠ NIL then
predecessor(x) is the largest
node in the subtree rooted at 2. return Tree-Maximum(left[x])
left(x). 3. temp ← parent[x]
Both have running times of O(h), where h is the height of the o If x has no leftchild, then 4. while (temp ≠ NIL and x = left[temp]))
tree. predecessor(x) is the nearest
5. x ← temp
ancestor of x whose right
child is also an ancestor of x 6. temp ← parent[temp]
(or x itself) 7. return temp
7 8

BST Successor BST Predecessor


x 20 x 20

18 22 temp 18 22 temp
25 16
17 19 21 25 16 17 19 21 25 25
14 23
temp 21 20 temp
15 22

Case where right[x] not x 20 Case where left[x] not 21 x


x 20 18 x
equal to NIL equal to NIL
18 23
18 21

17 19 22 24
17 19 19 20

Cases where leftt[x] equal to NIL


Cases where right[x] equal to NIL
9 10

BST Insert BST Delete


Delete(T, z)
Insert(T, z) 1. if left[z] = NIL or right[z] = NIL then
2. y←z Input: BST T containing node z to be
1. y ← NIL deleted. Three cases:
3. else y ← TREE-SUCCESSOR(z)
2. x ← root[T] 1) z has no children. Just remove it.
3. while x ≠ NIL do 4. if left[y] ≠ NIL then 2) z has only one child. Splice out z.
Input is a BST T and a node z such that 5. x ← left[y]
4. y← x left[z] = right[z] = NIL
3) z has two children. Splice out z’s
6. else x ← right[y] successor y, which has at most one
5. if key[z] < key[x] child, swap z and y’s data, and
6. x ← left[x] 7. if x ≠ NIL then
return y.
7. else x ← right[x] 8. parent[x] ← p[y]
8. parent[z] ← y 9. if parent[y] = NIL then
10. root[T] ← x
9. if y = NIL then
10. root[T] ← z 11. else if y = left[parent[y]] then What is running time of:
12. left[parent[y] ← x Successor(x)?
11. else if key[z] < key [y] then Predecessor(x)?
12. left[y] ← z 13. else right[parent[y]] ← x
Insert(T, x)?
13. else right[y] ← z 14. if y ≠ z then Delete(T, x)?
15. key[z] ← key[y]
16. copy y’s satellite data tp z
11 17. return y 12

Das könnte Ihnen auch gefallen