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 7618699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:28:26+00:00 2026-05-31T03:28:26+00:00

here is my code for implementing HeapTree #include<iostream> #include<assert.h> using namespace std; template <class

  • 0

here is my code for implementing HeapTree

 #include<iostream>
#include<assert.h>
using namespace std;

template <class Elem>
class HeapTree
{
  public:
    HeapTree(int MaxSize=500);
    HeapTree(const HeapTree<Elem> &OtherTree);
    HeapTree(Elem *Array, int ElemNum, int MaxSize);
    Elem *Sort(void); // Built-in HeapSort Algorithm
    ~HeapTree(void);

    bool Add(const Elem &Item);  // Add the Item to Heap
    Elem Remove(void);           // Remove and return Item from Heap

    inline int GetSize(void);    // Returns the number of nodes in the Heap

protected:
        Elem *Data;//actual data array
        int currentnum;
        const int Max_size;
        void shiftup(int node);
        void shiftdown(int node);
        inline int parent(int node);
 inline  int leftchild(int node);

};
template <class Elem>
inline int HeapTree<Elem>::parent(int node){
assert(node>0);
return (node-1)/2;

}
template <class Elem>
inline int HeapTree<Elem>::leftchild(int node){
 return (node*2)+1;
}
//HeapTree construction
template<class Elem>
HeapTree<Elem>::HeapTree(int maxsize)
:Max_size(maxsize)
{
Data=new Elem[Max_size];
currentnum=0;

}
//HeapTree copy construction function
template<class Elem>
HeapTree<Elem>::HeapTree(const HeapTree<Elem> &other)
        :Max_size(other.Max_size)

{
        Data=new Elem[Max_size];
        int  current=other.currentnum;
         for (int i=0;i<other.currentnum;++i)
                 Data[i]=other.Data[i];


}
template <class Elem>
HeapTree<Elem>::HeapTree(Elem *Array,int ElemNum,int Maxsize)
:Max_size(Maxsize)
{
Data=new Elem[Max_size];
currentnum=ElemNum;
 for (int i=0;i<ElemNum;++i)
         Data[i]=Array[i];
  for (int i=parent(currentnum-1);i>=0;--i)
          shiftdown(i);

}
// Built-in Heap Sort algorithm
template<class Elem>
Elem *HeapTree<Elem>::Sort(void)
{
Elem * newArray=new Elem[currentnum];
for (int i=currentnum-1;i>=0;--i)
{
newArray[i]=Remove();


}
 return newArray;
}
//HeapTree destruction function
template <class Elem>
HeapTree<Elem>::~HeapTree(void)
{
if (Data)
 delete Data;

}
template <class Elem>
bool HeapTree<Elem>::Add(const Elem &item)
{
if (currentnum>=Max_size)
 return false;
Data[currentnum]=item;
shiftup(currentnum++);
return true;

}
//remove function
template <class Elem>
Elem HeapTree<Elem>::Remove(void)
{
assert(currentnum>0);
Elem temp=Data[0];
Data[0]=Data[--currentnum];//replace with last element;
shiftdown(0);
return temp;


}
// GetSize() function
template<class Elem>
inline int HeapTree<Elem>::GetSize(void)
{
return currentnum;
}
//shift up
template<class Elem>
void HeapTree<Elem>::shiftup(int node)
{
int current=node;
int Parent=parent(current);
Elem item=Data[current];
while(current>0)
{
if(Data[Parent]<item)
{
Data[current]=Data[Parent];
current=Parent;
Parent=parent(current);

}
else
break;

}
Data[current]=item;
}
// ShiftDown() function
template <class Elem>
void HeapTree<Elem>::shiftdown(int node)
{
int current=node,
child=leftchild(current);
Elem item=Data[current];
while(child<currentnum)
{
if (child<(currentnum-1))
if(Data[child]<Data[child+1])
++child;
if(item<Data[child]){
Data[current]=Data[child];
current=child;
child=leftchild(current);
}
else
break;

}

Data[current]=item;
}
int main(){
        int a[]={12,31,10,6,4,7,9,8,0,11};
        int n=sizeof(a)/sizeof(a[0]);
        HeapTree<int>hp(a,n,n);
        hp.Sort();
        cout<<hp.Remove()<<" ";

return 0;
}

but when i run it,it writes compiltion error,i think that ,in main() place i have not created instance of HeapTree correctly,please help me to determine what is wrong with this code

  • 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-05-31T03:28:27+00:00Added an answer on May 31, 2026 at 3:28 am

    HeapTree::Sort calls HeapTree::Remove until your heap is sorted. After last call of Remove inside Sort value of currentnum is 0 so another call to Remove can’t assert condition you specified. (currentnum>0)

    Note: I believe you shouldn’t empty a tree by sorting it!

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

Sidebar

Related Questions

here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
Here is code example class A{ int i; public: A(int i) : i(i) {}
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
I am implementing Code Generation for WindowsForm control at Design-Time, using a Custom CodeDomSerializer.
I'm implementing some code using the java.util.concurrency framework. I will be passing a collection
Suppose I've written class template declaration somewhere in collector.h : template <class T, int
EDIT: Here's the code based on Max's suggestion editor.getControl().addKeyListener(new KeyAdapter() { @Override public void
I am implementing localization in my app. Here is the code, C_TITLE.text = NSLocalizedString(@C_TITLE,

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.