Leftist Heaps and Skew Heaps
Leftist Heaps
-
Target: Speed up merging in
-
Heap: Structure Property + Order Property
-
Leftist Heap
- Order Property – the same
- Structure Property – binary tree, but unbalanced
[Definition] The null path length, Npl(X), of any node X is the length of the shortest path from X to a node without two children.
[Definition] The leftist heap property is that for every node X in the heap, the null path length of the left child is at least as large as that of the right child.
- The tree is biased to get deep toward the left.
[Theorem] A leftist tree with nodes on the right path must have at least nodes.
Note: The leftist tree of nodes has a right path containing at most nodes.
- We can perform all the work on the right path, which is guaranteed to be short
- Trouble makers: Insert and Merge may destroy leftist heap property
Note: Insertion is merely a special case of merging.
Declaration
1 | struct TreeNode |
Merge (recursive version)
-
Step 1: Merge( H1->Right, H2 )
-
Step 2: Attach( H2, H1->Right )
-
Step 3: Swap(H1->Right, H1->Left ) if necessary
1 | PriorityQueue Merge( PriorityQueue H1, PriorityQueue H2 ) |
1 | static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) |
Merge(iterative version)
-
Step 1: Sort the right paths without changing their left children
-
Step 2: Swap children if necessary
Insert
1 | PriorityQueue Insert1(ElementType X, PriorityQueue H) |
DeleteMin
- Step 1: Delete the root
- Step 2: Merge the two subtrees
1 | PriorityQueue DeleteMin1(PriorityQueue H) |
Skew Heaps
- a simple version of the leftist heaps
- 斜堆的右路径在任何时刻都可以任意长,所有操作的最坏情形运行时间均为
- Target: Any consecutive operations take at most time.
Merge
-
Always swap the left and right children except that the largest of all the nodes on the right paths does not have its children swapped.
-
No Npl
Note: Not really a special case, but a natural stop in the recursions.
-
iterative version
Note:
- Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.
- It is an open problem to determine precisely the expected right path length of both leftist and skew heaps.
Amortized Analysis for Skew Heaps
- the root of the resulting tree
- the number of heavy nodes
[Definition] A node is heavy if the number of descendants of ’s right subtree is at least half of the number of descendants of , and light otherwise.
Note: The number of descendants of a node includes the node itself.
- The only nodes whose heavy/light status can change are nodes that are initially on the right path.
- Before merge:
- After merge:
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 OE.Heart's Blog!