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

  • Home
  • SEARCH
  • 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 8490853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:14:48+00:00 2026-06-10T22:14:48+00:00

good afternoon, i am trying to implement leftist heap here is my header file

  • 0

good afternoon, i am trying to implement leftist heap
here is my header file and source file
for header file

#include<iostream>
template<class comparable>
class leftistheap;

template<class comparable>
class leftistnode
{
    comparable element;
    leftistnode *left;
    leftistnode *right;
    int  npl;
    leftistnode(const comparable  &theelement,leftistnode *lt=NULL,leftistnode *rt=NULL,int np=0):element(thelement),left(lt),right(rt),npl(np){}
    friend class leftistheap<comparable>;
    };
template<class comparable>
class leftistheap
{
public:
    leftistheap();
    leftistheap (const leftistheap &rhs);
    ~leftistheap();
    bool isempthy()const ;
    bool isfull() const;
    const comparable &findmin() const;
    void insert(const comparable &x);
    void deletemin();
    void deletemin(comparable &minitem);
    void makeempthy();
    void merge(leftistheap &rhs);

    const leftistheap & operator=(const leftistheap &rhs);
private:
    leftistnode<comparable>*root;
    leftistnode<comparable>*merge(leftistnode<comparable>*h1,leftistnode<comparable>*h2)const;
     leftistnode<comparable> * merge1( leftistnode<comparable> *h1,
                                              leftistnode<comparable> *h2 ) const;
            void swapchildren( leftistnode<comparable> * t ) const;
            void reclaimmemory( leftistnode<comparable> * t ) const;
            leftistnode<comparable> * clone( leftistnode<comparable> *t ) const;
        };

and source file

#include<iostream>
#include "leftist.h"
using namespace std;
template<class comparable>
leftistheap<comparable>::leftistheap()
{
    root=NULL;
}
template<class comparable>
leftistheap<comparable>::leftistheap(const leftistheap<comparable>&rhs)
{
    root=NULL;
    *this=rhs;
}
template<class comparable>
leftistheap<comparable>::~leftistheap()
{
    makeempthy();
}
template<class comparable>
void leftistheap<comparable>::merge(leftistheap &rhs)
{
    if(this==&rhs)
         return;
    root=merge(root,rhs.root);
    rhs.root=NULL;

}
template<class comparable>
leftistnode<comparable>*leftistheap<comparable>::merge(leftistnode <comparable>*h1,leftistnode<comparable>*h2)const
{

    if(h1==NULL)
         return h2;
    if(h2==NULL)
         return h1;
    if(h1->element<h2->element)
        return merge1(h1,h2);
    else
        return merge1(h2,h1);


}

template<class comparable>
leftistnode<comparable>*leftistheap<comparable>::merge1(leftistnode<comparable>*h1,leftistnode<comparable>*h2)const
{

    if(h1->left==NULL)
        h1->left=h2;
    else
    {

        h1->right=merge(h1->right,h2);
        if(h1->left->npl<h1->right->npl)
            swapchildren(h1);
        h1->npl=h1->right->npl+1;






    }
     return h1;

}
template<class comparable>
void leftistheap<comparable>::swapchildren(leftistnode<comparable>*t)const
{
    leftistnode<comparable> *temp=t->left;
    t->left=t->right;
    t->right=emp;


}
template<class comparable>
void leftistheap<comparable>::insert(const comparable &x)
{
    root=merge(new leftistnode<comparable>(x),root);

}
template<class comparable>
const comparable &leftistheap<comparable>::findmin()const
    {
         if(isempty())
             throw Underflow();
    return root->element;


}
template<class comparable>
void leftistheap<comparable>::deletemin()
{
    if(isempty())
         throw Underflow();
    leftistnode<comparable>*oldroot=root;
    root=merge(root->left,root->right);
    delete oldroot;


}
template<class comparable>
void leftistheap<comparable>::deletemin(comparable & minitem)
{
    minitem=findmin();
    deletemin();

}
template<class comparable>
bool leftistheap<comparable>::isempthy() const
{


return root=NULL;
}
template<class comparable>
bool leftistheap<comparable>::isfull( ) const
        {
            return false;
        }

