P. 1
Left

Left

|Views: 83|Likes:
Published by Renjith

More info:

Published by: Renjith on Oct 10, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

10/10/2010

pdf

text

original

Leftist tree

Wikipedia:

Leftist tree
Sponsored Links The Economist Magazine Get a world view every week. 12 Issues for Rs. 720 only! EconomistSubscriptions.com Extinction For Socialism? New Book Shows Why Socialism Will Fall To Natural Selection www.LivingWithEvolution.com Home > Library > Miscellaneous > Wikipedia 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 merged. 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. Contents [hide]
• • • • •

1 Bias 2 S-value 3 Merging height biased leftist trees

3.1 Java code for merging a min height biased leftist tree

4 Initializing a height biased leftist tree 5 External links

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

if(y == null) return x. Node y) { if(x == null) return y.element Node temp = x. the s-value of the lower valued node must be updated (see above section. svalue).element.S-values of a leftist tree The s-value of a node is the distance from that node to the nearest leaf of the extended binary representation of the tree. In the diagram. then the child with the highest s-value should go on the left. x. The s-value of 5 is 1 since its extended representation would have one leaf itself. y).element > y.rightChild = merge(x. } x. Java code for merging a min height biased leftist tree public Node merge(Node x. x. For a min height biased leftist tree. // if this was a max height biased leftist tree.element) if(x.rightChild = null. if(x. After merging. then the // next line would be: if(x. Merging height biased leftist trees Merging two nodes together depends on whether the tree is a min or max height biased leftist tree.leftChild == null) { // left child doesn't exist. If it does not. the minimum distance to these leaves are marked in the diagram. Now check if the lower valued node has a left child. If it does have a left child.rightChild. then merge the higher valued node with the sub-tree rooted by the right child of the lower valued node. set the higher valued node as its right child of the lower valued node. y = temp. Thus s-value of 4 is 2.element < y. x = y. since the closest leaf is that of 8 --if 8 were extended.rightChild. } else { . so move right child to the left side x.s = 1. If the lower valued node already has a right child.element) > 0) { // x. then move the right child to the left.compareTo(y. the extended representation (not shown) fills in the tree to make it a complete binary tree (adding five leaves).leftChild = x.

and placed back into the queue. Each line of the diagram represents another cycle of the algorithm.leftChild. To initialize a min HBLT. place each element to be added to the tree into a queue. } // since we know the right child has the lower s-value. The first is to merge each node one at a time into one HBLT.s = x. 9.rightChild. In the fifth step.rightChild. the set of numbers [4. This can initialize a HBLT in O(n) time. x. 3.rightChild.s + 1.leftChild = x. Notice that the freshly created HBLT is added to the end of the queue. so compare s-values if(x. The sixth step shows two trees merged with each other. Initializing a height biased leftist tree Initializing a min HBLT . merged.Part 1 Initializing a height biased leftist tree is primarily done in one of two ways. with predictable results. This approach is detailed in the three diagrams supplied. In the example (see Part 1 to the left). 1. x. we can just // add one to its s-value x. 5. This process is inefficient and takes O(nlogn) time. the first occurrence of an s-value greater than 1 occurs. The other approach is to use a queue to store each node and resulting tree. // left child does exist. 6. 10.s < x.leftChild. 11] are initialized. 8. The first five steps are easy to follow. depicting the contents of the queue. .s) { Node temp = x. The first two items in the queue are removed.rightChild = temp.} } return x. A min height biased leftist tree is shown.

so they must be swapped. the resulting tree is put back into tree x. This uses the same process described for part 2. Sartaj Sahni's website (Department Chair in CISE at University of Florida) Java implementation of leftist tree This entry is from Wikipedia. The s-value of the root node 4 is also now 2. It may not have been reviewed by professional editors (see full disclaimer) Donate to Wikimedia . so merge must be called again on the subtree rooted by tree x's right child and the other tree.Initializing a min HBLT . The tree with the lower value (tree x) has a right child. the leading user-contributed encyclopedia. External links • • Leftist Trees at Dr. we recursively call merge twice (each time with the right child 's subtree that is not grayed out). Initializing a min HBLT .Part 3 Part 3 is the most complex.Part 2 In part 2 a slightly more complex merge happens. After the merge with the subtree. Here. The s-value of the right child (s=2) is now greater than the s-value of the left child (s=1).

The insertion and deletion of elements in the tree come from merging two skew heaps together in such a way that the heap property is preserved and the tree does not degrade to a linear tree.answ e http://w w w ... Top of Form new ansGo • Bottom of Form Browse: Unanswered questions | Most-recent questions | Reference library Top of Form Enter question or phrase. • • How do you create a sorted list from a skew heap? The same way you did from a heap. How do you create a skew heap from a list of random data? .answ Sponsored Links CS 510: Computer Algorithms Skew Heaps • What is a skew heap? A heap data structure that is stored in a binary tree (not necessarily complete and balanced)...English▼ English▼ Deutsch Español Français Italiano Tagalog • • Search unanswered questions. Search: L e f t is t tr e e All sources Community Q&A Reference topics • 1 Browse: Unanswered questions | New questions | New answers | Reference library 0 WA NotLgd Bottom of Form http://w iki.

See example below. and 7 with a heap containing the elements 4 and 6. 5 2 4 Step One: Identify the root. • So.merge -> 6 / 5 <.merge -> 2 7 / \ 4 5 / \ 3 Result: . Inserting an Element into a Skew Heap: Step One: 8 9 / \ 6 <. We then take the left subtree of this element and make it the right subtree of that element in the new tree.merge -> 2 Conclusion: At each step. how do you sort a list of random data using a heap? Example of Merging 2 Skew Heaps Suppose we are merging a heap containing the elements 2.merge -> 6 (to form the left subtree \ / of the new skew heap) 5 4 Step Two: 6 7 / \ \ Step Three: 7 / \ 6 / \ 2 4 5 4 7 / \ <. we take we find the largest element of the two heaps and place that into the skew heap as the left child of the new skew heap. thus 7 becomes the new root and the left subtree of the heap with root 7 becomes the right subtree of the other heap: 7 2 <. Then we remove the element and its left subtree from the original 2 trees and repeat. 5.

merge -> 2 Step Three: Result: 9 / \ 7 8 / \ 5 6 \ 4 3 Step Four: Result: 9 / \ 7 8 / \ 5 6 / \ 3 4 <.merge -> 2 7 / \ 5 / \ 3 Originals: 9 / \ 7 8 \ 6 5 / \ 4 3 <.9 Originals: 6 4 Step Two: Result: \ 8 <.merge -> 1 / .merge -> 2 Originals: / 2 Now we can insert 1: Beginning: 9 / \ 7 8 / \ 5 6 / \ 3 4 <.

merge -> 1 / \ 8 7 / \ 5 6 / \ 3 4 / 2 Next insert 1: 9 / \ 8 7 / / \ 1 5 6 / \ 3 4 / 2 Conclusion: A badly unbalanced tree can affect the performance of one operation. . but it can be shown that subsequent insertions and deletions must as a consequence be very rapid.2 First make the 7 subtree the right child of the root: 9 <. In fact. each operation is no worse than O(log n). amortized over time.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->