The beauty of structure--three major structures of priority Queue (iii)--pairing HEAP

Source: Internet
Author: User
Tags prev

About from http://dsqiu.iteye.com/blog/1714961 1.Pairing heap

The Fibonacci Heap has two main drawbacks: the programming is difficult to implement and the actual efficiency is not as fast as the theory (due to its storage structure and four pointers). The pairing heap is made to compensate for the two drawbacks of the Fibonacci heaps-the time complexity of programming simple operations and the same as the Fibonacci heaps.

The pairing heap is actually a tree with a heap (maximum heap or minimum heap), whose properties are not determined by its structure, but by its operations (inserting, merging, reducing a keyword, and so on).

ADT for 1.1Pairing Heap

C code [CPP] view plain copy print?       pairingheapnode {int key;       struct pairingheapnode* child;       struct pairingheapnode* sibling;      struct pairingheapnode* prev; }pairheap;
operation of the 2.Pairing Heap

Note: The plot process is demonstrated with the largest heap, but the code is written in the smallest heap, forgive me. 2.1. Merging of two sub-stacks


C code[C]  View plain copy print? Static pairheap* merge_subheaps (pairheap *p, pairheap *q)    {       if (q == null)             return p;       else if (P->key <= q->key)         {           q->prev =  p;           p->sibling = q->sibling;            if (p->sibling != null)                p->sibling->prev = p;               q->sibling = p->child;            if (q->sibling != null)                q->sibling->prev  = q;              p->child =  q;           return p;        }       else       {            q->prev = p->prev;            p->prev = q;            p->sibling = q->child;           if (P- >sibling != null)                 p->sibling->prev = p;               q->child = p;           return q;        }  }    

2.2. Inserting a node


C code [C] view plain copy print?          pairheap* Pairheap_insert (pairheap *p, int key) {pairheap *node;       node = (pairheap*) malloc (sizeof (*node)); if (node = = NULL)

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.