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

问题描述 投票:0回答:1

不好意思,问的太多了,所以这和我之前的问题差不多。 我有一个带删除功能的平衡二叉树代码,所以问题出在我的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 = %d\n", root->data);
    if(root == NULL) return root;
    else if(data != root->data) {
        root = searchdelete(root, data);
        printf("root after = %d\n", 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("NULL\n");
            free(root);
            root = NULL;
        }
        //Case 2: one child, left or right
        else if(root->left == NULL){
            printf("left null\n");
            struct node *temp = root;
            root = root->right;
            free(temp);
        } else if (root->right == NULL){
            printf("right null\n");
            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 Menu\n");
        printf("1. Insert Data\n");
        printf("2. View on pre order\n");
        printf("3. View on post order\n");
        printf("4. View on in order\n");
        printf("5. Search\n");
        printf("6. Delete\n");
        printf("7. Exit\n");
        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;
}


c struct binary-tree
1个回答
1
投票

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

我不知道为什么不返回。

因为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* 而不是仅仅 int 喜欢 搜删 ? 对我来说,他的功能 搜索 是没有用的。搜删 已经足以知道键是否存在于树中,只需调用它,并将其返回值与NULL进行比较。所以最好删除 搜索 并改名为 搜删 将要 搜索 (或 找到)

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

如果元素是随机放置的,那么对二叉树根本没有兴趣。


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

© www.soinside.com 2019 - 2024. All rights reserved.