Leftist tree
In computer science, a leftist tree or leftist heap is a priority queue implemented with a variant of a binary heap. Every node x has an s-value which is the distance to the nearest leaf in subtree rooted at x. In contrast to a binary heap, a leftist tree attempts to be very unbalanced. In addition to the heap property, leftist trees are maintained so the right descendant of each node has the lower s-value. The height-biased leftist tree was invented by Clark Allan Crane. The name comes from the fact that the left subtree is usually taller than the right subtree. A leftist tree is a mergeable heap. When inserting a new node into a tree, a new one-node tree is created and merged into the... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In computer science, a leftist tree or leftist heap is a priority queue implemented with a variant of a binary heap. Every node x has an s-value which is the distance to the nearest leaf in subtree rooted at x. In contrast to a binary heap, a leftist tree attempts to be very unbalanced. In addition to the heap property, leftist trees are maintained so the right descendant of each node has the lower s-value. The height-biased leftist tree was invented by Clark Allan Crane. The name comes from the fact that the left subtree is usually taller than the right subtree. A leftist tree is a mergeable heap. When inserting a new node into a tree, a new one-node tree is created and merged into the existing tree. To delete an item, it is replaced by the merge of its left and right sub-trees. Both these operations take O(log n) time. For insertions, this is slower than Fibonacci heaps, which support insertion in O(1) (constant) amortized time, and O(log n) worst-case. Leftist trees are advantageous because of their ability to merge quickly, compared to binary heaps which take Θ(n). In almost all cases, the merging of skew heaps has better performance. However merging leftist heaps has worst-case O(log n) complexity while merging skew heaps has only amortized O(log n) complexity. ==Bias== The usual leftist tree is a height-biased leftist tree. However, other biases can exist, such as in the weight-biased leftist tree. The exact worst-case complexity of both types of leftist trees is 2 log 2 n, counting comparisons. The exact amortized complexity of weight-biased leftist trees is known to match the log φ n (approximately 1.44 log 2 n) exact amortized complexity of skew heaps, where φ denotes the golden ratio; similarly, the amortized complexity of height-biased leftist trees is bounded below by log φ n, but whether this is also the upper bound is an open problem. == Operations on a height biased leftist tree== Most operations on a Height Biased Leftist Tree are done using the merge operation. if A = null return B if B = null return A if A.key > B.key return MERGE(B, A) A.right := MERGE (A.right, B) // the result cannot be null since B is non-null if A.left = null then SWAP(A.left, A.right) A.s_value := 1 // since the right subtree is null, the shortest path to a descendant leaf from node A is 1 return A if A.right.s_value > A.left.s_value then SWAP(A.right, A.left) A.s_value := A.right.s_value + 1 return A public Node merge(Node a, Node b) { if (a == null) return b; if (b == null) return a; // Reverse the sense of this comparison for a max-heap if (a.element.compareTo(b.element) > 0) { Node temp = a; a = b; b = temp; } if (a.leftChild == null) { assert a.rightChild == null; // By the leftist property // neither child exists, so trivial merge into left child. a.leftChild = b; assert a.s == 1; } else { Node temp = merge(a.rightChild, b); if (a.leftChild.s data LHeap a rank (Node _ _ r) = rank r + 1 merge :: Ord a => LHeap a -> LHeap a -> LHeap a merge Leaf h = h merge h Leaf = h merge h@(Node a l r) h'@(Node a' _ _) | a > a' = merge h' h | rank r' > rank l = Node a r' l | otherwise = Node a l r' where r' = merge r h' An example of how the merge operation in a leftist tree works is depicted. The boxes represent each merge call. File:HBLT 1.jpg File:HBLT 2.jpg File:HBLT 3.jpg File:HBLT 4.jpg File:HBLT 5.jpg File:HBLT 6.jpg File:HBLT 7.jpg File:HBLT 8.jpg File:HBLT 9.jpg When the recursion unwinds, we swap left and right children if x.right.s_value > x.left.s_value for every node x. In this case we swapped the subtrees rooted at nodes with keys 7 and 10. B := CREATE_TREE(x) return MERGE(A, B) x := A.key A := MERGE (A.right, A.left) return x In part 2 a slightly more complex merge happens. The tree with the lower value (tree x) has a right child, so merge must be called again on the subtree rooted by tree x's right child and the other tree. After the merge with the subtree, the resulting tree is put back into tree x. The s-value of the right child (s=2) is now greater than the s-value of the left child (s=1), so they must be swapped. The s-value of the root node 4 is also now 2. Part 3 is the most complex. Here, we recursively call merge twice (each time with the right child 's subtree that is not grayed out). This uses the same process described for part 2. == Weight biased leftist tree== Leftist trees can also be weight biased. In this case, instead of storing s-values in node x, we store an attribute w(x), the weight of x, denoting the number of nodes in the subtree rooted at : w(x) = w(x.right) + w(x.left) + 1 WBLTs ensure w(x.left) ≥ w(x.right) for all internal nodes x. WBLT operations ensure this invariant by swapping the children of a node when the right subtree outgrows the left one, just as in HBLT operations. A closely related concept, the maxiphobic heap, is just an arbitrary binary tree, where each node tracks its own weight as above, but the merge operation merges the two subtrees with the smallest weights among the three. As such, it differs from WBLT by recomputing the smaller subtree on demand instead of maintaining the w(x.left) ≥ w(x.right) invariant, and allowing recursively merging two children previously had the same parent in the recursive step. The merge operation in a Min WBLT is depicted in the graph below. File:HBLT 1.jpg File:WBLT 2.jpg File:WBLT 3.jpg File:WBLT 4.jpg File:WBLT 5.jpg File:WBLT 6.jpg File:WBLT 7.jpg File:WBLT 8.jpg File:WBLT 9.jpg File:WBLT 10.jpg ==Further reading==
Cite this
"Leftist tree". 1789359829. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1374815935 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy