binary tree...

This is a discussion on binary tree... within the C Programming forums, part of the General Programming Boards category; I tried out this dynamic creation of binary tree where in the user has the option of choosing whether the ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    binary tree...

    I tried out this dynamic creation of binary tree where in the user has the option of choosing whether the left child or the right child should be added or not. I just tried it out for the root and creating one left child and one right child. Now the thing which is puzzling me is that though this works fine for this case will this work fine for any number of left children and right children as well. I have used recursion for the same and after going to a certain depth i seem to get lost of where in i am in the loop. So i just tried it out for one case of having a root and one left child and one right child and it worked fine . So can i extend this to general case as well where in i can have n left child and n right child?

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void display(struct node * root);
    void buildtree(struct node **root);
    
    struct node
    {
    
    	struct node * left;
    	int data;
    	struct node * right;
    };
    
    
    
    int main(void)
    {
    	struct node *root;
    	int data;
    
    	root = (struct node *) malloc(sizeof(struct node));
    	printf("\n Enter the data for the root");
    	scanf("\n %d", &data);
    	root->data = data;
    	buildtree(&root);
    
    	printf("\n The tree that you built is");
    	display(root);
    
    	return 0;
    }
    
    void display(struct node * root)
    {
    	if(root != NULL)
    	{
    		display(root->left);
    		printf("\n%d", root->data);
    		display(root->right);
    	}
    }
    
    void buildtree(struct node **root)
    {
    	
    	char ch;
    	int data = 0;
    	struct node *temp = NULL;
    	printf("\n Do you want to add left child");
    	scanf("\n%c", &ch);
    	if(ch == 'y' || ch == 'Y')
    	{
    		temp = (struct node *) malloc(sizeof(struct node));
    		(*root)->left = temp;
    		scanf("\n %d", &data);
    		temp->data = data;
    		buildtree(&temp);
    	}
    	else
    	{
    		(*root)->left = NULL;	
    	}
    
    	printf("\n Do you want to add right child");
    	scanf("\n%c", &ch);
    	if(ch == 'y' || ch == 'Y')
    	{
    		temp = (struct node *) malloc(sizeof(struct node));
    		(*root)->right = temp;
    		scanf("\n %d", &data);
    		temp->data = data;
    		buildtree(&temp);
    	}
    	else
    	{
    		(*root)->right = NULL;	
    	}
    
    };

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Are you trying to create a balanced tree ie where no. of left nodes == no. of right nodes or is it going to be asymmetric.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    It can be asymmetric as well (depends on the user how many left children and right children he wants to create)

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Looking at your program, I think it'll work perfectly alright for any binary tree.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21