Home > Net >  I tried to implement a binary tree and traverse it in pre-order but it is showing 0 only in display
I tried to implement a binary tree and traverse it in pre-order but it is showing 0 only in display

Time:01-27

#include<stdio.h> #include<stdlib.h>

//diclaration
struct node* create();
 struct node
    {
        int data;
        struct node *left,*right;

    };
    struct node *root,*newnode;
    void preorder(struct node*root);

**the create function is working **

    struct node* create()
    {
        
        int x;
        //newnode=(struct node*)malloc(sizeof(struct node));
        newnode=(struct node*)malloc(sizeof(struct node));
        printf("\t\nenter the value or -1 for no node ");
        scanf("%d",&x);
        if(x==-1)
        {return 0;}
        else
        {
            newnode->data=x;
            printf("\t\nenter the left child of %d ",x);
            newnode->left=create();
            printf("\t\nenter the right child of %d ",x);
            newnode->right=create();
            return newnode;


        }
    }

**but the display is not working properly **

void preorder(struct node*root)
{
    if(root==0)
    { return;}
    else
    {
        printf("%d ",root->data);
        preorder(root->left);
        preorder(root->right);
    }
}


 void main()
{

//root=0;
//root=create();
int f;
    do{
        int c;
        
        printf("\t\ndo you want to create(0) or display(1)  a tree ");
        scanf("%d",&c);
        switch(c)
            {
                case 0: 
                root=0;
                root=create();
                break;
                case 1: printf("\t\npre order is ");
                        preorder(root);
                break;
                default:printf("\t\nenter a valid number");
                break;
            
                
            }   
        
        
        printf("\t\ndo you want to proceed (1/0)");
        scanf("%d",&f);
        
      }while(f==1);
    
    
}

/* OUTPUT do you want to create(0) or display(1) a tree 0

enter the value or -1 for no node 12

enter the left child of 12 enter the value or -1 for no node 13

enter the left child of 13 enter the value or -1 for no node -1

enter the right child of 13 enter the value or -1 for no node -1

enter the right child of 12 enter the value or -1 for no node 15

enter the left child of 15 enter the value or -1 for no node -1

enter the right child of 15 enter the value or -1 for no node -1

do you want to proceed (1/0)1

do you want to create(0) or display(1) a tree 1

pre order is 0 do you want to proceed (1/0) */

CodePudding user response:

In create you assign a newly allocated node to newnode which is a global variable. That means that in each instance of the function, it's writing to the same variable, overwriting whatever was there.

Because of issues like this, the use of global variables should be avoided. newnode should be declared local to create, and root should be declared in the main function.

Also, you have a memory leak in the case where you enter -1 for a particular node. To fix this, move the code that allocates the new node after reading the value, into the else section.

  •  Tags:  
  • Related