Thread: Unable to perfrom inorder traversal in a binary tree in C

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    Unable to perfrom inorder traversal in a binary tree in C

    Following is the code for inserting elements in a tree and then retrieving them back using inorder traversal technique.......
    elements are getting inserted just fine,but the code doesn't displays the elements of the tree while performing inorder traversal...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct node{
       int data;
       struct node *right;
       struct node *left;
       }*z,*t,*root=NULL,*x=NULL,*y=NULL;
    
    
    void inorder_visit(struct node *temp);//function to traverse tree with inorder technique
    
    
    void insert_node(struct node *temp,struct node *z);//function to insert elements in a tree
    
    
    int main(){
    int n,i;
    for(i=0;i<5;++i){
    printf("enter the data:");
    scanf("%d",&n);
    
    
    z=(struct node*)malloc(sizeof(int));
    
    
    if(z==NULL)
        printf("sorry!");
    else{
        z->data=n;
        z->left=NULL;
        z->right=NULL;
    
    
    
    
        t=root;
        insert_node(t,z);
        }
    }
    
    
    t=root;
    inorder_visit(t);
    
    
    
    
    return 0;
    }
    
    
    void inorder_visit(struct node *temp){
    if(temp!=NULL){
    inorder_visit(temp->left);
    printf("%d\t",temp->data);
    inorder_visit(temp->right);
    }
    }
    
    
    void insert_node(struct node *temp,struct node *z){
        if(temp==NULL)
            temp=z;
        else {
                if(z->data<=temp->data)
            insert_node(temp->left,z);
        else
            insert_node(temp->right,z);}
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    elements are getting inserted just fine
    Are you sure?




    Code:
    z=(struct node*)malloc(sizeof(int));
    Your size is wrong here. You should also avoid casting malloc in C.




    Code:
    t=root;
    This simply sets t to NULL all the time destroying whatever it may have been pointing at. You probably shouldn't be using the variable t at all. I'd stick to just using your root variable in place of t.




    Code:
    void insert_node(struct node *temp,struct node *z);
    
    ...
    
    insert_node(t,z);
    Like any function in C meant to alter the value of the variable passed into it (and have the changed value persist after the function call), you must pass in a pointer to what you need changed... even if what you are passing in is already in effect a pointer. You are essentially trying to change root (forget about t here). If you expect the value of the pointer root to be altered by the call to insert_node, then that pointer must be passed by pointer. This means that this parameter must be a pointer to a pointer (double pointer).

    There is a way around this by having the function return the pointer passed in which you then immediately reassign to itself so that the call (in main at least) would look like this:
    Code:
    root = insert_node(root,z);
    ...you'd still need to make similar changes to the recursive calls.



    Making the above/below changes I was able to get the code to compile and run and print the list (inorder) without issues:
    • Correct size parameter for the malloc call
    • Get rid of variable t, use root in its place
    • Make insert_node take a pointer to a pointer for the first argument (along with other associated changes needed by this alteration)
    Last edited by hk_mp5kpdw; 01-16-2014 at 08:09 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah you got it back-to-front.
    The in-order traversal code is exactly right but the insertion is quite wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set traversal in Binary Search Tree
    By Nurlana in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2013, 01:08 PM
  2. binary tree traversal
    By nimitzhunter in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2011, 07:13 PM
  3. Replies: 5
    Last Post: 11-17-2007, 03:47 PM
  4. InOrder traversal : show tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-17-2001, 10:38 PM