•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Binary tree with user input data

form user input binary

5 replies to this topic

### #1 bbto

bbto

CC Lurker

• Just Joined
• 5 posts

Posted 12 March 2009 - 12:36 PM

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:
```#include <iostream>
using namespace std;

struct Node{
int value;
};

Node *cons(int x){//create new node
Node *q;
q = new Node;
q->value = x;
return q;
}

void preorderPrint(Node *root );
void postorderPrint(Node *root );
void inorderPrint(Node *root );

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{
if(readtemp->value < root->value){//compare to root value
//left
}
//left - left
}
//left-right
}

}
else{
//right
}
//right - left
}
//right-right
}
}
cin >> x;
}
}

return root;
}

void preorderPrint(Node *root){
if ( root != NULL ){
cout << root->value << " ";
}
}

void postorderPrint(Node *root ){
if ( root != NULL ){
cout << root->value << " ";
}
}

void inorderPrint(Node *root ){
if ( root != NULL ) {
cout << root->value << " ";
}
}

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)
else
}

int main(){
Node *p;

inorderPrint(p);
cout << endl;
postorderPrint(p);
cout << endl;
if(search(4,p))
cout << "found" << endl;
else
system("PAUSE");
}
```

what is the problem with my readListinter function??

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
• 0

### #2 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 March 2009 - 01:21 PM

Look at this tutorial for a good example of non-recursively adding a node: http://forum.codecal...rees-101-a.html
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #3 bbto

bbto

CC Lurker

• Just Joined
• 5 posts

Posted 12 March 2009 - 09:54 PM

Thanks Winged Panther

i have make a change to my non-recursive readList function:
but still have some problems
```Node * readListInter(){
Node* root = NULL;//returning object
Node* temp;
int x;

cout << "enter number (>0 to stop): ";
cin >> x;
while(x>=0){
input = cons(x);
if(root == NULL){//if root is empty
root = input;
temp = root;//temp is use to store value for compare
}
else{
while(input != NULL){
if( x < temp->value){//if smaller x to add to left
input = NULL;//new node added, exit the loop
}
else{//if not empty set temp to subtree
}
}
input = NULL;//new node added, exit the loop
}
else{
}
}
}
}
cin >> x;
}
return root;
}```

the code above work fine with actually 7 input, if more then 7 it become a infinite loop.

The reason of the problem is those codes I'm commented problem.Because it change the temp to 2nd level(root is 1st Level) every time.

But if i change those code to what i comment out , the order of the tree will not correct.
it will depend on the 3rd input, if the 3rd is lesser the root then start from 4th input all the inputs will go into left side. 3rd is greater then every thing goes to right after 4th........

how can i temp only go back one level? like

------5
----/---\
---1-----9
--/--\--/--\
-0---2-7--10
-------------\
-------------11
temp is on 11, how to make it to 10
• 0

### #4 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 March 2009 - 06:27 AM

I've made three changes, but not tested. See how this works:
```Node * readListInter(){
Node* root = NULL;//returning object
Node* temp;
int x;

cout << "enter number (>0 to stop): ";
cin >> x;
while(x>=0){
input = cons(x);
if(root == NULL){//if root is empty
root = input;
temp = root;//temp is use to store value for compare
}
else{
temp = root; //for each new addition, must start at root to find correct spot
while(input != NULL){
if( x < temp->value){//if smaller x to add to left
input = NULL;//new node added, exit the loop
}
else{//if not empty set temp to subtree
temp = temp->lLink;//need to move left from the current position
}
}
input = NULL;//new node added, exit the loop
}
else{
temp = temp->rLink;//need to move right from the current position
}
}
}
}
cin >> x;
}
return root;
}```

• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #5 bbto

bbto

CC Lurker

• Just Joined
• 5 posts

Posted 13 March 2009 - 10:06 PM

Thanks WingedPanther!
it works!

i just focus in side the the else's while loop, forget to look outside

thanks you very much~
• 0

### #6 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 14 March 2009 - 04:47 AM

• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### Also tagged with one or more of these keywords: form, user input, binary

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download