Following is a binimial heap(data Structure) I copied and revised, it has 9 compile errors in my old version borland C++ compiler. As a non-experienced programming learner I've tried mine and can't find out why. Anybody has the patience to help me find out solution, I am much appreciated:
#include <math.h>
#include <fstream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
enum boolean {FALSE, TRUE};
const MaxTreeNo = 12;
class BinomialNode
{
friend BinomialHeap;
public:
BinomialNode(int, BinomialNode *, BinomialNode * );
~BinomialNode() {};
int TellKey()
{return TheKey; };
const BinomialNode * TellLeftChild()
{return LeftChild;};
const BinomialNode * TellSibling()
{return NextSibling;};
protected:
int TheKey;
BinomialNode * LeftChild;
BinomialNode * NextSibling;
};
class BinomialHeap
{
public:
BinomialHeap( );
~BinomialHeap( );
boolean IsEmpty( ) const;
boolean IsFull( ) const;
const int & FindMin( ) const;
void Insert( int );
void DeleteMin( );
void DeleteMin( int &);
void ShowMin();
void MakeEmpty( );
void Merge( BinomialHeap & rhs );
void ShowHeap();
void ShowNode(BinomialNode *);
private:
int CurrentSize;
BinomialNode * (RootNode[MaxTreeNo]); // ??? with or without * do i make it poer?
FindMinIndex( ) const;
Capacity( ) const;
BinomialNode * CombineTrees( BinomialNode *,
BinomialNode * );
void MakeEmpty( BinomialNode * & t ) const;
};
class Menu
{
public:
Menu(int=0, char ** = NULL, void(**)(void)=NULL);
~Menu();
void Display(void) const;
GetChoice(void);
boolean Done(void) const;
void DoChoice(int & ) const;
void TypicalQuit(void);
protected:
int NumItems;
char ** MenuItems;
void (** TheChoices)(void);
int TheChoice;
};
void CreatNew(void);
void InsertOne(void);
void ExtractMin(void);
void FindMin(void);
void ShowHeap(void);
void DoQuit(void);
char * MenuWords[] = {"Creat New Heap", "Insert an Key", "Extract the minimal",
"Find the minimal", "Show the Heap",
"Quit"};
void ( * MenuChoices[] ) (void) = {CreatNew, InsertOne, ExtractMin, FindMin,
ShowHeap, DoQuit};
Menu * TheMenu = new Menu(6, MenuWords, MenuChoices);
BinomialHeap * MyBHeap = new BinomialHeap;
void main(void)
{
int ChoiceIn = 0;
do
{
TheMenu->Display();
ChoiceIn=TheMenu->GetChoice();
TheMenu->DoChoice(ChoiceIn); //see bbb for -1 adjustment
}
while ( !(TheMenu->Done() ) );
};
BinomialNode::BinomialNode( int Key,
BinomialNode * LC, BinomialNode * NS )
{
TheKey=Key;
LeftChild=LC;
NextSibling=NS;
};
BinomialHeap::BinomialHeap( )
{
for(int i = 0; i < MaxTreeNo; i++ )
RootNode[ i ] = NULL;
CurrentSize = 0;
}
BinomialHeap::~BinomialHeap( )
{
MakeEmpty( );
}
void BinomialHeap::Merge( BinomialHeap & rhs )
{
if( this == &rhs )
{return;}
else
{
int WhichCase=0;
BinomialNode * Carry = NULL;
for(int i = 0; i<MaxTreeNo; i++)
{
BinomialNode *t1 = RootNode[ i ];//roots of this heap
BinomialNode *t2 = rhs.RootNode[ i ];//roots of the heap to Merge
WhichCase = t1 == NULL ? 0 : 1;
WhichCase += t2 == NULL ? 0 : 2;
WhichCase += Carry == NULL ? 0 : 4;
switch( WhichCase )
{
case 0: // No trees
case 1: // Only this tree
break;
case 2: // Only rhs
RootNode[ i ] = t2;
rhs.RootNode[ i ] = NULL;
break;
case 4: // Only Carry
RootNode[ i ] = Carry;
Carry = NULL;
break;
case 3: /* this and rhs */
Carry = CombineTrees( t1, t2 );
RootNode[ i ] = rhs.RootNode[ i ] = NULL;
break;
case 5: /* this and Carry */
Carry = CombineTrees( t1, Carry );
RootNode[ i ] = NULL;
break;
case 6: /* rhs and Carry */
Carry = CombineTrees( t2, Carry );
rhs.RootNode[ i ] = NULL;
break;
case 7: /* All three */
RootNode[ i ] = Carry;
Carry = CombineTrees( t1, t2 );
rhs.RootNode[ i ] = NULL;
break;
};
};
for( int k = 0; k < MaxTreeNo; k++ )
rhs.RootNode[k] = NULL;
rhs.CurrentSize = 0;
}
BinomialNode * BinomialHeap::CombineTrees( BinomialNode * T1,
BinomialNode * T2 )
{
if( T2->TellKey() < T1->TellKey() )
return CombineTrees( T2, T1 );
T2->NextSibling = T1->LeftChild;
T1->LeftChild = T2;
return T1;
};
void BinomialHeap::Insert( int x )
{
if (IsFull())
{cout<< "Disk Full!";
return;}
else
{
BinomialHeap OneItem;
OneItem.RootNode[ 0 ] = new BinomialNode(x);//=operator need to be overloaded??;
Merge( OneItem );
CurrentSize++;
};
const int & BinomialHeap::FindMin( ) const
{
if( IsEmpty( ) )
{ cout<<"Empty database.";
return;}
else
{
return RootNode[ FindMinIndex( ) ]->TellKey();
};
int BinomialHeap::FindMinIndex( ) const
{
int MinIndex=0;
for( i = 0; i<MaxTreeNo; i++ )
{
if( RootNode[ i ] != NULL &&
RootNode[ i ]->TellKey() < RootNode[ MinIndex ]->TellKey() )
MinIndex = i;
};
return MinIndex;
};
void BinomialHeap:eleteMin()
{
if( isEmpty( ) )
{ cout<< "Empty Database!!";}
else
{
MinIndex = FindMinIndex( );
BinomialNode * OldRoot = RootNode[ MinIndex ];//can't delete Node
BinomialNode * NextNodeToCopy = OldRoot->LeftChild;//keep record
delete OldRoot;//delete RootNode poier after keeping record of two poer
BinomialHeap * TemHeap; // one tree breaks o n-1 trees, make it o heap
TemHeap->CurrentSize=pow(2,MinIndex)- 1;//??
for( j = MinIndex - 1; j >= 0; j-- ) //"minIndex-1" is the root size of Heap
// and leftmost child is the biggest tree, rightmost is B0
{
TemHeap.RootNode[ j ] = NextNodeToCopy;
NextNodeToCopy = NextNodeToCopy->NextSibling;
TemHeap.RootNode[ j ]->NextSibling = NULL;//otherwise they'll keep
//their oringinal sibling poer
};
};
RootNode[ MinIndex ] = NULL;
CurrentSize--;
Merge( TemHeap );
};
boolean BinomialHeap::IsEmpty( ) const
{
return CurrentSize == 0;
};
boolean BinomialHeap::IsFull( ) const
{
return CurrentSize == Capacity( );
};
void BinomialHeap::ShowMin()
{
if( IsEmpty( ) )
{ cout<< "Already An Empty Database!!";}
else
MinIndex = FindMinIndex( );
cout<< RootNode[MinIndex]->TellKey();
};
void BinomialHeap::MakeEmpty( )
{
CurrentSize = 0;
for( i = 0; i < MaxTreeNo; i++ )
{MakeEmpty( RootNode );};
};
void BinomialHeap::ShowHeap()
{
if (IsEmpty())
{cout<< "Empty Data Base!";}
else
for (i=0; i<MaxTreeNo; i++)
{ ShowNode(RootNode);};
};
void BinomialHeap::ShowNode(BinomialNode * t)
{
if (t !=NULL)
{
cout<< t->TellKey();
ShowNode(t->LeftChild);
ShowNode(t->NextSibling);
};
};
BinomialHeap::Capacity( ) const
{
return pow(2, MaxTreeNo)-1;
};
void BinomialHeap::
MakeEmpty( BinomialNode * & t )
{
if( t != NULL )
{
MakeEmpty( t->leftChild );
MakeEmpty( t->nextSibling );
delete t;
t = NULL;
};
};
Menu :: Menu ( int howmany, char ** TheMenu, void(**thefunctions)(void))
{
NumItems = howmany;
MenuItems = TheMenu;
TheChoices= thefunctions;
TheChoice = 0;
};
Menu::~Menu()
{};
void Menu::display() const
{
clrscr();
cout<< setw(45) <<"Main Menu" <<endl <<endl;
for ( i=0; i<NumItems; i++)
{
cout <<setw(20) <<i+1 <<"." << MenuItems <<endl;
};
cout<< endl;
};
Menu::getchoice()
{
achoice;
cout<< "Enter the Number of your choice:";
cin>> achoice;
while( (achoice<1) || (achoice > NumItems))
{
cout<< endl <<"Please select a number between one and "
<< NumItems <<": ";
cin>> achoice;
};
TheChoice = achoice;
return achoice;
};
boolean Menu:one() const
{
return bool (TheChoice==6);
};
void Menu:oChoice(int & PickedChoice) const//use reference here??
{
(*TheChoices[PickedChoice-1])();//bbb
};
void Menu::typicalquit(void)
{
char YesNo;
clrscr();
cout<< setw(45) <<"Quiting The Program " <<endl <<endl;
cout<< "Are you Sure you want to quit (Y/N) ?";
cin>> YesNo;
YesNo= toupper(YesNo);
while ((YesNo !='Y') && (YesNo!='N'))
{
cout<< "Please enter <Y> or <N>: ";
cin>> YesNo;
YesNo = toupper(YesNo);
};
if (YesNo=='N')
{
TheChoice=0;
cout <<endl <<"Press the <Enter> key to continue: ";
}
else
{
cout << endl <<" Press the <Enter> key to finish up: ";
};
getch();
};
void InsertOne(void)
{
clrscr();
cout<<setw(45) <<" Adding a new data to the Binomial Heap " <<endl <<endl;
if(MyHeap->IsFull())
{ cout <<"Sorry, Disk space full.";}
else
{
ToBeInserted;
cin.ignore();
cout<< "What number do you want to insert?";
cin<< ToBeInserted;
MyBHeap.Insert( ToBeInserted );
};
cout <<endl <<"Press the <Enter> to continue: ";
getch();
};
void FindMin(void)
{
clrscr();
cout<<setw(45) <<"Find the Mininum data... " <<endl;
MyBHeap.ShowMin();
cout <<endl <<"Press the <Enter> to continue: ";
getch();
};
void ExtractMin(void)
{
clrscr();
cout<< setw(45) <<"The Minimum data is..." <<endl <<endl;
MyBHeap.ShowMin();
MyBHeap.DeleteMin();
cout <<endl <<"Press the <Enter> to continue: ";
getch();
};
void ShowHeap(void) //global function ShowHeap has same name as BinomialHeap::ShowHeap()
// it actually just call it
{
MyBHeap.ShowHeap();//what if there are more than one page of data???
cout <<endl <<endl <<"Press the <Enter> key to continue: ";
getch();
};
void DoQuit(void)
{ TheMenu->typicalquit();};
#include <math.h>
#include <fstream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
enum boolean {FALSE, TRUE};
const MaxTreeNo = 12;
class BinomialNode
{
friend BinomialHeap;
public:
BinomialNode(int, BinomialNode *, BinomialNode * );
~BinomialNode() {};
int TellKey()
{return TheKey; };
const BinomialNode * TellLeftChild()
{return LeftChild;};
const BinomialNode * TellSibling()
{return NextSibling;};
protected:
int TheKey;
BinomialNode * LeftChild;
BinomialNode * NextSibling;
};
class BinomialHeap
{
public:
BinomialHeap( );
~BinomialHeap( );
boolean IsEmpty( ) const;
boolean IsFull( ) const;
const int & FindMin( ) const;
void Insert( int );
void DeleteMin( );
void DeleteMin( int &);
void ShowMin();
void MakeEmpty( );
void Merge( BinomialHeap & rhs );
void ShowHeap();
void ShowNode(BinomialNode *);
private:
int CurrentSize;
BinomialNode * (RootNode[MaxTreeNo]); // ??? with or without * do i make it poer?
FindMinIndex( ) const;
Capacity( ) const;
BinomialNode * CombineTrees( BinomialNode *,
BinomialNode * );
void MakeEmpty( BinomialNode * & t ) const;
};
class Menu
{
public:
Menu(int=0, char ** = NULL, void(**)(void)=NULL);
~Menu();
void Display(void) const;
GetChoice(void);
boolean Done(void) const;
void DoChoice(int & ) const;
void TypicalQuit(void);
protected:
int NumItems;
char ** MenuItems;
void (** TheChoices)(void);
int TheChoice;
};
void CreatNew(void);
void InsertOne(void);
void ExtractMin(void);
void FindMin(void);
void ShowHeap(void);
void DoQuit(void);
char * MenuWords[] = {"Creat New Heap", "Insert an Key", "Extract the minimal",
"Find the minimal", "Show the Heap",
"Quit"};
void ( * MenuChoices[] ) (void) = {CreatNew, InsertOne, ExtractMin, FindMin,
ShowHeap, DoQuit};
Menu * TheMenu = new Menu(6, MenuWords, MenuChoices);
BinomialHeap * MyBHeap = new BinomialHeap;
void main(void)
{
int ChoiceIn = 0;
do
{
TheMenu->Display();
ChoiceIn=TheMenu->GetChoice();
TheMenu->DoChoice(ChoiceIn); //see bbb for -1 adjustment
}
while ( !(TheMenu->Done() ) );
};
BinomialNode::BinomialNode( int Key,
BinomialNode * LC, BinomialNode * NS )
{
TheKey=Key;
LeftChild=LC;
NextSibling=NS;
};
BinomialHeap::BinomialHeap( )
{
for(int i = 0; i < MaxTreeNo; i++ )
RootNode[ i ] = NULL;
CurrentSize = 0;
}
BinomialHeap::~BinomialHeap( )
{
MakeEmpty( );
}
void BinomialHeap::Merge( BinomialHeap & rhs )
{
if( this == &rhs )
{return;}
else
{
int WhichCase=0;
BinomialNode * Carry = NULL;
for(int i = 0; i<MaxTreeNo; i++)
{
BinomialNode *t1 = RootNode[ i ];//roots of this heap
BinomialNode *t2 = rhs.RootNode[ i ];//roots of the heap to Merge
WhichCase = t1 == NULL ? 0 : 1;
WhichCase += t2 == NULL ? 0 : 2;
WhichCase += Carry == NULL ? 0 : 4;
switch( WhichCase )
{
case 0: // No trees
case 1: // Only this tree
break;
case 2: // Only rhs
RootNode[ i ] = t2;
rhs.RootNode[ i ] = NULL;
break;
case 4: // Only Carry
RootNode[ i ] = Carry;
Carry = NULL;
break;
case 3: /* this and rhs */
Carry = CombineTrees( t1, t2 );
RootNode[ i ] = rhs.RootNode[ i ] = NULL;
break;
case 5: /* this and Carry */
Carry = CombineTrees( t1, Carry );
RootNode[ i ] = NULL;
break;
case 6: /* rhs and Carry */
Carry = CombineTrees( t2, Carry );
rhs.RootNode[ i ] = NULL;
break;
case 7: /* All three */
RootNode[ i ] = Carry;
Carry = CombineTrees( t1, t2 );
rhs.RootNode[ i ] = NULL;
break;
};
};
for( int k = 0; k < MaxTreeNo; k++ )
rhs.RootNode[k] = NULL;
rhs.CurrentSize = 0;
}
BinomialNode * BinomialHeap::CombineTrees( BinomialNode * T1,
BinomialNode * T2 )
{
if( T2->TellKey() < T1->TellKey() )
return CombineTrees( T2, T1 );
T2->NextSibling = T1->LeftChild;
T1->LeftChild = T2;
return T1;
};
void BinomialHeap::Insert( int x )
{
if (IsFull())
{cout<< "Disk Full!";
return;}
else
{
BinomialHeap OneItem;
OneItem.RootNode[ 0 ] = new BinomialNode(x);//=operator need to be overloaded??;
Merge( OneItem );
CurrentSize++;
};
const int & BinomialHeap::FindMin( ) const
{
if( IsEmpty( ) )
{ cout<<"Empty database.";
return;}
else
{
return RootNode[ FindMinIndex( ) ]->TellKey();
};
int BinomialHeap::FindMinIndex( ) const
{
int MinIndex=0;
for( i = 0; i<MaxTreeNo; i++ )
{
if( RootNode[ i ] != NULL &&
RootNode[ i ]->TellKey() < RootNode[ MinIndex ]->TellKey() )
MinIndex = i;
};
return MinIndex;
};
void BinomialHeap:eleteMin()
{
if( isEmpty( ) )
{ cout<< "Empty Database!!";}
else
{
MinIndex = FindMinIndex( );
BinomialNode * OldRoot = RootNode[ MinIndex ];//can't delete Node
BinomialNode * NextNodeToCopy = OldRoot->LeftChild;//keep record
delete OldRoot;//delete RootNode poier after keeping record of two poer
BinomialHeap * TemHeap; // one tree breaks o n-1 trees, make it o heap
TemHeap->CurrentSize=pow(2,MinIndex)- 1;//??
for( j = MinIndex - 1; j >= 0; j-- ) //"minIndex-1" is the root size of Heap
// and leftmost child is the biggest tree, rightmost is B0
{
TemHeap.RootNode[ j ] = NextNodeToCopy;
NextNodeToCopy = NextNodeToCopy->NextSibling;
TemHeap.RootNode[ j ]->NextSibling = NULL;//otherwise they'll keep
//their oringinal sibling poer
};
};
RootNode[ MinIndex ] = NULL;
CurrentSize--;
Merge( TemHeap );
};
boolean BinomialHeap::IsEmpty( ) const
{
return CurrentSize == 0;
};
boolean BinomialHeap::IsFull( ) const
{
return CurrentSize == Capacity( );
};
void BinomialHeap::ShowMin()
{
if( IsEmpty( ) )
{ cout<< "Already An Empty Database!!";}
else
MinIndex = FindMinIndex( );
cout<< RootNode[MinIndex]->TellKey();
};
void BinomialHeap::MakeEmpty( )
{
CurrentSize = 0;
for( i = 0; i < MaxTreeNo; i++ )
{MakeEmpty( RootNode );};
};
void BinomialHeap::ShowHeap()
{
if (IsEmpty())
{cout<< "Empty Data Base!";}
else
for (i=0; i<MaxTreeNo; i++)
{ ShowNode(RootNode);};
};
void BinomialHeap::ShowNode(BinomialNode * t)
{
if (t !=NULL)
{
cout<< t->TellKey();
ShowNode(t->LeftChild);
ShowNode(t->NextSibling);
};
};
BinomialHeap::Capacity( ) const
{
return pow(2, MaxTreeNo)-1;
};
void BinomialHeap::
MakeEmpty( BinomialNode * & t )
{
if( t != NULL )
{
MakeEmpty( t->leftChild );
MakeEmpty( t->nextSibling );
delete t;
t = NULL;
};
};
Menu :: Menu ( int howmany, char ** TheMenu, void(**thefunctions)(void))
{
NumItems = howmany;
MenuItems = TheMenu;
TheChoices= thefunctions;
TheChoice = 0;
};
Menu::~Menu()
{};
void Menu::display() const
{
clrscr();
cout<< setw(45) <<"Main Menu" <<endl <<endl;
for ( i=0; i<NumItems; i++)
{
cout <<setw(20) <<i+1 <<"." << MenuItems <<endl;
};
cout<< endl;
};
Menu::getchoice()
{
achoice;
cout<< "Enter the Number of your choice:";
cin>> achoice;
while( (achoice<1) || (achoice > NumItems))
{
cout<< endl <<"Please select a number between one and "
<< NumItems <<": ";
cin>> achoice;
};
TheChoice = achoice;
return achoice;
};
boolean Menu:one() const
{
return bool (TheChoice==6);
};
void Menu:oChoice(int & PickedChoice) const//use reference here??
{
(*TheChoices[PickedChoice-1])();//bbb
};
void Menu::typicalquit(void)
{
char YesNo;
clrscr();
cout<< setw(45) <<"Quiting The Program " <<endl <<endl;
cout<< "Are you Sure you want to quit (Y/N) ?";
cin>> YesNo;
YesNo= toupper(YesNo);
while ((YesNo !='Y') && (YesNo!='N'))
{
cout<< "Please enter <Y> or <N>: ";
cin>> YesNo;
YesNo = toupper(YesNo);
};
if (YesNo=='N')
{
TheChoice=0;
cout <<endl <<"Press the <Enter> key to continue: ";
}
else
{
cout << endl <<" Press the <Enter> key to finish up: ";
};
getch();
};
void InsertOne(void)
{
clrscr();
cout<<setw(45) <<" Adding a new data to the Binomial Heap " <<endl <<endl;
if(MyHeap->IsFull())
{ cout <<"Sorry, Disk space full.";}
else
{
ToBeInserted;
cin.ignore();
cout<< "What number do you want to insert?";
cin<< ToBeInserted;
MyBHeap.Insert( ToBeInserted );
};
cout <<endl <<"Press the <Enter> to continue: ";
getch();
};
void FindMin(void)
{
clrscr();
cout<<setw(45) <<"Find the Mininum data... " <<endl;
MyBHeap.ShowMin();
cout <<endl <<"Press the <Enter> to continue: ";
getch();
};
void ExtractMin(void)
{
clrscr();
cout<< setw(45) <<"The Minimum data is..." <<endl <<endl;
MyBHeap.ShowMin();
MyBHeap.DeleteMin();
cout <<endl <<"Press the <Enter> to continue: ";
getch();
};
void ShowHeap(void) //global function ShowHeap has same name as BinomialHeap::ShowHeap()
// it actually just call it
{
MyBHeap.ShowHeap();//what if there are more than one page of data???
cout <<endl <<endl <<"Press the <Enter> key to continue: ";
getch();
};
void DoQuit(void)
{ TheMenu->typicalquit();};