Soham_K

Binomial heap print

Nov 13th, 2020 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. {
  2.     Node *x = new Node();
  3.     x = root;
  4.     if(root==NULL) {
  5.         cout << "The heap is empty" << endl;
  6.         return;
  7.     }
  8.  
  9.     if(decider==1)
  10.     {
  11.         cout << "Printing Binomial Heap..." << endl;
  12.         while(x!=NULL)
  13.         {
  14.             cout << "Binomial Tree, B" << x->degree << endl;
  15.             int i=0;
  16.             Node *t = new Node();
  17.             t = x;
  18.             vector <Node*> level;
  19.             vector <Node*> children;
  20.             while(i<=x->degree) {
  21.                 cout << "Level " << i << " : ";
  22.                 if(i==0) {
  23.                     cout << x->key << endl;
  24.                     i++;
  25.                     continue;
  26.                 }
  27.                 else if(i==1) {
  28.                     t = t->child;
  29.                     Node *n = new Node(t->key, t->degree, t->parent, t->child, t->sibling);
  30.                     while(n!=NULL) {
  31.                         level.push_back(n);
  32.                         cout << n->key << " ";
  33.                         if(n->child!=NULL)  children.push_back(n->child);
  34.                         n = n->sibling;
  35.                     }
  36.                     cout << endl;
  37.                     level.clear();
  38.                     level = children;
  39.                     children.clear();
  40.                     i++;
  41.                     continue;
  42.                 }
  43.                 for(auto y: level) {
  44.                     Node *n = new Node(y->key, y->degree, y->parent, y->child, y->sibling);
  45.                     while(n!=NULL) {
  46.                         cout << n->key << " ";
  47.                         if(n->child!=NULL)  children.push_back(n->child);
  48.                         n = n->sibling;
  49.                     }
  50.                 }
  51.                 i++;
  52.                 level.clear();
  53.                 level = children;
  54.                 children.clear();
  55.                 cout << endl;
  56.             }
  57.             x = x->sibling;
  58.         }
  59.     }
  60. }
Add Comment
Please, Sign In to add comment