template <class comparable>
void leftistheap<comparable>::makeempthy()
        {
            reclaimmemory( root );
            root = NULL;
        }
        template<class comparable>
        const leftistheap<comparable>& leftistheap<comparable >::operator=(const leftistheap<comparable>& rhs)
        {
            if(this != &rhs)
            {
            makeempthy();
            root=clone(rhs.root);

            }
            return *this;
        }
        template<class comparable>
        void leftistheap<comparable>::reclaimmemory(leftistnode<comparable>*t) const
        {
            if(t!=NULL)
            {
                reclaimmemory(t->left);
                reclaimmemory(t->right);
                 delete t;


            }


        }
        template<class comparable>
        leftistnode<comparable>*leftistheap<comparable>::clone(leftistnode<comparable> *t)const
        {

            if(t==NULL)
                 return NULL;
            else
                return new leftistnode<comparable>( t->element, clone( t->left ),
                                              clone( t->right ), t->npl );
        }


int main()
{
    int numitems=100;
    leftistheap<int>h;
    leftistheap<int>h1;
    leftistheap<int>h2;
    int i=37;
    for(i=37;i!=0;i=(i+37)%numitems)
        if(i%2==0)
            h1.insert(i);
        else
            h.insert(i);
    h.merge(h1);
    h2=h;
     for( i = 1; i < numitems; i++ )
            {
                int x;
                h2.deletemin( x );
                if( x != i )
                    cout << "Oops! " << i << endl;
            }

            if (! h1.isempthy( ) )
                cout << "Oops! h1 should have been empty!" << endl;


    return 0;
}

but i am getting these errors, please help me why are these error?i have declared functions as const,but i am not modifing it,so what is problem?i am trying just to define this functions and this is all what i wont to do,the same problem is also with root,why compiler says,that i am trying to change value of root?isempthy() function just checks if (root==NULL),but it does not change it’s value,so i am really confused,will my code compile in case of i remove const keywords?will not it change main behavior of code?i am new here and sorry for posting so large code,but to make code more clearly,i though post header and source file together for simplifying task for you,please help me guys,i am happy that there exist so good site.i would be happy in case of getting help from you
errors are :

2010\projects\leftist_heap\leftist_heap\leftist_heap.cpp(115): error C3490: 'root' cannot be modified because it is being accessed through a const object
1>          2010\projects\leftist_heap\leftist_heap\leftist_heap.cpp(112) : while compiling class template member function 'bool leftistheap<comparable>::isempthy(void) const'
1>          with
1>          [
1>              comparable=int
1>          ]
1>          2010\projects\leftist_heap\leftist_heap\leftist_heap.cpp(169) : see reference to class template instantiation 'leftistheap<comparable>' being compiled
1>          with
1>          [
1>              comparable=int
1>          ]
1>2010\projects\leftist_heap\leftist_heap\leftist_heap.cpp(115): warning C4800: 'leftistnode<comparable> *const ' : forcing value to bool 'true' or 'false' (performance warning)
1>          with
1>          [
1>              comparable=int
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • 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-10T22:14:49+00:00Added an answer on June 10, 2026 at 10:14 pm

    Change implementation of your isempty member function to (logic is to check whether tree is empty or not) :

    template<class comparable>
    bool leftistheap<comparable>::isempthy() const
    {
    return root==NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good afternoon, We are trying to build a prototype of Memory Mapped File Caching
Good afternoon, I'm trying to optimize a source code based on Require.js and Backbone
Good afternoon ... I'm trying to implement a web grid web grid and call
Good afternoon all, What I'm trying to accomplish : I'd like to implement an
Good Afternoon, I am trying to extract the contents of a jar file programmatically
I've spent a good while this afternoon trying to implement the deserialization of JSON
Good afternoon, I've run into some issues trying to combine HTTP caching with Rack::Cache
Good Afternoon, A customer has provided me with a spreadsheet file his team uses
good afternoon, I am trying to display a list of items in a table
Good Afternoon, I am trying to assign some variables to a listing which has

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.