c-为什么我的二叉树搜索函数不返回根地址



很抱歉问得太多了,所以这与我之前的问题几乎相同
我有一个带有删除函数的平衡二进制树代码,所以问题出在我的searchdelete((函数中。我不知道它为什么不把根还回来。例如

5, 4, 3, 2, 1

然后我在菜单上输入"要删除的数据:1">
它进入searchdelete((;函数,然后它成功地
显示找到了数据,found = 1
,但在那之后,它就不会返回根地址。我不知道它为什么不回来
所以在Delete((中;功能,它不会打印printf("root after = %d", root->data)

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
struct node{
int data, balance;
struct node *left, *right;
};
int insert(struct node **root, struct node **curr, int data){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode -> data = data;
newNode -> left = NULL;
newNode -> right = NULL;
newNode -> balance = 0;
if((*root) == NULL){
(*root) = (*curr) = newNode;
(*root) -> left = NULL;
(*root) -> right = NULL;
return 0;
} else {
if((*curr)->left == NULL && (*curr)->balance == 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) -> left = newNode;
return 0;
} else if ((*curr)->right == NULL && (*curr)->balance == -1){
(*curr) -> balance = (*curr) -> balance + 1;
(*curr) -> right = newNode;
return 0;
} else if ((*curr)->balance == 0 && (*curr)->left->balance == 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) = (*curr)->left;
return insert(root,curr,data);
} else if ((*curr)->balance < 0 && (*curr)->left->balance < 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) = (*curr) -> left;
return insert(root,curr,data);
} else if ((*curr)->balance < 0 && (*curr)->left->balance == 0){
(*curr) -> balance = (*curr) -> balance + 1;
(*curr) = (*curr)->right;
return insert(root, curr, data);
}
}
}
void preorder(struct node *root){
if(root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node *root){
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
void inorder(struct node *root){
if(root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

void search(struct node *root, int *key, int *found){
if(root == NULL) return;
search(root->left, key, found);
if(root->data == *key){
*found = 1;
return ;
}
search(root->right, key, found);
}
struct node *findMin(struct node *root){
while(root->left != NULL) root = root->left;
return root;
}
struct node *searchdelete(struct node *root, int data){
if(root == NULL) return root;                                    //This is the searchdelete
searchdelete(root->left, data);
if(root->data == data){
printf("found = %d", root->data);
return root;
}
searchdelete(root->right, data);
}
struct node *Delete(struct node *root, int data){
printf("root before = %dn", root->data);
if(root == NULL) return root;
else if(data != root->data) {
root = searchdelete(root, data);
printf("root after = %dn", root->data);           //this is where it won't print
system("pause");
}
else{
//Case 1: no child / leaf node
if(root->left == NULL && root->right == NULL){
printf("NULLn");
free(root);
root = NULL;
}
//Case 2: one child, left or right
else if(root->left == NULL){
printf("left nulln");
struct node *temp = root;
root = root->right;
free(temp);
} else if (root->right == NULL){
printf("right nulln");
struct node *temp = root;
root = root->left;
free(temp);
}
//Case 3: two children
else{
printf("two children n");
if(root->right->data > root->data){
struct node *temp = root;
root = root->right;
free(temp);
} else {
struct node *temp = root;
root = root->left;
free(temp);
}
}
}
system("pause");
return root;
}

int main(){
struct node *root, *curr;
int choice, data, key, found, delKey;
root = curr = NULL;
while(1){
found = 0;
printf("Balanced Binary Tree Menun");
printf("1. Insert Datan");
printf("2. View on pre ordern");
printf("3. View on post ordern");
printf("4. View on in ordern");
printf("5. Searchn");
printf("6. Deleten");
printf("7. Exitn");
printf("Pilihan: ");scanf("%d", &choice);fflush(stdin);
if(choice == 1){
printf("Enter data : "); scanf("%d", &data);
curr = root;
insert(&root, &curr, data);
} else if (choice == 2){
preorder(root);
system("pause");
} else if (choice == 3){
postorder(root);
system("pause");
} else if (choice == 4){
inorder(root);
system("pause");
} else if (choice == 5){
printf("Search: "); scanf("%d", &key);
search(root, &key, &found);
if(found == 1){
printf("Data found !n");
} else {
printf("Data not found !n");
}
system("pause");
} else if (choice == 6){
printf("Enter data to be deleted: "); scanf("%d", &delKey);
Delete(root, delKey);
} else if (choice == 7){
return 1;
}
system("cls");
}
return 0;
}

为什么我的二叉树搜索函数不返回根地址?

我不知道它为什么不回来。

因为2个return缺少

可以是:

struct node * searchdelete(struct node *root, int data){
if(root == NULL)
return root;
if(root->data == data){
printf("found = %d", root->data); /* debug, to be removed */
return root;
}
if ((root = searchdelete(root->left, data)) != NULL)
return root;
return searchdelete(root->right, data);
}

搜索中,为什么int*,而不是像searchdelete那样的int?对我来说,search函数是无用的,searchdelete已经足够知道树中是否存在键,只需调用它并将其返回值与NULL进行比较。因此,最好删除搜索,并将searchdelete重命名为earch(或find(

在这两个函数中,搜索时都不使用树可以排序的事实,我的意思是左边的数据较少,右边的数据较多,这提高了的性能

如果元素被随机放置在中,那么对二进制树根本没有兴趣


您对Delete的定义有很多问题,包括未定义的行为,因为您在不从树中删除单元格的情况下释放了单元格

最新更新