page_title: Leftist tree
section_title: Initializing a height biased leftist tree
hierarchical_section_title: Leftist tree / Operations on a height biased leftist tree / Initializing a height biased leftist tree
caption_attribution_description: English: First steps to initialize a min height biased leftist tree. Each line is the queue after the first two items in the previous line have been merged.
context_page_description: 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) 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 Θ. In almost all cases, the merging of skew heaps has better performance.
context_section_description: Initializing a height biased leftist tree is primarily done in one of two ways.  The first is to merge each node one at a time into one HBLT.  This process is inefficient and takes O(nlogn) time.  The other approach is to use a queue to store each node and resulting tree.  The first two items in the queue are removed, merged, and placed back into the queue.  This can initialize a HBLT in O(n) time.  This approach is detailed in the three diagrams supplied.  A min height biased leftist tree is shown.
  To initialize a min HBLT, place each element to be added to the tree into a queue.  In the example (see Part 1 to the left), the set of numbers [4, 8, 10, 9, 1, 3, 5, 6, 11] are initialized.  Each line of the diagram represents another cycle of the algorithm, depicting the contents of the queue.  The first five steps are easy to follow.  Notice that the freshly created HBLT is added to the end of the queue.  In the fifth step, the first occurrence of an s-value greater than 1 occurs.  The sixth step shows two trees merged with each other, with predictable results.
  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.
caption: Initializing a min HBLT - Part 1
exif: {}