c - 二叉搜索树 C 实现

标签 c binary-search-tree

我最近写了一段相当简单的代码,试图用 C 语言实现一个带有插入、搜索、删除和显示操作的二叉搜索树。不幸的是,代码似乎不起作用。

#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int data;
    struct TreeNode *leftChildNode;
    struct TreeNode *rightChildNode;
};

typedef struct TreeNode node;
node *rootNode = NULL;

void insertNode(int i, node *n) {
    if(n == NULL) {
        n = (node*)malloc(sizeof(node));
        n->leftChildNode = NULL;
        n->rightChildNode = NULL;
        n->data = i;
    }
    else 
    if(n->data == i)
        printf("\nThis value already exists in the tree!");
    else
    if(i > n->data)
        insertNode(i, n->rightChildNode);
    else
        insertNode(i, n->leftChildNode);
    }

void searchNode(int i, node *n) {
    if(n == NULL)
        printf("\nValue does not exist in tree!");
    else
    if(n->data == i)
        printf("\nValue found!");
    else
    if(i > n->data)
        searchNode(i, n->rightChildNode);
    else
        searchNode(i, n->leftChildNode);
    }

void deleteNode(int i, node *n) {
    if(n == NULL)
        printf("\nValue does not exist in tree!");
    else
    if(n->data == i) {
        if(n->leftChildNode == NULL)
            n = n->rightChildNode;
        else
        if(n->rightChildNode == NULL)
            n = n->leftChildNode;
        else {
            node *temp = n->rightChildNode;
            while(temp->leftChildNode != NULL)
                temp = temp->leftChildNode;
            n = temp;
        }
    }
    else
    if(i > n->data)
        deleteNode(i, n->rightChildNode);
    else
        deleteNode(i, n->leftChildNode);
    }

void displayPreOrder(node *n) {
    if(n != NULL) {
        printf("%d ", n->data);
        displayPreOrder(n->leftChildNode);
        displayPreOrder(n->rightChildNode);
    }
}

void displayPostOrder(node *n) {
    if(n != NULL) {
        displayPostOrder(n->leftChildNode);
        displayPostOrder(n->rightChildNode);
        printf("%d ", n->data);
    }
}

void displayInOrder(node *n) {
    if(n != NULL) {
        displayInOrder(n->leftChildNode);
        printf("%d ", n->data);
        displayInOrder(n->rightChildNode);
    }
}

int main(void) {
    int ch, num, num1;
    do {
        printf("\nSelect a choice from the menu below.");
        printf("\n1. Insert a node.");
        printf("\n2. Search for a node.");
        printf("\n3. Delete a node.");
        printf("\n4. Display the Binary Search Tree.");
        printf("\nChoice: ");
        scanf("%d", &ch);
        switch(ch) {
            case 1: printf("\nEnter an element: ");
                    scanf("%d", &num);
                    //printf("YESYES");
                    insertNode(num, rootNode);
                    break;

            case 2: printf("\nEnter the element to be searched for: ");
                    scanf("%d", &num);
                    searchNode(num, rootNode);
                    break;

            case 3: printf("\nEnter the element to be deleted: ");
                    scanf("%d", &num);
                    deleteNode(num, rootNode);
                    break;

            case 4: printf("\nSelect an order for the elements to be display in.");
                    printf("\n1. Pre-order.");
                    printf("\n2. Post-order.");
                    printf("\n3. In-order.");
                    printf("\nChoice: ");
                    scanf("%d", &num1);
                    switch(num1) {
                        case 1: printf("\nPre-order Display: ");
                                displayPreOrder(rootNode);
                                break;

                        case 2: printf("\nPost-order Display: ");
                                displayPostOrder(rootNode);
                                break;

                        case 3: printf("\nIn-order Display: ");
                                displayInOrder(rootNode);
                                break;

                        default: exit(0);
                    }
                    break;

            default: exit(0);
            }
        //printf("%d", rootNode->data);
        printf("\nIf you want to return to the menu, press 1.");
        printf("\nChoice: ");
        scanf("%d", &num);
    } while(num == 1);

    return 0;
}

事实上,请注意注释行 printf("%d", rootNode->data); 就在 main( ) 结束。如果我取消注释这一行,编译程序并运行它,程序会抛出一个段错误。谁能告诉我为什么会出现这个错误以及为什么整个代码没有运行?提前致谢。

最佳答案

您对 C 处理参数的方式有误解。在 C 中,所有参数都是按值传递的,包括指针。当您在函数内部重新分配指针时,您正在重新分配该指针的副本。

例如:

void f ( int *p );

int *p;

f(p);

函数中指针的地址(&p)不同。它们都指向相同的位置(具有相同的值),但每个都有不同的地址。当您将指针分配给 malloc 的返回值时,它只是分配该指针的函数本地副本。

解决这个问题的一种方法是引入另一个间接级别,并传递指针的地址:void insertNode(int i, node **n),您可以像 >插入节点(0,&n)。当您想将其更改为其他内容时,取消引用一次并然后分配:*p = malloc(sizeof(node))

另一个解决方案是让函数返回指针并在调用代码中分配它:return malloc(sizeof(node))。 (注意:您实际上会在初始化代码之后返回它...也不要在 C 中强制转换 malloc 的返回值)。

关于c - 二叉搜索树 C 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16452854/

相关文章:

c - 实现广度优先搜索时的错误 (C)

java - 只处理其中一个子树

c - 二进制搜索树插入不起作用

c - 0 1 背包的算法实现的复杂度是多少?

c - 如何修复 scanf 中的 %ms 警告

c - 通过 C 中具有位字段的结构指针访问无符号整数的各个位

c++ - 递归删除二叉树中的每个节点

c - 是否有任何库为 C/C++ 守护程序提供管理 CLI?

c - 解析配置文件中带有前导和终止空格的简单名称/值对设置 - C

binary-search-tree - 从排序数组创建二叉搜索树