Thread: Help!!!

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    17

    Help!!!

    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;
    };

  2. #2
    lurker
    Guest
    You really don't have too many errors, it's just that the one's you do throw off everything else.
    Code:
    void AVLBST::RightBalance(AVLNode* &r, bool &taller)[
    and
    void AVLBST::LeftBalance(AVLNode* &r, bool &taller)[
    should have a '{' instead of '['
    
    and
    void PrettyPrint( int level);
    doesn't match
    void AVLBST::PrettyPrint(Node* r, int level)
    
    and 
    void RotateRight(AVLNode* p);
    doesn't quite match
    void AVLBST::RotateRight(AVLNode* &p) 
    (the prototype doesn't have the '&')
    Try fixing those, then you should have much fewer errors.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    WOW!

    Well, sorry I'm not sitting at my compiler, so I'm not going to be be much help...

    1- "Help!!!" not a very specific title! (See the FAQ)

    2- I think you need to work on some troubleshooting/debugging techniques. For example, when you first make a function, make an empty shell. Maybe make it display "In function2. x = ...") If there's a return value, make it return some known value. If you test your code as you create it you won't end-up with a whole page of code with errors you can't narrow-down. I compile almost as often as I have something complete enough to compile, and I test every time I have a bit of code that is testable... that is code that actually does something. If I was going to troubleshoot this code, I'd start by comenting-out most of it... then un-commenting one small section at a time 'till everything was OK.

    3- You don't need all the semicolons after each closing brace. But I don't think they are causing any problems. Mostly they are compiled as blank statements. You need that eding-semicolon to end a class or a structure.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    17
    Sorry about the subject I meant to add to it but hit submit too fast- I have read the "read first" -again I apologize. I have done mucho trouble shooting and I found the 2 ['s rather than {....it's just that after awhile you see a big blur. Testing as I go probably would be a HUGE HELP!! Thanks for that suggestion. I have matched up my prototypes with the functions....my last error - I hope is
    This is my function prototype...
    Code:
    void PrettyPrint(AVLNode*  r, int item);
    void PrettyPrintHelper(AVLNode* r, int item);
    This is the function ...
    Code:
    void AVLBST::PrettyPrint(Node* r,  int item) { 
    const int TABSIZE = 10; 
    int i; 
    	if(r != NULL) { 
    		PrettyPrint(r->right, item+1); 
    	for (i=0;i<=item*TABSIZE; i++) { 
    		cout << " "; 
    	}; 
    		cout << r->datum<< endl; 
    	PrettyPrint(r->left, item+1); 
    	}; 
    }
    and I call it by

    Code:
    int main (){
    
    	AVLBST tree;
    	int item;
    		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(r,0);
    		}
    		return 0;
    }
    I am getting the error that r is an undeclared identifier. I declared AVLNode* r in the .h file is that the appropriate place or am I thinking along the wrong line????

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I didn't mention the title because he used code tags, which is a large problem in the last couple days.

Popular pages Recent additions subscribe to a feed