C++ Program to Implement Leftist Heap

This C++ Program demonstrates operations on LeftList Heap.

Here is source code of the C++ Program to demonstrate LeftList Heap. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement LeftList Heap
  3.  */
  4. #include <iostream>
  5. #include <cstdlib>
  6. using namespace std;
  7. /*
  8.  * Node Class Declaration
  9.  */
  10. class LeftistNode
  11. {
  12.     public:
  13.         int element;
  14.         LeftistNode *left;
  15.         LeftistNode *right;
  16.         int npl;
  17.         LeftistNode(int & element, LeftistNode *lt = NULL,
  18.                     LeftistNode *rt = NULL, int np = 0)
  19.         {
  20.             this->element = element;
  21.             right = rt;
  22.             left = lt,
  23.             npl =  np;
  24.         }
  25. };
  26. /*
  27.  * Class Declaration
  28.  */
  29. class LeftistHeap
  30. {
  31.     public:
  32.         LeftistHeap();
  33.         LeftistHeap(LeftistHeap &rhs);
  34.         ~LeftistHeap();
  35.         bool isEmpty();
  36.         bool isFull();
  37.         int &findMin();
  38.         void Insert(int &x);
  39.         void deleteMin();
  40.         void deleteMin(int &minItem);
  41.         void makeEmpty();
  42.         void Merge(LeftistHeap &rhs);
  43.         LeftistHeap & operator=(LeftistHeap &rhs);
  44.     private:
  45.         LeftistNode *root;
  46.         LeftistNode *Merge(LeftistNode *h1, LeftistNode *h2);
  47.         LeftistNode *Merge1(LeftistNode *h1, LeftistNode *h2);
  48.         void swapChildren(LeftistNode * t);
  49.         void reclaimMemory(LeftistNode * t);
  50.         LeftistNode *clone(LeftistNode *t);
  51. };
  52.  
  53.  
  54. /*
  55.  * Construct the leftist heap.
  56.  */
  57. LeftistHeap::LeftistHeap()
  58. {
  59.     root = NULL;
  60. }
  61.  
  62. /*
  63.  * Copy constructor.
  64.  */
  65. LeftistHeap::LeftistHeap(LeftistHeap &rhs)
  66. {
  67.     root = NULL;
  68.     *this = rhs;
  69. }
  70.  
  71. /*
  72.  * Destruct the leftist heap.
  73.  */
  74. LeftistHeap::~LeftistHeap()
  75. {
  76.     makeEmpty( );
  77. }
  78.  
  79. /*
  80.  * Merge rhs into the priority queue.
  81.  * rhs becomes empty. rhs must be different from this.
  82. */
  83. void LeftistHeap::Merge(LeftistHeap &rhs)
  84. {
  85.     if (this == &rhs)
  86.         return;
  87.     root = Merge(root, rhs.root);
  88.     rhs.root = NULL;
  89. }
  90.  
  91. /*
  92.  * Internal method to merge two roots.
  93.  * Deals with deviant cases and calls recursive Merge1.
  94.  */
  95. LeftistNode *LeftistHeap::Merge(LeftistNode * h1, LeftistNode * h2)
  96. {
  97.     if (h1 == NULL)
  98.         return h2;
  99.     if (h2 == NULL)
  100.         return h1;
  101.     if (h1->element < h2->element)
  102.         return Merge1(h1, h2);
  103.     else
  104.         return Merge1(h2, h1);
  105. }
  106.  
  107. /*
  108.  * Internal method to merge two roots.
  109.  * Assumes trees are not empty, and h1's root contains smallest item.
  110.  */
  111. LeftistNode *LeftistHeap::Merge1(LeftistNode * h1, LeftistNode * h2)
  112. {
  113.     if (h1->left == NULL)
  114.         h1->left = h2;
  115.     else
  116.     {
  117.         h1->right = Merge(h1->right, h2);
  118.         if (h1->left->npl < h1->right->npl)
  119.             swapChildren(h1);
  120.         h1->npl = h1->right->npl + 1;
  121.     }
  122.     return h1;
  123. }
  124.  
  125. /*
  126.  * Swaps t's two children.
  127.  */
  128. void LeftistHeap::swapChildren(LeftistNode * t)
  129. {
  130.     LeftistNode *tmp = t->left;
  131.     t->left = t->right;
  132.     t->right = tmp;
  133. }
  134.  
  135. /*
  136.  * Insert item x into the priority queue, maintaining heap order.
  137.  */
  138. void LeftistHeap::Insert(int &x)
  139. {
  140.     root = Merge(new LeftistNode(x), root);
  141. }
  142.  
  143. /*
  144.  * Find the smallest item in the priority queue.
  145.  * Return the smallest item, or throw Underflow if empty.
  146.  */
  147. int &LeftistHeap::findMin()
  148. {
  149.     return root->element;
  150. }
  151.  
  152. /*
  153.  * Remove the smallest item from the priority queue.
  154.  * Throws Underflow if empty.
  155.  */
  156. void LeftistHeap::deleteMin()
  157. {
  158.     LeftistNode *oldRoot = root;
  159.     root = Merge(root->left, root->right);
  160.     delete oldRoot;
  161. }
  162.  
  163. /*
  164.  * Remove the smallest item from the priority queue.
  165.  * Pass back the smallest item, or throw Underflow if empty.
  166.  */
  167. void LeftistHeap::deleteMin(int &minItem)
  168. {
  169.     if (isEmpty())
  170.     {
  171.         cout<<"Heap is Empty"<<endl;
  172.         return;
  173.     }
  174.     minItem = findMin();
  175.     deleteMin();
  176. }
  177.  
  178. /*
  179.  * Test if the priority queue is logically empty.
  180.  * Returns true if empty, false otherwise.
  181.  */
  182. bool LeftistHeap::isEmpty()
  183. {
  184.     return root == NULL;
  185. }
  186.  
  187. /*
  188.  * Test if the priority queue is logically full.
  189.  * Returns false in this implementation.
  190.  */
  191. bool LeftistHeap::isFull()
  192. {
  193.     return false;
  194. }
  195.  
  196. /*
  197.  * Make the priority queue logically empty.
  198.  */
  199. void LeftistHeap::makeEmpty()
  200. {
  201.     reclaimMemory(root);
  202.     root = NULL;
  203. }
  204.  
  205. /*
  206.  * Deep copy.
  207.  */
  208. LeftistHeap &LeftistHeap::operator=(LeftistHeap & rhs)
  209. {
  210.     if (this != &rhs)
  211.     {
  212.         makeEmpty();
  213.         root = clone(rhs.root);
  214.     }
  215.     return *this;
  216. }
  217.  
  218. /*
  219.  * Internal method to make the tree empty.
  220.  */
  221. void LeftistHeap::reclaimMemory(LeftistNode * t)
  222. {
  223.     if (t != NULL)
  224.     {
  225.         reclaimMemory(t->left);
  226.         reclaimMemory(t->right);
  227.         delete t;
  228.     }
  229. }
  230.  
  231. /*
  232.  * Internal method to clone subtree.
  233.  */
  234. LeftistNode *LeftistHeap::clone(LeftistNode * t)
  235. {
  236.     if (t == NULL)
  237.         return NULL;
  238.     else
  239.         return new LeftistNode(t->element, clone(t->left), clone(t->right), t->npl);
  240. }
  241.  
  242. int main()
  243. {
  244.     LeftistHeap h;
  245.     LeftistHeap h1;
  246.     LeftistHeap h2;
  247.     for (int i = 0; i < 20; i++)
  248.     {
  249.         if (i % 2 == 0)
  250.         {
  251.             h.Insert(i);
  252.             cout<<"Element"<<i<<" inserted in Heap 1"<<endl;
  253.         }
  254.         else
  255.         {
  256.             h1.Insert(i);
  257.             cout<<"Element"<<i<<" inserted in Heap 2"<<endl;
  258.         }
  259.     }
  260.     h.Merge(h1);
  261.     h2 = h;
  262.     for (int i = 0; i < 20; i++)
  263.     {
  264.         int x;
  265.         h2.deleteMin(x);
  266.         cout<<"Element "<<x<<" Deleted"<<endl;
  267.         if (h2.isEmpty())
  268.         {
  269.             cout<<"The Heap is Empty"<<endl;
  270.             break;
  271.         }
  272.     }
  273.     return 0;
  274. }

