please solve this error...

Hi..

I am getting a error in the following code for AVL trees(HIGHLIGHTED CODE).. I tried in the internet for the error, but not able to resolve..CAn you help me with this...?And i am getting the following error..

avl2.cpp:84: error: expected constructor, destructor, or type conversion before '*' token


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;
class AVL
{
public:

struct AVLNode
{
char Name[50];
int Balance;
AVLNode *LeftChild;
AVLNode *RightChild;
};

struct fig
{
int breadth;
int pluscounter;
string mat;
}*f[200];

AVL()
{
}
~AVL()
{
}

AVLNode *InputData(FILE *);
AVLNode *InsertData(AVLNode *, AVLNode *);

int FindBalance(AVLNode *);
int max( int, int);
void headassign(FILE *);

void PreorderTraversal(AVLNode *,int);

AVLNode *LeftLeft(AVLNode *);
AVLNode *RightRight(AVLNode *);
AVLNode *LeftRight(AVLNode *);
AVLNode *RightLeft(AVLNode *);

int j,c;
int count;

};

//bool h;
//AVLNode * erase(char*);
//AVLNode * _erase(char*, AVLNode *);
//AVLNode *K1, *K2, *K3, *newnode;

int main(int argc,char *argv[])
{
AVL *a=new AVL();
FILE *fp;
fp=fopen(argv[1],"r"); //attempts to open read file, and tests for existence
if(fp == NULL)
{
printf("Unable to open output file!\n");
fclose(fp);
}
else
{
a->headassign(fp);

cout << "\n\nPreorder:\n\n";

cout << endl;
}
}
void AVL :: headassign(FILE * fp)
{
AVLNode *head;
head=InputData(fp);
PreorderTraversal(head,0);
}

AVLNode * AVL :: InputData(FILE *fp)
{
char data[100];
AVLNode *Root = NULL;
AVLNode *Leaf;
while(!feof(fp))
{

fscanf(fp,"%s",data);
if(feof(fp))
{
break;
}
Leaf = new AVLNode;
strcpy(Leaf->Name,data);
Leaf->Balance = 0;
Leaf->RightChild = Leaf->LeftChild = NULL;
if(Root == NULL)
Root = Leaf;
Root = InsertData(Leaf, Root);
}
return Root;
}

AVLNode* AVL :: InsertData(AVLNode *Leaf, AVLNode *Root)
{
if(Root == NULL)
{
c=0;
count=0;
j=0;
return Leaf;
}
else if(strcmp(Leaf->Name,Root->Name)<0)
{
c=0;
count=0;
j=0;
Root->LeftChild = InsertData(Leaf, Root->LeftChild);
if(FindBalance(Root->LeftChild) - FindBalance(Root->RightChild) == 2)
{
if(strcmp(Leaf->Name,Root->LeftChild->Name)<0)
Root = LeftLeft(Root);
else
Root = LeftRight(Root);
}
}
else if(strcmp(Leaf->Name,Root->Name)>0)
{
c=0;
count=0;
j=0;
Root->RightChild = InsertData(Leaf, Root->RightChild);
if(FindBalance(Root->RightChild) - FindBalance(Root->LeftChild) == 2)
{
if(strcmp(Leaf->Name,Root->RightChild->Name)>0)
Root = RightRight(Root);
else
Root = RightLeft(Root);
}
}
Root->Balance = max(FindBalance(Root->LeftChild), FindBalance(Root->RightChild)) + 1;
return Root;
}

int AVL :: FindBalance(AVLNode *Root)
{
if(Root == NULL)
return -1;
else
return Root->Balance;
}

int AVL :: max( int a, int b )
{
return a > b ? a : b;
}

AVLNode* AVL :: LeftLeft(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->LeftChild;
Rotate->LeftChild = Pivot->RightChild;
Pivot->RightChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) + 1;
Pivot->Balance = max(FindBalance(Pivot->LeftChild), FindBalance(Rotate->RightChild)) + 1;
return Pivot;
}

AVLNode* AVL :: RightRight(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->RightChild;
Rotate->RightChild = Pivot->LeftChild;
Pivot->LeftChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) + 1;
Pivot->Balance = max(FindBalance(Pivot->RightChild), FindBalance(Rotate->LeftChild)) + 1;
return Pivot;
}

AVLNode* AVL :: LeftRight(AVLNode *RotateTop)
{
RotateTop->LeftChild = RightRight(RotateTop->LeftChild);
return LeftLeft(RotateTop);
}

AVLNode* AVL :: RightLeft(AVLNode *RotateTop)
{
RotateTop->RightChild = LeftLeft(RotateTop->RightChild);
return RightRight(RotateTop);
}



use class scope: (missing part in bold)

AVL:: AVLNode * AVL :: InputData(FILE *fp)

similarly for the rest
AVL:: AVLNode* AVL :: LeftLeft(AVLNode *Rotate)

1
2
3
4
.
.
.
AVL:: AVLNode* AVL :: LeftRight(AVLNode *RotateTop)


and have defination for PreorderTraversal(head,0); your programs will run without any liking errors.
Last edited on
Topic archived. No new replies allowed.