c - 二叉树 - 取消引用指针

标签 c

<分区>

我只是想写一个简单的二叉搜索树程序,用户可以在其中插入节点并以中序、前序或后序模式查看树中的所有节点。我的代码是


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

struct treenode
{
int data;
struct treenode *lchild;
struct treenode *rchild;
}*root;

void insertnode(struct treenode *p,int d)
{
    if(p==NULL)
    {
         // means the tree is empty
         p=(struct treenode *)malloc(sizeof(struct treenode));
         p->data=d;
         p->lchild=NULL;
         p->rchild=NULL;
    }

    else
    {
         // start comparing the new data from root
         if( d<p->data )
             insertnode((&(p->lchild)),d);

        else
             insertnode((&(p->lchild)),d);
    }
}

void preorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
    }

    else
    {
        printf("%d",p->data);
        preorder(p->lchild);
        preorder(p->rchild);
    }
}

void postorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
    }

    else
    {
        preorder(p->lchild);
        preorder(p->rchild);
        printf("%d",p->data);
    }
}

void inorder(struct treeode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
    }

    else
    {
        preorder(p->lchild);
        printf("%d",p->data);
        preorder(p->rchild);
    }
}

int main(void)
{
    root=NULL;
    int choice,data;

    while(1)
    {
         printf("\nPress 1 for inserting a node in BST fashion: ");
         printf("\nPress 2 for traversing the tree in preorder fashion :");
         printf("\nPress 3 for traversing the tree in postorder fashion :");
         printf("\nPress 4 for traversing the tree in inorder fashion :");
         printf("\nPress 5 to exit :");


         printf("\nEnter your choice: ");
         scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("\nEnter the data to be inserted:");
            scanf("%d",&data);
            insertnode(root,data);
            break;

        case 2: preorder(root);
            break;

        case 3: postorder(root);
            break;

        case 4: inorder(root);
            break;

        case 5: exit(0);
            break;

        default: printf("\nYou have enetred an invalid choice. Please try again");
    }
}

return 0;
}

有一堆错误信息说


dereferencing pointer to incomplete type

问题是什么?此外,我对双重间接指针不是很满意,所以有人可以解释我如何传递和检索双重间接指针(如果我需要在上面的程序中完全传递它们)。

最佳答案

以下只是编译错误,所以修正这些错误,你的程序就会编译。

问题#1:
您将结构定义为:

struct treenode
{
    /* Blah blah... */
} *root;  /* Mistake: should be root, not *root */

相反,将其命名为 root,而不是 *root


问题#2:
您错误地调用了 insertnode()。而不是这个:

insertnode((&(p->lchild)),d);  /* Mistake: taking the address of the pointer */

你应该这样调用它:

insertnode(p->lchild,d);


问题#3:
您在 main() 中定义 root 错误:

root = NULL;  /* Mistake: root is implicitly declared as int */

你应该做的是:

struct treenode* root = NULL;


问题#4:

你在 inorder() 的声明中有错别字:

void inorder(struct treeode *p)  /* Typo: should be treenode and not treeode */

应该是:

void inorder(struct treenode *p)

下一组问题是程序中的逻辑错误:

问题#5:
insertnode() 中,您总是将新值 d 插入到左节点中。您应该将任一递归 insertnode(p->lchild, d); 调用更改为:

insertnode(p->rchild, d);

取决于你想如何组织你的树。


问题#6:
就像 AndersK 指出的那样,传递的指针 root 在传递给 insertnode() 之后永远不会改变,所以这是一个主要错误。

在你的情况下,当你想改变传递的指针本身(即将它指向另一个地址)而不改变指针本身时,双间接指针是必要的。

您想在 insertnode() 中更改 root,因此添加另一层间接并传递 root 的地址, &root,这样 root 也可以在函数内更改。

相应地,insertnode()的声明应该是:

insertnode(struct treenode** p, int d)

关于c - 二叉树 - 取消引用指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11501016/

相关文章:

c++ - 幂运算符C语言程序

c - 为什么我的 C 转换方法仍然失败?

c - 为什么我们在 getopt() 函数中使用 argc 作为参数?

c - 在 C 中仅使用系统调用打印浮点值

C:转换 A ? B : C 进入 if (A) B else C

c - 下面的格式说明符在做什么?

c - C 中的 fscanf 动态数组?

c++ - 使用 RenderDrawRect 在屏幕上绘制矩形与使用 RenderDrawPoint 填充每个所需像素所需的时间相同吗?

c++ - 条件变量 POSIX 线程 : C/C++

java - C附加到现有的jvm