简体   繁体   中英

I tried to implement a binary tree and traverse it in pre-order but it is showing 0 only in display function, and the create function is working

#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) */

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.

The problem is not the display function thought you made one small mistake that some may say it isn't a mistake just because the compiler accepts it and does what it should do. The small mistake you made is the check up of the pointer. When you check the value of a pointer it's better to use the keyword NULL, not 0.!! root==NULL or root!=NULL.

Now about your create function. The global newNode when combined with recursion won't work properly!!! The structure of your tree is messed up because of the create and this is the reason the display doesn't work!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM