Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert ...
-
date post
15-Jan-2016 -
Category
Documents
-
view
230 -
download
0
Transcript of Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert ...
![Page 1: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/1.jpg)
Dynamic Dictionaries
• Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete
• Additional operations: Ascend() Get(index) Delete(index)
![Page 2: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/2.jpg)
Complexity Of Dictionary OperationsGet(), Insert() and Delete()
Data Structure Worst Case Expected
Hash Table O(n) O(1)
Binary SearchTree
O(n) O(log n)
BalancedBinary SearchTree
O(log n) O(log n)
n is number of elements in dictionary
![Page 3: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/3.jpg)
Complexity Of Other OperationsAscend(), Get(index), Delete(index)
Data Structure Ascend Get and Delete
Hash Table
O(D + n log n) O(D + n log n)
Indexed BST O(n) O(n)
Indexed Balanced BST
O(n) O(log n)
D is number of buckets
![Page 4: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/4.jpg)
AVL Tree
• binary tree
• for every node x, define its balance factorbalance factor of x = height of left subtree of x
– height of right subtree of x
• balance factor of every node x is – 1, 0, or 1
![Page 5: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/5.jpg)
Balance Factors
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
This is an AVL tree.
![Page 6: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/6.jpg)
Height Of An AVL Tree
The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2).
The height of every n node binary tree is at least log2 (n+1).
log2 (n+1) <= height <= 1.44 log2 (n+2)
![Page 7: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/7.jpg)
Proof Of Upper Bound On Height
• Let Nh = min # of nodes in an AVL tree whose height is h.
• N0 = 0.
• N1 = 1.
![Page 8: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/8.jpg)
Nh, h > 1
• 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.
L R
![Page 9: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/9.jpg)
Fibonacci Numbers
• F0 = 0, F1 = 1.
• Fi = Fi-1 + Fi-2 , i > 1.
• N0 = 0, N1 = 1.
• Nh = Nh-1 + Nh-2 + 1, i > 1.
• Nh = Fh+2 – 1.
• Fi ~ i/sqrt(5).
sqrt
![Page 10: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/10.jpg)
AVL Search Tree
0 0
0 0
1
0
-1 0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
![Page 11: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/11.jpg)
Insert(9)
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
90
-1
0
10
7
83
1 5
30
40
20
25
35
45
60
![Page 12: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/12.jpg)
Insert(29)
0 0
0 0
1
0
0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
29
0
-1
-1-2
RR imbalance => new node is in right subtree of right subtree of white node (node with bf = –2)
![Page 13: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/13.jpg)
0 0
0 0
1
0
0
1
0
-1
1
-110
7
83
1 5
30
40
2535
45
600
RR rotation. 20
029
Insert(29)
![Page 14: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/14.jpg)
Insert
• Following insert, retrace path towards root and adjust balance factors as needed.
• Stop when you reach a node whose balance factor becomes 0, 2, or –2, or when you reach the root.
• The new tree is not an AVL tree only if you reach a node whose balance factor is either 2 or –2.
• In this case, we say the tree has become unbalanced.
![Page 15: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/15.jpg)
A-Node
• Let A be the nearest ancestor of the newly inserted node whose balance factor becomes +2 or –2 following the insert.
• Balance factor of nodes between new node and A is 0 before insertion.
![Page 16: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/16.jpg)
Imbalance Types
• RR … newly inserted node is in the right subtree of the right subtree of A.
• LL … left subtree of left subtree of A.
• RL… left subtree of right subtree of A.
• LR… right subtree of left subtree of A.
![Page 17: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/17.jpg)
LL Rotation
• Subtree height is unchanged.• No further adjustments to be done.
Before insertion.
1
0
A
B
BL BR
AR
h h
h
A
B
B’L BR
AR
After insertion.
h+1 h
h
B
A
After rotation.
BR
hAR
h
B’L
h+10
0
1
2
![Page 18: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/18.jpg)
LR Rotation (case 1)
• Subtree height is unchanged.• No further adjustments to be done.
Before insertion.
1
0
A
B
A
B
After insertion.
C
C
A
After rotation.
B
0
-1
2
0 0
0
![Page 19: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/19.jpg)
LR Rotation (case 2)
• Subtree height is unchanged.• No further adjustments to be done.
C
A
CR
h-1AR
h
1
0
A
B
BL
CR
AR
h
h-1
h
0
CL
h-1
C
A
B
BL
CR
AR
h
h-1
h
C’L
h
C
B
BL
hC’L
h
1
-1
2
0 -1
0
![Page 20: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/20.jpg)
LR Rotation (case 3)
• Subtree height is unchanged.• No further adjustments to be done.
1
0
A
B
BL
CR
AR
h
h-1
h
0
CL
h-1
C
A
B
BL
C’R
AR
h
h
h
CL
h-1
C-1
-1
2 C
A
C’R
hAR
h
B
BL
hCL
h-1
1 0
0
![Page 21: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/21.jpg)
Single & Double Rotations
• Single LL and RR
• Double LR and RL LR is RR followed by LL RL is LL followed by RR
![Page 22: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/22.jpg)
LR Is RR + LL
A
B
BL
C’R
AR
h
h
h
CL
h-1
C-1
-1
2
After insertion.
A
C
CL
C’R
AR
h
h
BL
h
B
2
After RR rotation.
h-1
C
A
C’R
hAR
h
B
BL
hCL
h-1
1 0
0
After LL rotation.
![Page 23: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/23.jpg)
Delete An Element
0 0
0 0
1
0
-1 0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
Delete 8.
![Page 24: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/24.jpg)
Delete An Element
0 0
0
2
0
-1 0
1
0
-1
1
-110
7
3
1 5
30
40
20
25
35
45
60
• Let q be parent of deleted node.
• Retrace path from q towards root.
q
![Page 25: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/25.jpg)
New Balance Factor Of q
• Deletion from left subtree of q => bf--.• Deletion from right subtree of q => bf++.• New balance factor = 1 or –1 => no change in height of
subtree rooted at q.• New balance factor = 0 => height of subtree rooted at q
has decreased by 1.• New balance factor = 2 or –2 => tree is unbalanced at q.
q
![Page 26: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/26.jpg)
Imbalance Classification• Let A be the nearest ancestor of the deleted
node whose balance factor has become 2 or –2 following a deletion.
• Deletion from left subtree of A => type L.• Deletion from right subtree of A => type R.• Type R => new bf(A) = 2.• So, old bf(A) = 1.• So, A has a left child B.
bf(B) = 0 => R0. bf(B) = 1 => R1. bf(B) = –1 => R-1.
![Page 27: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/27.jpg)
R0 Rotation
• Subtree height is unchanged.• No further adjustments to be done.• Similar to LL rotation.
Before deletion.
1
0
A
B
BL BR
AR
h h
h
B
A
After rotation.
BR
hA’R
h-1
BL
h1
-1A
B
BL BR
A’R
After deletion.
h h
h-10
2
![Page 28: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/28.jpg)
R1 Rotation
• Subtree height is reduced by 1.• Must continue on path to root.• Similar to LL and R0 rotations.
Before deletion.
1
1
A
B
BL BR
AR
h h-1
h
B
A
After rotation.
BR
h-1A’R
h-1
BL
h0
0A
B
BL BR
A’R
After deletion.
h h-1
h-11
2
![Page 29: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/29.jpg)
R-1 Rotation
• New balance factor of A and B depends on b.• Subtree height is reduced by 1.• Must continue on path to root.• Similar to LR.
1
-1
A
B
BL
CR
AR
h-1
h
b
CL
C
A
B
BL
CR
A’R
h-1
h-1
CL
Cb
-1
2 C
A
CR A’R
h-1
B
BL
h-1CL
0
![Page 30: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/30.jpg)
Number Of Rebalancing Rotations
• At most 1 for an insert.
• O(log n) for a delete.
![Page 31: Dynamic Dictionaries Primary Operations: Get(key) => search Insert(key, element) => insert Delete(key) => delete Additional operations: Ascend()](https://reader033.fdocuments.us/reader033/viewer/2022051215/56649d4a5503460f94a26f58/html5/thumbnails/31.jpg)
Rotation Frequency
• Insert random numbers. No rotation … 53.4% (approx). LL/RR … 23.3% (approx). LR/RL … 23.2% (approx).