Hi, everyone
I'm writing a program(lab assignment), which i need to build a binary tree with user input form keyboard. and output in post order and in-order.
i'm fine with output function, but have big problem on reading user input.
for the read input function i have to make a recursive, and anon recursive
but i cant even work out the non recursive one...
here is my code so far:
what is the problem with my readListinter function??Code:#include <iostream> using namespace std; struct Node{ Node *lLink; int value; Node *rLink; }; Node *cons(int x){//create new node Node *q; q = new Node; q->lLink = NULL; q->value = x; q->rLink = NULL; return q; } Node * readListInter();// read input date and bulid a ninary tree void preorderPrint(Node *root ); void postorderPrint(Node *root ); void inorderPrint(Node *root ); Node * readListInter(){ Node *readtemp; Node *left, *right,*root; root = NULL; int x; cout << " enter number(>=0 to stop): "; cin >>x; while(x>=0){ if(root == NULL){ root = cons(x); left = root; right = root; } else{ readtemp = cons(x); if(readtemp->value < root->value){//compare to root value //left if(left->lLink==NULL){ left->lLink = readtemp; left = readtemp; } else if(readtemp->value < left->value){ //left - left left->lLink = readtemp; left = readtemp; } else if(readtemp->value >= left->value){ //left-right left->rLink = readtemp; left = readtemp; } } else{ //right if(right->lLink==NULL){ right->lLink = readtemp; right = readtemp; } else if(readtemp->value < right->value){ //right - left right->lLink = readtemp; right = readtemp; } else if(readtemp->value >= right->value){ //right-right right->rLink = readtemp; right = readtemp; } } cin >> x; } } return root; } void preorderPrint(Node *root){ if ( root != NULL ){ cout << root->value << " "; preorderPrint( root->lLink ); preorderPrint( root->rLink ); } } void postorderPrint(Node *root ){ if ( root != NULL ){ postorderPrint( root->lLink ); postorderPrint( root->rLink ); cout << root->value << " "; } } void inorderPrint(Node *root ){ if ( root != NULL ) { inorderPrint( root->lLink ); cout << root->value << " "; inorderPrint( root->rLink ); } } bool search(int x, Node *root){ if(root==NULL) return false;//empty tree else if(x == root->value) return true; else if(x < root->value) return search(x, root->lLink); else return search(x, root->rLink); } int main(){ Node *p; p = readListInter(); inorderPrint(p); cout << endl; postorderPrint(p); cout << endl; if(search(4,p)) cout << "found" << endl; else cout << "not found" << endl; system("PAUSE"); }
also the instructor said some that i can use
cin.claer
cin.ignore(numeric_limits<streamsize>::max(),'\n') ;
for the read input function, instead of assign a number to stop inputting
but he didn't say anything abot this two code
and it is my first time see it....
please help me with this, this lab will not be mark mark but my midterm is coming soon , so i would like to understand this.
sorry for my poor english


LinkBack URL
About LinkBacks




Reply With Quote



Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum