繁体   English   中英

C中的BST搜索功能

[英]Search function in BST in C

我有搜索功能的问题..我搜索了很多带有返回值的例子,但我只是想在void函数中编写它,这个函数只需打印“存在”或“不存在”..任何帮助或提示怎么做? 在C ofc。

struct bin_tree {
int data;
struct bin_tree * right, * left;
};

typedef struct bin_tree node;

void insert(node ** tree, int val)
{
    node *temp = NULL;
    if(!(*tree))
    {
        temp = (node *)malloc(sizeof(node));
        temp->left = temp->right = NULL;
        temp->data = val;
        *tree = temp;
    return;
    }
    if(val < (*tree)->data)
    {
        insert(&(*tree)->left, val);
    }
    else if(val > (*tree)->data)
    {
        insert(&(*tree)->right, val);
    }
}
void print_preorder(node * tree)
{
     if (tree)
     {
          printf("%d\n",tree->data);
          print_preorder(tree->left);
          print_preorder(tree->right);
     }
}
node* search(node ** tree, int val)
{
    if(!(*tree))
    {
        printf("Tree doesnt exist");
    }
    if(val < (*tree)->data)
    {
        search(&((*tree)->left), val);
    }
    else if(val > (*tree)->data)
    {
        search(&((*tree)->right), val);
    }
    else if(val == (*tree)->data)
    {
        printf("%d exist", val);
    }
    printf("%d not exist", val);
}
int main()
{
    node *root;
    node *tmp;
    int x, b;
    root = NULL;
    int warunek;
    do
    {
        printf("\nMENU\n");
        printf("1. Add[1]\n2. Preorder\n3. Search\n0. Exit[0]\n");
        scanf("%d", &warunek);
        switch(warunek)
        {
            case 1:
            {
                printf("Give a number: ");
                scanf("%d", &x);
                insert(&root, x);
            }
            break;
            case 2: print_preorder(root);
            break;
            case 3:
            {
                printf("Give a number to search: ");
                scanf("%d", &b);
                search(&root, b);
            }
            break;
            default: printf("Bad value, write 1, 2 or 3");
        }
    }
    while(warunek != 0);
    return 0;
}

假设所有其他功能都正确,我只关注功能

node* search(node ** tree, int val)

您需要将函数的返回类型更改为void,因为您没有返回任何值 -

void search(node ** tree, int val)

现在,对于搜索函数,您已检查*tree是否为NULL。
请注意,这种情况会在两种情况下发生 -

  • 传递给函数本身的树是空的。
  • 您正在搜索的元素不存在。

在将树根传递给函数之前,可以在main函数本身中检查第一种情况。 所以你需要改变你的if部分 -

if(!(*tree))
{
    printf("%d not exist", val);
}

在主要功能中,您需要进行以下更改 -

if(root!=NULL){
  printf("Give a number to search: ");
  scanf("%d", &b);
  search(&root, b);
 }
  else {
      printf("No search can be performed as the tree has no nodes.");
  }

请注意,如注释中所述,搜索功能不需要指向根指针的指针。 但在这里,我刚刚建议您的代码中必要的最小更改。 存在更有效的实施。

你的搜索功能基本上是,但......

  • ...你必须做出所有四种可能的结果 - 空节点,数字等于,小于或大于当前节点的独占结果。 也就是说,你有一个if ... else if ... else if ... else ... chain。
  • ...因为你不想返回任何东西,你的函数的返回类型必须是void
  • ...您不修改树的内容,因此没有必要传入指向根指针的指针。 只有在修改树结构时才需要,即添加,删除或重新排序节点。

所以:

void search(const node *tree, int val)
{
    if (!tree) {
        printf("%d doesn't exist.\n", val);
    } else if (val < tree->data) {
        search(tree->left, val);
    } else if (val > tree->data) {
        search(tree->right, val);
    } else {
        printf("%d exists.\n", val);
    }
}

因为你只遵循树中的一条路径,所以你也可以迭代这个函数:

int search(const node *tree, int val)
{
    while (tree) {
        if (val == tree->data) {
            printf("%d exists.\n", val);
            return 1;
        }

        if (val < tree->data) {
            tree = tree->left;
        } else {
            tree = tree->right;
        }
    }

    printf("%d doesn't exist.\n", val);
    return 0;
}

这次,我已经回复了真值。 在我看来,最好让函数不打印任何东西,但打印调用代码的结果。

暂无
暂无

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

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