The rank of the heap, the winner tree, the Loser tree, which is superior or inferior?

Source: Internet
Author: User

In a sequential storage structure, heap ordering is a very good advanced sorting algorithm, and the time complexity can be controlled at O (n * logn) in both normal and worst-case scenarios.

Heap sequencing can be used in sequential storage structures because of a unique nature of the complete binary tree. And here you have to mention a full two fork tree.

What do you call a two-prong tree? Full two fork tree is such a binary tree, each of its layers are "full", set the root for the No. 0 layer, then each layer has 2^n nodes. The degrees of all nodes are either 2 or 0 (leaves).

What about the complete two-pronged tree? We first make the following provisions, that is, the nodes in the two-fork tree, from the root to the leaves, each layer from left to right increment number; If a tree has all its nodes numbered with a number one by one corresponding to a two-tree node with the same number of layers, the tree is called a complete binary tree.

Obviously, the complete binary tree does not count the bottom layer, must be a full two fork tree ———— and the bottom layer must be from left to right continuous without gaps.

Because the number of the complete binary tree and the full two-tree is one by one corresponding, and the number of nodes in each layer is very clear, because we can be a complete binary tree (no doubt the full binary tree is necessarily a complete two-pronged trees) stored as sequential storage structure. This is because an arbitrary node can theoretically calculate the offset of its parent and its children after it knows its offset.

Then let's calculate it.

Has a node, which is located on the nth layer (from root to leaf, root layer number 0), K (left to right, leftmost numbered 0), so N and K can also be understood as the number of node layers/nodes before it. Set its offset function to P (n,k)

Then we know that there are n layers, each layer I is full, there are 2^i nodes.

P (n,k) =20+21+...+2^ (n-1) +k

Equal to the sequence ah, easy to get, P (n,k) =2^n+k-1.

Well, then the number of the child of the node, the number of layers must be n+1, the node before the nodes have two children, so the child's line number must be 2 * k and 2 * k + 1.
i.e. P (n+1, 2 * k) and P (n+1, 2 * k + 1)

P(n+1, 2 * k)=2^(n+1)+2*k-1=2 * (2^n+k-1)+1=2 * P(n,k)+1P(n+1, 2 * k + 1)=2^(n+1)+2*k=2 * (2^n+k-1)+2=2 * P(n,k)+2

So we know a node, if its offset is D, its child nodes have an offset of 2 * d + 1 and 2 * d + 2.

In turn, it is easy to know that the offset of its parent node is (d-1)/2.

As mentioned above, this means that we can use sequential storage to represent a complete binary tree, and it is convenient to calculate the parent and child nodes of any node.

On this basis, we can sort the heap. Heap sorting is very simple, is the first to a single element heap continuously append leaf nodes, while adjusting to maintain the heap characteristics, until all nodes (in order to store the elements are all) added to the heap, according to the nature of the heap at the root of the heap must be the extremum, and then the Extremum and the end of the leaf node exchange, discard the leaf and adjust the heap from the root to maintain the characteristics, and then repeat the process until the heap becomes a single-element heap, ending.

Why would I want to raise a heap? We can see that the second half of the heap ordering algorithm is a very clear process of "selecting extrema in multiple values, removing the extremum, updating the root, and then repairing the heap".

This process can be applied to the multi-merge sort, but the heap sort throws the end node, and the multi-path merge does not discard the end node (that is, the length of the heap is unchanged).

But here's the problem-when you fix a heap, each layer adjusts to a minimum of two comparisons of three values (parent node, two child nodes).

So in order to reduce the consumption of comparisons, people develop a tree of winners-the winner Tree lets all the values that participate in the ordered sequence as leaves, and then on top of which the node becomes a complete binary tree, and the parent node holds the winner when the two child nodes are compared.

This allows each layer to be compared only once (compared to the sibling).

However, some people are not satisfied, and then made the loser tree, the loser tree and the winner tree structure is basically the same, but the parent node is not the winner but the loser, and an additional pointer to save the current winner (after the correction is the final winner).

Where is the loser tree better than the winner tree?

The winner tree, from the leaves to the root of the process, each layer will be compared with the brothers, and then the winner 付赋 to the parent node.

And the loser tree, from the leaves to the root of the process, each layer only and the parent node to compare (the leaf before the renewal can be won, all its often wins-so its parents at all levels of the parent node must be the competition and the failure of the Brother node), and then the loser to the parent node, the winner continues to go upward.

In fact, the gap is not too large, saving a single address.

In general, if there are n pending sequences, construct the loser tree or the victor tree, the size of the tree is 2 * n-1. The loser tree, the victor tree and the complete binary tree are all adjusted first, and should be adjusted from the back to the front. be interested in calculating yourself.

The rank of the heap, the winner tree, the Loser tree, which is superior or inferior?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.