Partial preview of the text
Download Initializing a Max Heap and Leftist Tree and more Study notes Data Structures and Algorithms in PDF only on Docsity!
Initializing A Max Heap
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Initializing A Max Heap
Start at rightmost array position that has a child.
Index is n/2.
Initializing A Max Heap
Move to next lower array position.
Initializing A Max Heap
Initializing A Max Heap
Initializing A Max Heap
Initializing A Max Heap
Find a home for 2.
Initializing A Max Heap
Find a home for 2.
Initializing A Max Heap
Find home for 1.
Initializing A Max Heap
Find home for 1.
Initializing A Max Heap
Find home for 1.
Initializing A Max Heap
Done.
Leftist Trees
Linked binary tree.
Can do everything a heap can do and in thesame asymptotic complexity.
Can meld two leftist tree priority queues in O(log n) time.
Extended Binary Trees
Start with any binary tree and add anexternal node wherever there is an
empty subtree.
Result is an extended binary tree.
A Binary Tree
An Extended Binary Tree
number of external nodes is n+
s() Values Example
Properties Of s()
If x is an external node, then s(x) = 0.
Otherwise,
s(x) = min {s(leftChild(x)),s(rightChild(x))} + 1
Height Biased Leftist Trees
A binary tree is a (height biased) leftist treeiff for every internal node x,
s(leftChild(x)) >= s(rightChild(x))
A Leftist Tree
Leftist Trees—Property 2
The number of internal nodes is at least 2 s(root) - 1
Because levelsexternal nodes. 1 through s(root) have no
So, s(root) <= log(n+1)
A Leftist Tree
Levels 1 and 2 have no external nodes.
Leftist Trees—Property 3
Length of rightmost path is n is the number of nodes in a leftist tree. O(log n), where
Follows from Properties 1 and 2.
Leftist Trees As Priority Queues
Min leftist tree … leftist tree that is a min tree.
Used as a min priority queue.
Max leftist tree … leftist tree that is a max tree.
Used as a max priority queue.
Put Operation
put(7)
Put Operation
put(7)
Create a single node min leftist tree. 7
Put Operation
put(7)
Create a single node min leftist tree.
Meld the two min leftist trees.
Remove Min