简体   繁体   English

我试图实现一棵二叉树并按预定顺序遍历它,但它仅在显示 function 中显示 0,并且创建 function 正在工作

[英]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> #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 ** **创建 function 正在工作**

    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 /* OUTPUT 你想创建(0)还是显示(1)一棵树 0

enter the value or -1 for no node 12输入值或 -1 表示没有节点 12

enter the left child of 12 enter the value or -1 for no node 13输入 12 的左子节点 输入值或 -1 表示没有节点 13

enter the left child of 13 enter the value or -1 for no node -1输入 13 的左子节点 输入值或 -1 表示没有节点 -1

enter the right child of 13 enter the value or -1 for no node -1输入 13 的右子节点 输入值或 -1 表示没有节点 -1

enter the right child of 12 enter the value or -1 for no node 15输入 12 的右子节点 输入值或 -1 表示没有节点 15

enter the left child of 15 enter the value or -1 for no node -1输入 15 的左子节点 输入值或 -1 表示没有节点 -1

enter the right child of 15 enter the value or -1 for no node -1输入 15 的右子节点 输入值或 -1 表示没有节点 -1

do you want to proceed (1/0)1你想继续吗 (1/0)1

do you want to create(0) or display(1) a tree 1你想创建(0)还是显示(1)一棵树 1

pre order is 0 do you want to proceed (1/0) */预购是 0 你想继续吗 (1/0) */

In create you assign a newly allocated node to newnode which is a global variable.create中,您将新分配的节点分配给newnode ,它是一个全局变量。 That means that in each instance of the function, it's writing to the same variable, overwriting whatever was there.这意味着在 function 的每个实例中,它写入同一个变量,覆盖那里的所有内容。

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. newnode应该声明为 local 以create ,并且root应该声明在main function 中。

Also, you have a memory leak in the case where you enter -1 for a particular node.此外,如果您为特定节点输入 -1,则会出现 memory 泄漏。 To fix this, move the code that allocates the new node after reading the value, into the else section.要解决此问题,请将读取值后分配新节点的代码移至else部分。

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.问题不在于显示 function 认为你犯了一个小错误,有些人可能会说这不是一个错误,因为编译器接受它并做了它应该做的事情。 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.!!当您检查指针的值时,最好使用关键字 NULL,而不是 0。!! root==NULL or root!=NULL. root==NULL 或 root!=NULL。

Now about your create function.现在关于您创建 function。 The global newNode when combined with recursion won't work properly!!!与递归结合使用的全局 newNode 将无法正常工作!!! The structure of your tree is messed up because of the create and this is the reason the display doesn't work!!!由于创建,您的树的结构搞砸了,这就是显示不起作用的原因!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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