[英]Search function in BST in C
I have a problem with search function there.. I searched a lot of examples with returning values, but i just want to write it in void function and this function just have to print "Exist" or "Not exist".. Any help or hint how to do it? 我有搜索功能的问题..我搜索了很多带有返回值的例子,但我只是想在void函数中编写它,这个函数只需打印“存在”或“不存在”..任何帮助或提示怎么做? In C ofc. 在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;
}
Assuming all of the other functions correct I am just focusing on the function 假设所有其他功能都正确,我只关注功能
node* search(node ** tree, int val)
You need to change the return type of the function to void as you are not returning any value - 您需要将函数的返回类型更改为void,因为您没有返回任何值 -
void search(node ** tree, int val)
Now for the search functions you have checked whether *tree
is NULL or not. 现在,对于搜索函数,您已检查*tree
是否为NULL。
Note that this condition will occur in two cases - 请注意,这种情况会在两种情况下发生 -
The first case can be checked in the main function itself, before passing the tree root to the function. 在将树根传递给函数之前,可以在main函数本身中检查第一种情况。 So you need to change your if
part as - 所以你需要改变你的if
部分 -
if(!(*tree))
{
printf("%d not exist", val);
}
In main function you need to make the following changes - 在主要功能中,您需要进行以下更改 -
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.");
}
Note that as mentioned in the comments the search function does not require a pointer to pointer to the root. 请注意,如注释中所述,搜索功能不需要指向根指针的指针。 But here I have just suggested the necessary minimal changes in your code. 但在这里,我刚刚建议您的代码中必要的最小更改。 More efficient implementation exist. 存在更有效的实施。
Your search function is basicallyokay, but ... 你的搜索功能基本上是,但......
if
... else if
... else if
... else
... chain. 也就是说,你有一个if
... else if
... else if
... else
... chain。 void
. ...因为你不想返回任何东西,你的函数的返回类型必须是void
。 So: 所以:
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);
}
}
because you follow only one path through the tree, you can also make this function iterative: 因为你只遵循树中的一条路径,所以你也可以迭代这个函数:
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;
}
Thistime, I've returned a truth value. 这次,我已经回复了真值。 In my opinion, it is better to have the function not print anything, but print the outcome from the calling code. 在我看来,最好让函数不打印任何东西,但打印调用代码的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.