Download Data Structures for Dynamic Sets Operations on Dynamic Sets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Linked list wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Operations on Dynamic Sets
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 finite dynamic
sets. Different data structures support different kinds of data
manipulations, e.g.,
§ dictionary: insert, delete, test for membership
§ priority queue: insert, extract-max
•
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
returns nil)
MINIMUM (S) returns element with the smallest key
MAXIMUM (S) returns element with the largest key
SUCCESSOR (S, x) returns element with the next key larger than
key[x]
PREDECESSOR (S, x) returns element with the 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).
1
Elementary Data Structures (Ch. 10)
2
Binary Search Trees (Ch. 12)
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.
Strategy: visit left(x), visit x, visit right(x)
Inorder-Tree-Walk(x) /** start at root **/
1. if x ≠ NIL then
2.
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 algorithms for pre-order
and post-order traversals of
BSTs are covered in a
homework problem.
5
The iterative version
is more efficient, in
terms of space used,
on most computers.
Recursive-Tree-Search(x, k)
1. if (x = NIL) or (k = key[x]) then
2.
return x
3. if (k < key[x] then
4.
return Tree-Search (left(x), k)
5. else return Tree-Search (right(x), k)
Both have running
times of O(h), where
h is the height of the
tree.
Iterative-Tree-Search(x, k)
1. while (x ≠ NIL) and (k ≠ key[x]) do
2.
if k < key[x] then x ← left(x)
3.
else x ← right(x)
4. return x
6
1
BST Succssor & Predecessor
BST Min & Max
Tree-Maximum( x)
1. while right[x] ≠ NIL do
2.
x ← right[x]
3. return x
Tree-Minimum( x)
1. while left[x] ≠ NIL do
2.
x ← left[x]
3. return x
Tree-Successor( x)
1. if right(x) ≠ NIL then
2.
return Tree-Minimum(right[x])
3. temp ← parent[x]
4. while (temp ≠ NIL and x = right(temp))
5.
x ← temp
6.
temp ← parent[temp]
7. return temp
o If x has a rightchild, then
successor(x) is the smallest
node in the subtree rooted at
right(x).
o If x has no rightchild, then
successor(x) is the nearest
ancestor of x whose left
child is also an ancestor of x
(or x itself)
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 .
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 also an ancestor of x
(or x itself)
Both have running times of O(h), where h is the height of the
tree.
Tree-Predecessorr(x)
1. if left[x]≠ NIL then
2.
return Tree-Maximum(left[x])
3. temp ← parent[x]
4. while (temp ≠ NIL and x = left[temp]))
5.
x ← temp
6.
temp ← parent[temp]
7. return temp
7
8
BST Successor
BST Predecessor
x 20
18
17
19
x 20
temp
22
21
18
25
16
25
Case where right[x] not
equal to NIL
x
20
19
19
21
16
25
25
Case where left[x] not
equal to NIL
x 20
21
22
x
18 x
23
18
17
23
20 temp
15
18
17
17
14
temp 21
temp
22
22
19
21
24
19
Cases where leftt[x] equal to NIL
Cases where right[x] equal to NIL
9
BST Insert
Insert(T, z)
1. y ← NIL
2. x ← root[T]
3. while x ≠ NIL do
4.
y← x
5.
if key[z] < key[x]
6.
x ← left[x]
7.
else x ← right[x]
8. parent[z] ← y
9. if y = NIL then
10.
root[T] ← z
11. else if key[z] < key [y] then
12.
left[y] ← z
13. else right[y] ← z
20
10
BST Delete
Input is a BST T and a node z such that
left[z] = right[z] = NIL
11
Delete(T, z)
1. if left[z] = NIL or right[z] = NIL then
2.
y←z
3. else y ← TREE-SUCCESSOR(z)
4. if left[y] ≠ NIL then
5.
x ← left[y]
6. else x ← right[y]
7. if x ≠ NIL then
8.
parent[x] ← p[y]
9. if parent[y] = NIL then
10.
root[T] ← x
11. else if y = left[parent[y]] then
12.
left[parent[y] ← x
13. else right[parent[y]] ← x
14. if y ≠ z then
15.
key[z] ← key[y]
16.
copy y’s satellite data tp z
17. return y
Input: BST T containing node z to be
deleted. Three cases:
1) z has no children. Just remove it.
2) z has only one child. Splice out z.
3) z has two children. Splice out z’s
successor y, which has at most one
child, swap z and y’s data, and
return y.
What is running time of:
Successor(x)?
Predecessor(x)?
Insert(T, x)?
Delete(T, x)?
12
2