二叉树 – 解除引用指针

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


#include  #include  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( ddata ) 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->lchild, d); 来电:

 insertnode(p->rchild, d); 

取决于您希望如何组织树。

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

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

你想在insertnode()里面改变root ,所以添加另一个间接层并传递root的地址, &root ,这样root也可以在函数内改变。

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

 insertnode(struct treenode** p, int d) 

为了更改指针指向的数据,您需要提供一个更多级别的间接

比如你写的

 insertnode(root,data); 

但是root在开始时设置为NULL,并且不能像insertnode一样在函数中提供它。

而是将insertnode声明为

 insertnode(struct treenode **p,int d) 

并调用insertnode

 insertnode(&root, data);