Leftist Heap & Skew Heap¶
约 177 个字 20 行代码 预计阅读时间 1 分钟
Idea: faster merge¶
Leftist Heap¶
\(O(logN)\)
-
Keep the length right path low.(logN)
Balanced tree can do this, but too more constraint.
A more relaxed constraint: \(NPL\) (NULL path length)
Shortest path to a node without 2 child.
\(NPL(single node) = 0\)
\(NPL(NULL) = -1\)
\(NPL(x) = min_{c \in child(x)}\{NPL(c) + 1\}\)
-
Maintain NPL
Swap childs.
-
Merge Operation
Heap A, B
-
Time complexity
\(O(logN)\)
-
Codes
Don't forget to maintain NPL.
PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ) { if ( H1 == NULL ) return H2; if ( H2 == NULL ) return H1; if ( H1->Element < H2->Element ) return Merge1( H1, H2 ); else return Merge1( H2, H1 ); } static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) { if ( H1->Left == NULL ) /* single node */ H1->Left = H2; /* H1->Right is already NULL and H1->Npl is already 0 */ else { H1->Right = Merge( H1->Right, H2 ); /* Recursive Merge */ if ( H1->Left->Npl < H1->Right->Npl ) /* Maintain subtree */ SwapChildren( H1 ); H1->Npl = H1->Right->Npl + 1; /* Update NPL value*/ } /* end else */ return H1; }
Skew Heap¶
-
No maintaining of NPL anymore.
Length of right path is ONLY guaranteed under amortized analysis.
-
Merge Operation
- Sort right path of A B, let it to be the new left path.
- Attach left-sub tree to the corresponding right child. (except the largest on new left path)