Structural Beauty -- Basic Structure of priority queue (4) -- binary heap, d heap, left heap, and oblique heap

Source: Internet
Author: User

The implementation of the priority queue structure is mainly completed through the heap, mainly including: Binary heap, d heap, left heap, oblique heap, two heap, Fibonacci heap, pairing heap and so on.

 

1. Binary heap 1.1. Definition

Complete Binary Tree, minimum root.

Sequence is used for storage.

 

1.2. Operation (1). insert (filtering)

Insert 26 at the end and compare them upwards. If the value is greater than 26, the switch position is displayed. If the value is smaller than 26, the switch position is stopped.

 

(2). deletemin (downstream filter)

Extract the end element and place it on the top of the heap to continue Filtering:

 

(3) Other operations:

These operations are based on insert and deletemin.

Reduce Element: Reduce the value of the node, and filter and adjust the heap.

Increase Element: Increase the node value and filter down to adjust the heap.

Delete non-vertex nodes: Deletion may cause problems. Method: reduce the element value to an infinitely small value and delete it after filtering.

Merge:Insert one by one

 

2. d cross heap 2.1. Definition

The entire d-tree has the smallest root.

Sequence is used for storage.

 

2.2. Operation:

The operation is basically the same as that of the binary heap: insert, deletemin, increase elements, reduce elements, delete non-top elements, merge.

 

2.3 comparison between the binary heap and the D heap:

 

3. left heap 3.1. defines the length of the zero path: the shortest distance to the node without two sons left heap: 1. one binary tree 2. zero path length: Left son, right son, right son, parent node = min {son} + 1 (this property leads to severe Left deviation of the Left heap), zero path length:

 

3.2. Operation:

(1) Merge:

Principle: The heap with a large root value is merged with the right sub-heap of the heap with a small root value (root value: element value at the root position, with a non-zero path length) there are three cases (the root value of heap H1 is smaller than H2). H1 has only one node, H1 root, and right child. H1 root has right child (1.1 ). h1 has only one node. If the node does not meet the requirements, the path length is zero. The left son and the right son are switched to the left and right children. (1.2). H1 root has no right child. If there is not enough: Zero path length: Left son, right son, right son, swap left and right children.

 

(1.3). H1 root has right child

1. In the initial state, root 6 of H1 and root 8 of H2 are merged into H1.

2. Construct H1 into a root with no right child:

3. to convert element 10, merge to H2, we must first construct H2 into the form of root without right child. recursion: Merge. If there is any problem: Zero path length: Left son; right son, exchange between children ......

--

4.

5.

3.3. Nature Analysis: insert: mergedeletemin: delete root, merge Time Complexity: The sum of merge and the right path length is proportional; worst O (logn) Disadvantage: The switch needs to be judged; maintenance of zero path length

 

4. Oblique heap

 

4.1. Define a binary tree with the smallest root. It can be seen that: Merge unconditional exchange. Time Complexity: Worst O (n); best? (1); average O (logn)

 

4.2 comparison of performance:

Definition
  • There is an oblique heap in the pipeline;
  • The result of the combination of the two oblique heaps is still the oblique heap.
Merge operations

The merge process of the Oblique heap merge operation is exactly the same as that of the left-side merge operation. We need to combine a and B, and the root nodes of a are smaller than those of B, we only need to set the root points of a as the root points of the new oblique heap after the combination, and combine the right sub-points of a with B. Since the sum ends along the rightmost direction, after the sum ends, the rightmost direction growth of the new oblique heap will inevitably increase, this will affect the efficiency of the next merge. Therefore, after the combination, the rightmost route length of the entire tree is very small (this is the development rule) through the intersection of the left and right sub-trees ). However, the oblique heap does not remember the distance between nodes. During the operation, the bottom and top of the stack follow the merged path, at each vertex, the cursor is left and right. The heap throws the rightmost traffic to the left side of the heap.

Verify and verify
  • Set P to a heap with a smaller root value, Q to another heap, and r to the result heap after merging.
  • So that the root of R is P (with the minimum root partition value), and the right sub-partition of R is the left sub-partition of P.
  • The result of making the left sub-operator of R equal to the right sub-operator of P and the Q-combination operator.

Example. Before merging:


After Contract

Non-dynamic merge operation
  • Cut down the rightmost child of each heap. This makes the right sub-region of each tree empty.
  • Sort these partitions in ascending order of root values.
  • Iteration merge contains two trees with the maximum root partition value:
    • The right sub-partition of the token with the next big root token value must be empty. Submit the Left and Right sub-accounts. The left subaccount of the cursor is empty.
    • The token with the maximum root limit value is the left sub-iterator with the maximum root limit value.

Example:

5. Summary

 

If Union operations are not supported, the normal heap data structure is an ideal data structure (heap sorting ). However, if you want to support merge operations on a set, it is best to use two heaps or a Fibonacci heap. The worst case for a normal heap in a union operation is O (n ), but the two heap and the Fibonacci heap are O (lgn ).

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.