Thread: binary tree solution-- help needed

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    binary tree solution-- help needed

    respected sir,

    this is a non recursive solution for a binary tree.but the code
    seems to have some problem,
    even after debugging i am not able to find out the problem.
    the problem is

    while printing the elements only the root is getting printed,the other elements are not getting printed.
    i would be greatful if anyone could suggest a solution.

    the code
    Code:
    #include<stdio.h>
    struct btree
    {
    	int data;
    	struct btree *right;
    	struct btree *left;
    };
    void main()
    {
    	char ch;
    	struct btree *temp,*start,*p;
    	struct btree *disp;
    	start = temp = p = NULL;
        
      do
        {
    		p=(struct btree *)malloc(sizeof(struct btree));/* new node */
    
    		printf("ENTER THE NODE VALUE  ");
    		scanf("%d",&(p->data));
    		p->left = NULL;
    		p->right = NULL;
    	
    	if (start == NULL)		/* for the very first time */
    		{
    			start = p;
    			printf("ROOT NODE IS  %d",start->data);
            	}
        else
    	{   
    	    temp = start;
    		while(1 && temp)   /* traversing till the end of left and right pointers */
          	  {
    		
    		if ((p->data) < ((temp->data) && (temp->left != NULL)))		
    			 {
    					temp = temp->left;
    			 }
    	    else if ((p->data) >= ((temp->data) && ( temp->right != NULL)))
    			 {
    					temp= temp->right;
    			 
    			 }
    		else
    			{
    				break;
    			}
          }
       }	
    		
    			/* inserting the node in the below statements */
    		temp=(struct btree *)malloc(sizeof(struct btree));
    					if (p->data < temp->data)
    						{
    							temp->left = p;
    						}
    					else
    						{
    						   temp->right = p;
    						
    						}
    		
    	
    	
    	printf("The elements of the tree \n");
    	for(disp=start;disp != NULL;disp=disp->right)	
    			{
    			printf("%d\n",disp->data); /* printing elements */
    			}
    			
    	printf("type y if  u want to continue ");
    	scanf("%s",&ch);
    }while(ch=='y');
      
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well as I suspected your problem was with the way you were inserting new leaves into the tree. Although the way you were printing leaves is non-recursive it also doesn't fully print out the tree. But that sounds like a different problem that I think you can fix yourself. Here is some working code:

    Code:
    #include<stdio.h>
    struct btree
    {
    	int data;
    	struct btree *right;
    	struct btree *left;
    };
    
    
    int main(void)
    {
    	char ch, done;
    	struct btree *temp,*start,*p;
    	struct btree *disp;
    	start = temp = p = NULL;
    
    	do {
    		p=(struct btree *)malloc(sizeof(struct btree));/* new node */
    		done = 0;
    
    		printf("ENTER THE NODE VALUE  ");
    		scanf("%d",&(p->data));
    		p->left = NULL;
    		p->right = NULL;
    
    		if (start == NULL) {	/* for the very first time */
    			start = p;
    			printf("ROOT NODE IS  %d",start->data);
    	    } else {
    			temp = start;
    			while(!done) {   /* traversing till the end of left and right pointers */
    				if ((p->data) < ((temp->data))) {
    					if(temp->left)
    						temp = temp->left;
    					else {
    						temp->left = p;
    						done = 1;
    					}
    				} else if ((p->data) >= ((temp->data))) {
    					if(temp->right)
    						temp = temp->right;
    					else {
    						temp->right = p;
    						done = 1;
    					}
    				} else {
    					break;
    				}
    		  	}
    		}
    
    		printf("The elements of the tree \n");
    		for(disp=start->left;disp != NULL;disp=disp->left) {
    			printf("%d\n",disp->data); /* printing elements */
    		}
    		printf("%d\n", start->data);
    		for(disp=start->right;disp != NULL;disp=disp->right) {
    			printf("%d\n",disp->data); /* printing elements */
    		}
    
    		printf("type y if  u want to continue ");
    		scanf("%s",&ch);
    	} while(ch=='y');
    }

  3. #3
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71

    Post

    Are you sure about those contitionals below 'while (1 && temp)' (hmm.)? What about



    if ((p->data < temp->data) && (temp->left != NULL))

    ...

    instead of



    if ((p->data) < ((temp->data) && (temp->left != NULL)))

    ...



    ?

    I'm not sure, but it might be the thing, otherwise i'd recomend you debugging it again )

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually those didn't even contribute to the problem although lobo is certainly correct that the syntax was a little suspect. Real quick, (assuming you are a newbie, sanju) statements like

    Code:
    while(1 && temp)
    do not technically interfere with any loop. However what you are saying is:

    "while one is one and while temp is not equal to zero"

    Since one is always going to be one it is safe to puts your loops like this

    Code:
    while(temp)
    //or alternatively
    while(temp != NULL)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple binary search tree?
    By sybariticak47 in forum C++ Programming
    Replies: 8
    Last Post: 08-09-2007, 03:53 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. binary search tree help
    By noob2c in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2003, 02:51 PM
  4. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 02:48 PM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 04:08 PM