error-handling - 二进制搜索:please tell me whats wrong here

标签 error-handling binary-tree

我尝试了二叉树数据结构,但发现它无法正常工作并给出错误。请更正我的代码。谢谢!

它发出警告,但是在main中输入后,它将停止运行。

#include<stdlib.h>
#include<stdio.h>
typedef struct
{
    int item;
    struct node * leftc;
    struct node * rightc;
}node;

void create(int key, node **tree )
{
    if(*tree ==0)
    {
        (*tree)= (node *)malloc(sizeof(node *));
        (*tree)->item=key;
        (*tree)->leftc=((*tree)->rightc)=NULL;
    } 
    else
{
    if(key >= (*tree)->item )
    {
        create(key, &((*tree)->rightc));
    }
    else if(key<(*tree)->item)
    {
        create(key, &((*tree)->leftc));
    }
}
}

node * search(int key, node * tree)
{
    if(tree !=NULL)
    {
      if(key == tree->item)
        return tree;
      else if(key > tree->item)
        search(key, tree->rightc);
      else
        search(key, tree->leftc);
    }
  return NULL;
}

void cut(node * tree)
{
  if(tree != NULL)
  {
    cut(tree->leftc);
    cut(tree->rightc);
    free(tree);
  }
}

void print_preorder(node * tree)
{
if (tree) {
 printf("%d\n",tree->item);
 print_preorder(tree->leftc);
 print_preorder(tree->rightc);
}
}


int main()
{
   node * root=NULL;
 create(9,&root);
 create(16,&root);
 create(24,&root);
 create(6,&root);

 return 0;

}

最佳答案

更改

typedef struct
{
    int item;
    struct node * leftc;
    struct node * rightc;
}node;


typedef struct node
{
    int item;
    struct node * leftc;
    struct node * rightc;
}node;

在您的结构中,您引用“结构节点”,因此您需要名称才能使其正确引用自身。当然,在typedef之后,您可以将其称为节点。

测试程序可以编译,并且可以使用其他代码正常运行。

关于error-handling - 二进制搜索:please tell me whats wrong here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17328391/

相关文章:

java - 无法转换为 java.lang.Comparable

c - 使用堆栈和入栈和出栈函数将 BST 转换为数组

binary-tree - 计算BST中的剩余节点数

wpf - 处理 WPF 应用程序中所有异常的最佳实践是什么?

ios - 在异步 block 中使用xcode 7.0 beta处理错误

c++ - 有没有办法使函数仅在通过 C++ 中的参数传递的函数中可用?

binary-tree - 这是一棵完整的二叉树吗?

python - 如何确定二叉树节点是左 child 还是右 child ?

objective-c - 在 Objective-C 中,我试图封装多个可能出错的调用和 "return"最有用的错误

c++ - 使用两个链表