weight-biased leftist heaps advanced)
TRANSCRIPT
NUCES-FAST • Advanced Data Structures • Spring 2007
Weight-biased Leftist Heaps
Rabea Aden
NUCES-FAST • Advanced Data Structures • Spring 2007
For any node X Є Weight-biased Leftist Tree, Rightmost path is the Shortest
Weight-biased Leftist Tree
• Invented by Seonghun Cho and Sartaj Sahni
• A Weight-biased Leftist Tree is a priority queue
• An alternative to Height-biased Leftist Trees
• It satisfies the leftist structural property
NUCES-FAST • Advanced Data Structures • Spring 2007
ld(X))w(rightChi d(X))w(leftChil 1 w(X)
Weight
• denoted by w(X)• Number of internal nodes in subtree with root X
w(NULL) = 0
NUCES-FAST • Advanced Data Structures • Spring 2007
Weight
w(X) = 1 + w( leftChild(X) ), w( rightChild(X) )
1
2 1
4
1
2
3
8
w(NULL) = 0
0 0
0 0 0
0 0
0
0
Null
Null
Null
NullNullNull
Null Null Null
NUCES-FAST • Advanced Data Structures • Spring 2007
Structural Property
• Weight-biased Leftist Property
• Tree is unbalanced • Biased towards left• Left paths are long, giving the name Leftist Heap• Rightmost path is the shortest
ld(X))w(RightChi d(X))w(LeftChil WBLT X
NUCES-FAST • Advanced Data Structures • Spring 2007
Structural Property
Weights: T1 is a Weight-biased Leftist Tree, T2 is not
T1 T2
Leftist Heap Property Violatedw(Left Child) < w(Right Child)
1
2 1 1 1
4 3
8
1 1
2 1 1 2
4 4*
9
1 2
NUCES-FAST • Advanced Data Structures • Spring 2007
Weight-biased Leftist Heap
• A Heap-ordered Weight-biased Leftist Tree
NUCES-FAST • Advanced Data Structures • Spring 2007
Heap-order Property
• Min Leftist Heap
• Max Leftist Heap
(X))key(Parent key(X) Heap X
(X))key(Parent key(X) Heap X
If X is root Parent (X) = root
NUCES-FAST • Advanced Data Structures • Spring 2007
Binary Heap vs. Weight-biased Leftist Heap
Binary Heap Weight-biased Leftist Heap — Perfectly balanced — Very unbalanced
Operations Binary Heap Weight-biased Leftist Heap
BuildHeapFindMinMergeInsertDeleteMin
O(N)O(1)Θ(N)
O(log N)O(log N)
O(N)O(1)
O(log N)O(log N)O(log N)
NUCES-FAST • Advanced Data Structures • Spring 2007
Binary Heap Weight-biased Leftist Heap — Perfectly balanced — Very unbalanced
Merge is expensive in ordinary Heaps
Operations Binary Heap Weight-biased Leftist Heap
BuildHeapFindMinMergeInsertDeleteMin
O(N)O(1)Θ(N)
O(log N)O(log N)
O(N)O(1)
O(log N)O(log N)O(log N)
Binary Heap vs. Weight-biased Leftist Heap
NUCES-FAST • Advanced Data Structures • Spring 2007
Theorem
In a weight-biased leftist tree, the number of nodes N on the rightmost path of any node x is
Proof is by Induction
1)(w(x)log2
NUCES-FAST • Advanced Data Structures • Spring 2007
Structure of a Node
DataLsizeRight
PointerRsize
LeftPointer
Apart from data, left and right pointers, each node also contains Lsize and Rsize — the number
of nodes in left and right subtrees, respectively
NUCES-FAST • Advanced Data Structures • Spring 2007
Design
• A Weight-biased Leftist Tree is a priority queue
• A header node points to the root of the weight-biased leftist tree with data = -∞ and left pointer pointing to itself
• A bottom node acts as a common NULL node with data = ∞
NUCES-FAST • Advanced Data Structures • Spring 2007
Structure of Header & Bottom Nodes
Figure Empty min WBLT
-∞+∞ 0
∞
Header Node
Bottom Node
NUCES-FAST • Advanced Data Structures • Spring 2007
Structure of Header & Bottom Nodes
Figure min WBLT
-∞+∞ 4
32 1
Header Node
∞Bottom Node
71 0 50 0
90 0
1
2 1
4
NUCES-FAST • Advanced Data Structures • Spring 2007
Operations
• BuildHeap O(N)
• FindMin O(1)
• Merge O(log N)
• Insert O(log N)
• DeleteMin O(log N)
NUCES-FAST • Advanced Data Structures • Spring 2007
General Idea
• All insertions and merges are performed on the rightmost path containing at most nodes
• This may violate Weight-biased Leftist Heap Property
• Weight-biased Leftist Heap Property can be easily restored
1)(nlog2
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
• O(log N) time
• It is a fundamental operation– Both Insert and DeleteMin are based on Merge– Insert:
• The key to be inserted is a node • Merge this node with the given min leftist heap
– DeleteMin• Delete root, left and right child of root are two min leftist heaps• Merge the two min leftist heaps
NUCES-FAST • Advanced Data Structures • Spring 2007
Insert
Existing min leftist heap — H1The key to be inserted is a node — Y, where data(Y) = keyX = header nodeWhile data( rightChild(X) ) < key
Increment Rsize(X) by 1If Lsize(X) < Rsize(X) then swap children of X and X = leftChild(X)Otherwise X = rightChild(X)
leftChild(Y) = rightChild(X) and rightChild(Y) = bottom nodeLsize(Y) = Rsize(X) and Rsize(Y) = 0If Lsize(X) = Rsize(x) then
rightChild(X) = leftChild(X) leftChild(X) = Y Increment Lsize(X) by 1
Otherwise rightChild(X) = Y Inrement Rsize(X) by 1
NUCES-FAST • Advanced Data Structures • Spring 2007
Header Node
Bottom Node
-∞∞ 9
54 3
112 1 131 1
201 0 190 0 170 0
290 01
230 0
∞
8
4
38 0
3
1112
1
9
38 0
Header Node
Bottom Node
-∞∞ 8
54 3
112 1 131 1
201 0 190 0 170 0
290 01
230 0
∞
8
4
30 0
3
1112
1
Header Node
Bottom Node
-∞∞ 8
54 3
112 1 131 1
201 0 190 0 170 0
290 01
230 0
∞
8
4
30 0
3
1112
1
Insert 3
Header Node -∞∞ 8 30 0
Bottom Node
54 3
112 1 131 1
201 0 190 0 170 0
290 01
230 0
∞
8
4 3
1112
1
9
Header Node -∞∞ 9
X = header nodeSince data(rightChild(X)) '5' > key '3'
Since Lsize(X) ≠ Rsize(X) rightChild(X) = '3'
Increment Rsize(X) by 1 '9'
Lsize('3') = Rsize(X) '8'Rsize('3') = 0
leftChild('3') = rightChild(X) '5'rightChild(Y) = bottom node
NUCES-FAST • Advanced Data Structures • Spring 2007
Time Complexity of Insert
• O(log n)
• Create a one-node heap — O(1)• As only shortest path is traversed — O(log n)
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge AlgorithmMerge(H1, H2)
If both heaps are Leftist
If one heap is NULL then return the other
while H1 ≠ NULL
If Lsize(H1) < Rsize(H1) + w(H2) then work on leftChild(H1)
rightChild(H1) = leftChild(H1), Rsize(H1) = Lsize(H1), Lsize(H1) = Rsize(H1) + w(H2)
If data(H1) > data(H2) then leftChild(H1) = H2, H1 moves to its leftChild and H2 moves to H1's rightChild
Otherwise leftChild(H1) = rightChild(H1) and H1 moves to its rightChild
Otherwise work on rightChild(H1) (Symmetric case)
Move to rightChild(H1)
If Leftist Heap Property is violated i.e
If w(leftChild(H1)) < w( rightChild(H1)) then
swap children and weights of H1, leftChild(H1) = H2 & Lsize(H1) = w(H2)
Otherwise rightChild(H1) = H2 & Rsize = w(H2)
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 3
181 1 91 1
290 0 140 0 120 0250 0
∞
2 2
1111
Merge the two min weight-biased leftist heaps
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 3
181 1 91 1
290 0 140 0 120 0250 0
∞
2 2
1111
Since data(H1) '18' > data(H2) '19'Swap the two
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 7
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Merge the two min weight-biased leftist heaps
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 3
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
H1 '9' now becomes rightChild of Header nodeRsize(Header Node) = '3'
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 3
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
H1 '9' now becomes rightChild of Header nodeRsize(Header Node) = '3'
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 3
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Since data(H2) '18' > data(H1) '9' and w(H2) + Rsize(Header) = 6
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Rsize(Header Node) = '6'
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
1 + 3 = 4
Since Lsize(H1) '1' < Rsize(H1) + w(H2) '4' Leftist Property is violated
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 7
94 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Swap children of H1
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
120 0 290 0 250 0140 0
∞
2 2
1111
Swap children of H1
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 7
94 1 181 1
120 0 290 0 250 0140 0
∞
2 2
1111
Work on left of H1 — Compare data(H1) '12' and data(H2) '18' Since data(H1) < data(H2) — H1 moves to its rightChild 'NULL'
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1111
0 + 3 = 3
Since rightChild(H1) is 'NULL' and attaching H2 to right of H1 violates Leftist Property — Lsize(H1) = 3
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
0 + 3 = 3
w(rightChild(H1)) = '4'
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
w(rightChild(H1)) = '4'
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
H2 becomes leftChild(H1)
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
H2 becomes leftChild(H1)
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1
123 0 140 0
181 1
∞
2
1 1
2
290 0
1 1
250 0
H2 becomes leftChild(H1)
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge
Header Node
Bottom Node
-∞∞ 6
94 1
123 0 140 0
181 1
∞
2
1 1
2
290 01
1 1
250 01
NUCES-FAST • Advanced Data Structures • Spring 2007
Time Complexity of Merge
• O(log n)
• As only shortest path is traversed
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
• O(log N) time
DeleteMin is based on Merge
Delete root — (minimum value is in root) Left and right children of root are two min
leftist heaps — H1 and H2 respectively Merge the two min leftist heaps H1 and H2
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 7
73 3
181 1 91 1
290 0 140 0 120 0250 0
∞
7
3 3
1111
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 7
73 3
181 1 91 1
290 0 140 0 120 0250 0
∞
3
2 2
1111
Delete '7'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 3
181 1 91 1
290 0 140 0 120 0250 0
∞
2 2
1111
Since data(H1) '18' > data(H2) '19'Swap the two
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 3
181 1 91 1
290 0 140 0 120 0250 0
∞
2 2
1111
Merge the two min weight-biased leftist heaps
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 7
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
H1 '9' now becomes rightChild of Header nodeRsize(Header Node) = '3'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 3
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
H1 '9' now becomes rightChild of Header nodeRsize(Header Node) = '3'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 3
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Since data(H2) '18' > data(H1) '9' and w(H2) + Rsize(Header) = 6
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 3
91 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Rsize(Header Node) = '6'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Since Lsize(H1) '1' < Rsize(H1) + w(H2) '4' Leftist Property is violated
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
1 + 3 = 4
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 7
94 1 181 1
140 0 290 0 250 0120 0
∞
2 2
1111
Swap children of H1
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
120 0 290 0 250 0140 0
∞
2 2
1111
Swap children of H1
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 7
94 1 181 1
120 0 290 0 250 0140 0
∞
2 2
1111
Work on left of H1 — Compare data(H1) '12' and data(H2) '18' Since data(H1) < data(H2) — H1 moves to its rightChild 'NULL'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1111
0 + 3 = 3
Since rightChild(H1) is 'NULL' and attaching H2 to right of H1 violates Leftist Property — Lsize(H1) = '3'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
0 + 3 = 3
w(rightChild(H1)) = '4'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
w(rightChild(H1)) = '4'
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
H2 becomes leftChild(H1)
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1 181 1
123 0 290 0 250 0140 0
∞
2 2
1114
H2 becomes leftChild(H1)
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1
123 0 140 0
181 1
∞
2
1 1
2
290 0
1 1
250 0
H2 becomes leftChild(H1)
NUCES-FAST • Advanced Data Structures • Spring 2007
DeleteMin
Header Node
Bottom Node
-∞∞ 6
94 1
123 0 140 0
181 1
∞
2
1 1
2
290 01
1 1
250 01
NUCES-FAST • Advanced Data Structures • Spring 2007
Time Complexity of DeleteMin
• O(log n)
• Delete root — O(1)• Initialize H1 with left child of root — O(1)• Initialize H2 with right child of root — O(1)• Merge — O(log n)
– As only shortest path is traversed
NUCES-FAST • Advanced Data Structures • Spring 2007
Height-Biased vs. Weight-Biased Leftist Heaps
• Insert and Delete • 2 passes:
– top-down & – bottom-up (to adjust
npl)
• Insert and Delete• Single pass
– Top-down