Dynamic Dictionaries: Primary Operations
Dynamic Dictionaries: Primary Operations
Primary Operations:
get(key) => search put(key, element) => insert remove(key) => delete
Additional operations:
ascend() get(index) remove(index)
D is number of buckets
10 6 15 30
40
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
Replace with largest key in left subtree (or smallest in right subtree).
10 6 15 30
40
2 7
18
25
35
Replace with largest key in left subtree (or smallest in right subtree).
8 6 15 30
40
2 7
18
25
35
Replace with largest key in left subtree (or smallest in right subtree).
8 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
18
25
35
10 6 15 30
40
2 7
25
35
Complexity is O(height).
25
Follow rightmost path to max element. Follow leftmost path to min element. Search and/or remove => O(h) time.
Initialize
20
10
6 15 30
40
25
Sort n elements.
Initialize search tree. Output in inorder (O(n)).
Initialize must take O(n log n) time, because it isnt possible to sort faster than O(n log n).
10 6 15
Meld
1
5
12
10
17
6
8
17
15
12
=
1
15
10
AVL Tree
binary tree for every node x, define its balance factor
balance factor of x = height of left subtree of x height of right subtree of x
Balance Factors
-1
1
0 1
1
-1 0
-1 0
The height of every n node binary tree is at least log2 (n+1). log2 (n+1) <= height <= 1.44 log2 (n+2)
Nh, h > 1
L
Both L and R are AVL trees. The height of one is h-1. The height of the other is h-2. The subtree whose height is h-1 has Nh-1 nodes. The subtree whose height is h-2 has Nh-2 nodes. So, Nh = Nh-1 + Nh-2 + 1.
Fibonacci Numbers
1
7 0 3 0 1 5 0 8 -1 20 0 0 1 30 35 0 40
1
-1 45 0 60
25