Leftist tree

Leftist tree

A leftist tree or leftist heap is a priority queue implemented with a variant of a binary heap. Every node has an "s-value" which is the distance to the nearest leaf. 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 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.

When inserting a new node into a tree, a new one-node tree is created and merged into the existing tree. To delete a minimum item, we remove the root and the left and right sub-trees are then melded. Both these operations take O(log "n") time. For insertions, this is slower than binary heaps which support insertion in amortized constant time, O(1) 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, skew heaps have better performance.

Bias

The usual leftist tree is a "height-biased" leftist tree. However, other biases can exist, such as in the "weight-biased" leftist tree.

S-value

The s-value of a node is the distance from that node to the nearest leaf. In the diagram, the s-values are shown as calculated by including both the starting node and the leaf in the s-value. Thus the s-value of the top node (with value 4) is 2 because there are 2 nodes on the way to the leaf (the starting node, and the leaf itself). For the other three nodes, the s-value is 1 because those nodes are leaves themselves.

Merging height biased leftist trees

Merging two nodes together depends on whether the tree is a min or max height biased leftist tree. For a min height biased leftist tree, set the higher valued node as its right child of the lower valued node. If the lower valued node already has a right child, then merge the higher valued node with the sub-tree rooted by the right child of the lower valued node.

After merging, the s-value of the lower valued node must be updated (see above section, s-value). Now check if the lower valued node has a left child. If it does not, then move the right child to the left. If it does have a left child, then the child with the highest s-value should go on the left.

Java code for merging a min height biased leftist tree

public Node merge(Node x, Node y) { if(x = null) return y; if(y = null) return x;

// if this was a max height biased leftist tree, then the // next line would be: if(x.element < y.element) if(x.element.compareTo(y.element) > 0) { // x.element > y.element Node temp = x; x = y; y = temp; }

x.rightChild = merge(x.rightChild, y);

if(x.leftChild = null) { // left child doesn't exist, so move right child to the left side x.leftChild = x.rightChild; x.rightChild = null; x.s = 1; } else { // left child does exist, so compare s-values if(x.leftChild.s < x.rightChild.s) { Node temp = x.leftChild; x.leftChild = x.rightChild; x.rightChild = temp; } // since we know the right child has the lower s-value, we can just // add one to its s-value x.s = x.rightChild.s + 1; } return x;}

Initializing a height biased leftist tree

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.


External links

* [http://www.cise.ufl.edu/~sahni/cop5536/slides/lec114.pdf Leftist Trees] at Dr. Sartaj Sahni's website (Department Chair in CISE at University of Florida)
* [http://niketanpansare.com/MinLeftistTree.aspx Java implementation of leftist tree]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • The Olive Tree (political coalition) — The Olive Tree Leader Romano Prodi (1995 1998) Massimo D Alema (1998 2000) Francesco Rutelli (2000 2004) Roman …   Wikipedia

  • List of terms relating to algorithms and data structures — The [http://www.nist.gov/dads/ NIST Dictionary of Algorithms and Data Structures] is a reference work maintained by the U.S. National Institute of Standards and Technology. It defines a large number of terms relating to algorithms and data… …   Wikipedia

  • Список терминов, относящихся к алгоритмам и структурам данных —   Это служебный список статей, созданный для координации работ по развитию темы.   Данное предупреждение не устанавливается на информационные списки и глоссарии …   Википедия

  • Список терминов — Список терминов, относящихся к алгоритмам и структурам данных   Это сл …   Википедия

  • Linksbaum — Ein Linksbaum oder Linksheap ist eine Vorrangwarteschlange, die durch eine Variante eines Binärbaumes verwirklicht ist. Diese Datenstruktur ist eine Erfindung von Clark Allan Crane und nutzt eine Heapstruktur. Linksbäume sollen, im Gegensatz zu… …   Deutsch Wikipedia

  • WBLT — Watson Barker Listening Test (Community » Educational) *** Weight Biased Leftist Tree (Academic & Science » Mathematics) * AM 1350, Christiansburg, Virginia (Community » Radio Stations) …   Abbreviations dictionary

  • literature — /lit euhr euh cheuhr, choor , li treuh /, n. 1. writings in which expression and form, in connection with ideas of permanent and universal interest, are characteristic or essential features, as poetry, novels, history, biography, and essays. 2.… …   Universalium

  • Hugo Chávez — This name uses Spanish naming customs; the first or paternal family name is Chávez and the second or maternal family name is Frías. Hugo Chávez …   Wikipedia

  • Finland — /fin leuhnd/, n. 1. Finnish, Suomi. a republic in N Europe: formerly a province of the Russian Empire. 5,109,148; 130,119 sq. mi. (337,010 sq. km). Cap.: Helsinki. 2. Gulf of, an arm of the Baltic, S of Finland. * * * Finland Introduction Finland …   Universalium

  • El Salvador — /el sal veuh dawr /; Sp. /el sahl vah dhawrdd / a republic in NW Central America. 5,661,827; 13,176 sq. mi. (34,125 sq. km). Cap.: San Salvador. Also called Salvador. * * * El Salvador Introduction El Salvador Background: El Salvador achieved… …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”