Thread: binary tree problem - help needed

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

    binary tree problem - help needed

    respected sir,

    i would be greatful if anyone could help me with my non recursive
    binary tree problem.actually i had already got some help from u.thanks for that.


    the problem is when u give data in either order it gets printed
    meaning acending or descending ie either on left side or right side
    but if u give in between values the value does not get printed.
    like for example

    10,15,20,13
    here 13 is inbetween it does not get printed.
    13 does not get printed, the remaining elements
    get printed.

    can anyone tell me what's wrong with the code,
    one problem i found is
    in if (temp->right) and the else at times
    both get satisfied is that the problem.
    is that the problem ...??? i am attaching my code

    i would be greatful if anyone could point out the problem.

    thanks


    sanju





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

  2. #2
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    This will be of no help whatsoever, but what is a binary tree?

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, if you insert those number in a regular binary tree, it will look something like this
    Code:
        10
          \
           15
          /  \
        13    20
    Follow your loops and see if you think you are inserting and printing correctly (you're definately not printing correctly, I haven't followed the insertion process.)


    What is a binary tree... see here. Also see here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    What is a binary tree... see here. Also see here.
    Thanks Hammer!

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

    thanks any way

    respected sir
    thanks any way for the guidance,

    i think i am able to figure out the problem.
    bye

    sanju

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  2. Problem with binary search tree :(
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-01-2002, 10:31 PM
  3. problem in binary expression tree
    By hanij in forum C Programming
    Replies: 6
    Last Post: 04-28-2002, 08:31 AM
  4. binary tree node structure
    By Kirsten in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 08:02 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM