GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2006, 00:48
ajohn ajohn is offline
New Member
 
Join Date: Nov 2006
Posts: 10
ajohn is on a distinguished road

Trees


Hello everybody. I have made a simple program of ceation binary search tree. The program is not displaying all the contents of the tree, just displaying the root node. I am not able to fix where the problem actually lies.
CPP / C++ / C Code:
#include<stdio.h>
struct node
{
	struct node *left;
	int value;
	struct node *right;
};

void create_tree(struct node *node_ptr, struct node *new_node)
{
	if(node_ptr== NULL)
		node_ptr= new_node;
	else if (new_node->value <= node_ptr->value)
		create_tree(node_ptr->left , new_node);
	else if((new_node->value) > (node_ptr->value))
		create_tree(node_ptr->right,new_node);
}

void preorder(struct node *node_ptr)
{
  printf("%d",node_ptr->value);
  if(node_ptr->left!=NULL)
	preorder(node_ptr->left);

  if(node_ptr->right!=NULL)
	preorder(node_ptr->right);
}

int main()
{
   int i=1;
   char choice='y';
   struct node *root=NULL;

   struct node *new_node=(struct node*)malloc(sizeof(struct node));
   new_node->left=new_node->right=NULL;

   printf("\n Enter the value of the %d first node",i);
   scanf("%d",&new_node->value); 
   root=new_node;
/* first node is filled and taken as the root node*/
 
printf("\nDo you want to create more nodes");
	scanf("%c",&choice);     
while((choice=='y')||(choice=='Y'))
{
        i=i+1;

        new_node=(struct node*)malloc(sizeof(struct node));
        new_node->left=new_node->right=NULL;

        printf("\n Enter the value of the",i," %d th node");
	scanf("%d",&new_node->value);      
        create_tree(root,new_node);

        printf("\nDo you want to create more nodes");
	scanf("%c",&choice);   }

preorder(root);
return 0;

}
  #2  
Old 14-Nov-2006, 12:24
mathematician mathematician is offline
Member
 
Join Date: Nov 2006
Location: Shrewsbury Uk
Posts: 133
mathematician will become famous soon enough

Re: Trees


The scanf function is leaving a '\n' character behind in the buffer, which is being retrieved by the second call to scanf. So when you test for choice == 'y' it fails (because choice == '\n').

Instead of scanf( "%c", &choice ); try choice = getch();
  #3  
Old 14-Nov-2006, 12:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Trees


Quote:
Originally Posted by mathematician
The scanf function is leaving a '\n' character behind in the buffer, which is being retrieved by the second call to scanf. So when you test for choice == 'y' it fails (because choice == '\n').

Instead of scanf( "%c", &choice ); try choice = getch();
No, the proper command is getchar(); because getch(); is not portable. You will still have to erase the new line, though. See this for a way to handle this problem, as well as other links about the dreaded scanf()...
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Trees Problem emC++ C++ Forum 3 13-Aug-2006 08:33
Question about Binary trees mia C++ Forum 0 25-May-2006 11:10
Trees and Keys (not for the faint-hearted!) greymalkin C++ Forum 11 08-Nov-2005 08:04
Search Binary Trees and I/O files Krandygrl00 C++ Forum 1 20-Jul-2005 08:11
folder problem in trees zuzupus MySQL / PHP Forum 23 26-Sep-2003 07:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:46.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.