词条 | 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 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 height-biased leftist tree was invented by Clark Allan Crane.[1] 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. BiasThe usual leftist tree is a height-biased leftist tree.[1] However, other biases can exist, such as in the weight-biased leftist tree.[2] S-valueThe s-value (or rank) of a node is the distance from that node to the nearest missing leaf. Put another way, the s-value of a Merging height biased leftist treesAssuming a min-heap, choose the lower-valued node as the root, and merge the higher-valued node into the new root's right subtree. After merging, compare the s-values (heights) of the resultant children. Swap them, if necessary, so the left child's s-value is at least as large as the right child's. (If one child is missing, it should be the right one.) Then update the s-value of the root to be one more than its right child's. Java code for merging a min height biased leftist treeVariantsSeveral variations on the basic leftist tree exist, which make only minor changes top the basic algorithm:
Initializing a height biased leftist treeInitializing 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. {{clear}}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. {{clear}}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. {{clear}}References1. ^1 {{citation|first=Clark A. |last=Crane|title=Linear Lists and Priority Queues as Balanced Binary Trees|year=1972|publisher=Department of Computer Science, Stanford University|type=Ph.D. thesis|isbn=0-8240-4407-X|id=[https://searchworks.stanford.edu/view/4591631 STAN-CS-72-259]}} 2. ^{{citation|author1=Seonghun Cho |author2=Sartaj Sahni|title=Weight Biased Leftist Trees and Modified Skip Lists|journal=Journal of Experimental Algorithmics|year=1996|volume=3|url=http://www.cise.ufl.edu/~sahni/papers/wblt.pdf|citeseerx=10.1.1.13.2962|doi=10.1145/297096.297111}} External links
Further reading
|author1=Dinesh P. Mehta |author2=Sartaj Sahni |title=Handbook of Data Structures and Applications |chapter=Chapter 5: Leftist trees |chapter-url=https://books.google.com/books?id=fQVZy1zcpJkC&pg=SA5-PA1&lpg=SA5-PA1 |date=28 October 2004 |publisher=CRC Press |isbn=978-1-4200-3517-9}} 3 : Trees (data structures)|Heaps (data structures)|Priority queues |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。