Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compile error, help me to find out

Status
Not open for further replies.

hongweiwu

Programmer
Nov 11, 2001
3
CN
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[] = {&quot;Creat New Heap&quot;, &quot;Insert an Key&quot;, &quot;Extract the minimal&quot;,
&quot;Find the minimal&quot;, &quot;Show the Heap&quot;,
&quot;Quit&quot;};
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<< &quot;Disk Full!&quot;;
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<<&quot;Empty database.&quot;;
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::DeleteMin()
{
if( isEmpty( ) )
{ cout<< &quot;Empty Database!!&quot;;}
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-- ) //&quot;minIndex-1&quot; 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<< &quot;Already An Empty Database!!&quot;;}
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<< &quot;Empty Data Base!&quot;;}
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) <<&quot;Main Menu&quot; <<endl <<endl;
for ( i=0; i<NumItems; i++)
{
cout <<setw(20) <<i+1 <<&quot;.&quot; << MenuItems <<endl;
};
cout<< endl;
};

Menu::getchoice()
{
achoice;
cout<< &quot;Enter the Number of your choice:&quot;;
cin>> achoice;
while( (achoice<1) || (achoice > NumItems))
{
cout<< endl <<&quot;Please select a number between one and &quot;
<< NumItems <<&quot;: &quot;;
cin>> achoice;
};
TheChoice = achoice;
return achoice;
};

boolean Menu::Done() const
{
return bool (TheChoice==6);
};


void Menu::DoChoice(int & PickedChoice) const//use reference here??
{
(*TheChoices[PickedChoice-1])();//bbb
};


void Menu::typicalquit(void)
{
char YesNo;
clrscr();
cout<< setw(45) <<&quot;Quiting The Program &quot; <<endl <<endl;
cout<< &quot;Are you Sure you want to quit (Y/N) ?&quot;;
cin>> YesNo;
YesNo= toupper(YesNo);
while ((YesNo !='Y') && (YesNo!='N'))
{
cout<< &quot;Please enter <Y> or <N>: &quot;;
cin>> YesNo;
YesNo = toupper(YesNo);
};
if (YesNo=='N')
{
TheChoice=0;
cout <<endl <<&quot;Press the <Enter> key to continue: &quot;;
}
else
{
cout << endl <<&quot; Press the <Enter> key to finish up: &quot;;
};
getch();
};


void InsertOne(void)
{
clrscr();
cout<<setw(45) <<&quot; Adding a new data to the Binomial Heap &quot; <<endl <<endl;
if(MyHeap->IsFull())
{ cout <<&quot;Sorry, Disk space full.&quot;;}
else
{
ToBeInserted;
cin.ignore();
cout<< &quot;What number do you want to insert?&quot;;
cin<< ToBeInserted;
MyBHeap.Insert( ToBeInserted );
};
cout <<endl <<&quot;Press the <Enter> to continue: &quot;;
getch();
};

void FindMin(void)
{
clrscr();
cout<<setw(45) <<&quot;Find the Mininum data... &quot; <<endl;
MyBHeap.ShowMin();
cout <<endl <<&quot;Press the <Enter> to continue: &quot;;
getch();
};


void ExtractMin(void)
{
clrscr();
cout<< setw(45) <<&quot;The Minimum data is...&quot; <<endl <<endl;
MyBHeap.ShowMin();
MyBHeap.DeleteMin();
cout <<endl <<&quot;Press the <Enter> to continue: &quot;;
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 <<&quot;Press the <Enter> key to continue: &quot;;
getch();
};

void DoQuit(void)
{ TheMenu->typicalquit();};


 
Two things, one put your code is the TGML code brackets. See the link at the bottom marked &quot;Process TGML&quot; for more help. It'll make your code easier to read.

Second, what exactly were the compiler errors?
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
What's TGML? I know the compiler I used is pretty old version of Borland
Main error is improper usage of BinomialNode, it said out that either I declare the BinomialNode in a wrong way or misspelling, I am sure it's not misspelling, but dont know how I declare or defined it wrongly.
 
TGML is the markup language used at this site site. See below. I'll see if I can track down the error...
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top