I am having a hard time with my program. It is to add numbers to a AVLBST and then print it out. Everytime I try to compile it I get different errors- 1st it was I had an obsolete function and then it was that I had undeclared identifiers....any help/suggestions/arrows to what any help=) would be soooo appreciated!!!
Here is my main
Code:
/avlbst.cpp
#include <string>
#include<iostream>
#include "avlbst.h"
using namespace std;
int main (){
AVLBST tree;
int item;
int level;
cout<< "Enter the item that you would like to place in the Binary Search Tree, 000 to quit "<< endl;
cin >> item;
while (cin != 000 ){
tree.AVLInsert(item);
tree.PrettyPrint(level);
}
return 0;
}
Here are my functions....
Code:
void AVLBST::AVLInsert(int item){
bool taller;
AVLInsertHelper(root, item, taller);
}
void AVLBST::AVLInsertHelper(AVLNode* &r, int item, bool &taller){
bool tallersubtree;
if (r==NULL){
AVLNode* p =new AVLNode;
p->datum= item;
p->left = NULL;
p->right = NULL;
p-> bf= EH;
r= p;
}else{
if( item < r->datum){
AVLInsertHelper(r->left, item, tallersubtree);
if (tallersubtree){
switch (r->bf){
case LH: LeftBalance(r,taller);
break;
case EH: r->bf=LH;
taller = true;
break;
case RH: r->bf=EH;
taller= false;
break;
};
};
};
if (item > r->datum){
AVLInsertHelper(r->right, item, tallersubtree);
if (tallersubtree){
switch (r->bf){
case RH: RightBalance(r, taller);
break;
case EH: r->bf=RH;
taller = true;
break;
case LH: r->bf= EH;
taller= false;
break;
};
};
};
};
}
void AVLBST::RotateLeft(AVLNode* &p){
AVLNode* temp;
if (p== NULL){
cout<<"Error"<<endl;
};
if(p!= NULL){
if (p->right == NULL){
cout <<"Error"<<endl;
};
if(p->right != NULL){
temp= p->right;
p->right=temp->left;
temp->left=p;
p=temp;
};
};
}
void AVLBST::RotateRight(AVLNode* &p){
AVLNode* temp;
if (p== NULL){
cout<<"Error"<<endl;
};
if(p!= NULL){
if (p->left == NULL){
cout <<"Error"<<endl;
};
if(p->left != NULL){
temp= p->left;
p->left=temp->right;
temp->right=p;
p=temp;
};
};
}
void AVLBST::RightBalance(AVLNode* &r, bool &taller)[
AVLNode* x;
AVLNode* w;
x=r->right;
switch(x->bf){
case RH:
r->bf=EH;
x->bf=EH;
RotateLeft(r);
taller=false;
break;
case EH:
cout << "Error"<< endl;
break;
case LH:
w=x->left;
switch(w->bf){
case EH:
r->bf=EH;
x->bf=EH;
break;
case LH:
r->bf=EH;
x->bf=RH;
break;
case RH:
r->bf=LH;
x->bf=EH;
break;
};
w->bf=EH;
RotateRight(x);
r->right=x;
RotateLeft(r);
taller=false;
break;
};
}
void AVLBST::LeftBalance(AVLNode* &r, bool &taller)[
AVLNode* x;
AVLNode* w;
x=r->left;
switch(x->bf){
case LH:
r->bf=EH;
x->bf=EH;
RotateRight(r);
taller=false;
break;
case EH:
cout << "Error"<< endl;
break;
case RH:
w=x->left;
switch(w->bf){
case EH:
r->bf=EH;
x->bf=EH;
break;
case LH:
r->bf=EH;
x->bf=RH;
break;
case RH:
r->bf=LH;
x->bf=EH;
break;
};
w->bf=EH;
RotateLeft(x);
r->left=x;
RotateRight(r);
taller=false;
break;
};
}
void AVLBST::PrettyPrint(Node* r, int level) {
const int TABSIZE = 10;
int i;
if(r != NULL) {
PrettyPrint(r->right, level+1);
for (i=0;i<=level*TABSIZE; i++) {
cout << " ";
};
cout << r->datum<< endl;
PrettyPrint(r->left, level+1);
};
}
My .h file is
Code:
enum BalanceFactor{LH,EH,RH};
struct AVLNode{
AVLNode* left;
int datum;
AVLNode* RIght;
BalanceFactor bf;
};
class AVLBST{
public:
AVLBST();//constructor of empty tree
void RotateLeft(AVLNode* &p);
void RightBalance(AVLNode* &r, bool &taller);
void RotateRight(AVLNode* p);
void LeftBalance(AVLNode* &r, bool &taller);
void AVLInsert(int);
void AVLInsertHelper(AVLNode* &r, int, bool &);
AVLNode* Search(int);
AVLNode* SearchHelper(AVLNode* &r, int);
void InOrder();
void InOrderHelper(AVLNode* &r);
void PrettyPrint( int level);
void PrettyPrintHelper(AVLNode* r, int level);
private:
AVLNode* root;
};