$ g++ leftlistheap.cpp
$ a.out
Element0 inserted in Heap 1
Element1 inserted in Heap 2
Element2 inserted in Heap 1
Element3 inserted in Heap 2
Element4 inserted in Heap 1
Element5 inserted in Heap 2
Element6 inserted in Heap 1
Element7 inserted in Heap 2
Element8 inserted in Heap 1
Element9 inserted in Heap 2
Element10 inserted in Heap 1
Element11 inserted in Heap 2
Element12 inserted in Heap 1
Element13 inserted in Heap 2
Element14 inserted in Heap 1
Element15 inserted in Heap 2
Element16 inserted in Heap 1
Element17 inserted in Heap 2
Element18 inserted in Heap 1
Element19 inserted in Heap 2
Element 0 Deleted
Element 1 Deleted
Element 2 Deleted
Element 3 Deleted
Element 4 Deleted
Element 5 Deleted
Element 6 Deleted
Element 7 Deleted
Element 8 Deleted
Element 9 Deleted
Element 10 Deleted
Element 11 Deleted
Element 12 Deleted
Element 13 Deleted
Element 14 Deleted
Element 15 Deleted
Element 16 Deleted
Element 17 Deleted
Element 18 Deleted
Element 19 Deleted
The Heap is Empty
 
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.