Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:16:35+00:00 2026-06-16T01:16:35+00:00

I just implemented the pairing heap data structure. Pairing heap supports insert, find-min, merge

  • 0

I just implemented the pairing heap data structure. Pairing heap supports insert, find-min, merge in O(1) amortized time and delete, delete-min in O(logN) amortized time. But the most remarkable operation is the decrease-key, which has complexity O(log logN). More about pairing heaps can be found at http://en.wikipedia.org/wiki/Pairing_heap.

I have implemented the insert, merge, and delete-min operations, but the wikipedia article didn’t say how to decrease a key of a given node, so I couldn’t implement it. Could someone tell me how it actually works?

Here is my code:

template< class key_t, class compare_t=std::less< key_t > >
struct pairing_heap {
private:
  struct node { 
    key_t key; std::vector< node* > c;
    node( key_t k=key_t() ) : key( k ), c( std::vector< node* >() ) {}
  };
  node *root;
  compare_t cmp;
  unsigned sz;
public:
  typedef key_t value_type;
  typedef compare_t compare_type;
  typedef pairing_heap< key_t, compare_t > self_type;

  pairing_heap() : root( 0 ) {}
  node* merge( node *x, node *y ) {
    if( !x ) return y;
    else if( !y ) return x;
    else {
      if( cmp( x->key, y->key ) ) {
        x->c.push_back( y );
        return x;
      } else {
        y->c.push_back( x );
        return y;
      }
    }
  }
  node* merge_pairs( std::vector< node* > &c, unsigned i ) {
    if( c.size()==0 || i>=c.size() ) return 0;
    else if( i==c.size()-1 ) return c[ i ];
    else {
      return merge( merge( c[ i ], c[ i+1 ] ), merge_pairs( c, i+2 ) );
    }
  }
  void insert( key_t x ) {
    root = merge( root, new node( x ) );
    sz++;
  }
  key_t delmin() {
    if( !root ) throw std::runtime_error( "std::runtime_error" );
    key_t ret=root->key;
    root = merge_pairs( root->c, 0 );
    sz--;
    return ret;
  }
  bool empty() const { return root==0; }
  unsigned size() const { return sz; }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:16:36+00:00Added an answer on June 16, 2026 at 1:16 am

    According to the original paper on pairing heaps, page 114, the decrease-key operation is implemented as follows. First, change the priority of the node whose key is to be decreased to have the new priority. If its priority is still greater than its parent (or if it’s the root of the tree), you are done. Otherwise, cut the link from that node to its parent, then use the merge operation to combine the original heap with the subtree that was just cut from that tree.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just implemented a best match file search algorithm to find the closest match
Going through classic data structures and have stopped on linked lists.Just implemented a circular
I just implemented a DatePicker for my app. But I'm having a hard time
I just implemented sharding with RavenDB. When running it for the first time, this
I just implemented QuickSort algorithm from book and got weird output. It works but
I just implemented a remember me feature for a user login on a website.
I just implemented uploadify in my project, and I noticed what seems like an
We just implemented SVN usage at our office and the other dev and myself
So I just implemented a base controller on my MVC3 site in order to
I've just implemented Elmah for logging in my MVC3 app, and of course all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.