Merging Priority Queues: left-side tree and oblique heap

Source: Internet
Author: User

Sometimes the priority queue needs to be merged. For a binary heap, the heap can only be created with the complexity of O (n) because it has nothing except the key value and the heap sequence.

To reduce the complexity of merging to O (logn), you can use oblique heap (left-side tree), two-item heap, and other data structures. The Fibonacci heap is even better. Many operations are the theoretical complexity of O (1), but the complexity of thinking and programming ......

I learned a little about the two items, but I did not know the nature of the items. If you have time, try again. Forget the Fibonacci heap ......

The nature of the left-side tree is that the zero path length of the left-side tree is not smaller than that of the right-side tree, which leads to a severe left-side deviation of the tree. Merge the two trees to compare the size of the top elements of the heap. Then, combine the right subtree of a tree and the other tree recursively. If the zero path length of the right subtree of the merged tree is smaller than that of the left subtree, the left and right subtree is exchanged. Insert can be considered to merge the left tree with the other one with only one element. If it is deleted, merge the left and right subtree. Because the tree conforms to the heap sequence, the operation to obtain the minimum value is the same.

The oblique heap is the self-adjusting form of the left-side tree. To put it bluntly, it is a simplified version. We do not need to maintain a long path. After each merge, the left and right sub-trees are exchanged, so that the operation time of the shared operation remains unchanged. The idea is similar to splay and aVL. The specific implementation is as few lines as possibleCodeThat's all.

Hdu1512 is used to search for Dad.

I am reading the http://www.cnblogs.com/lxf90/archive/2011/04/07/2008180.html written here.

Below is a link http://oi.imzzl.com/2010/07/610.html for this question and the left Tree PPT

Code: (It's a diagonal heap. Remove it. // It's the left Tree)

 

# Include <stdio. h> # Define Maxn 100000Struct Node { Int Dis, L, R, strong;} tree [maxn]; Int I, J, K, M, N; Int Father [maxn]; Int X, Y, FX, FY; Void Swap ( Int * X, Int * Y ){ Int Temp; temp = * X; * x = * Y; * Y = temp ;} Int Find ( Int X ){ If (X! = Father [x]) Father [x] = find (father [x]); Return Father [X];}Int Merge ( Int X, Int Y ){ If (X = 0) Return Y; If (Y = 0) Return X; If (Tree [X]. strong <tree [Y]. strong) Swap (& X, & Y); tree [X]. R = Merge (tree [X]. r, y ); Int L = tree [X]. L; Int R = tree [X]. R; father [R] = X; // If (tree [l]. Dis <tree [R]. Dis) Swap (& tree [X]. l, & tree [X]. R );// If (tree [X]. r = 0)     // Tree [X]. Dis = 0;     // Else     // Tree [X]. Dis = tree [R]. DIS + 1;      Return X ;} Int Del ( Int X ){ Int L, R; L = tree [X]. l; r = tree [X]. r; father [l] = L; father [R] = r; tree [X]. L = tree [X]. R = tree [X]. dis = 0; Return Merge (L, R );} Void Fight ( Int X, Int Y ){ Int Left, right, now; tree [X]. strong/= 2; tree [Y]. strong/= 2; left = del (x); Right = del (y); left = Merge (left, x); Right = Merge (right, y ); now = Merge (left, right); printf ( "% D \ n" , Tree [now]. Strong );} Int Main (){ While (Scanf ( "% D" , & N) = 1 ){ For (I = 1; I <= N; I ++) {scanf ( "% D" , & Tree [I]. strong); tree [I]. L = tree [I]. R = tree [I]. dis = 0; father [I] = I;} scanf ( "% D" , & M ); For (I = 1; I <= m; I ++) {scanf ("% D" , & X, & Y); FX = find (x); FY = find (y ); If (FX = FY) printf ( "-1 \ n" ); Else Fight (FX, FY );}} Return 0 ;}

 

References:

CLRS

Data Structure andAlgorithmAnalysis

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.