二叉搜索树C实现

时间:2013-05-09 01:22:29

标签: 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);do-while块前的注释行main()。如果我取消注释这一行,编译程序并运行它,程序会抛出一个分段错误。任何人都可以告诉我为什么会出现这个错误以及为什么整个代码没有运行?提前谢谢。

4 个答案:

答案 0 :(得分:5)

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

例如:

void f ( int *p );

int *p;

f(p);

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

解决此问题的一种方法是引入另一级别的间接,并传递指针的地址:void insertNode(int i, node **n),您可以调用insertNode(0, &n)。如果要将其更改为其他内容,请将其取消引用一次,然后然后分配:*p = malloc(sizeof(node))

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

答案 1 :(得分:1)

在顶部,您声明

node *rootNode = NULL; 

如果你没有运行insertNode(成功 - 请参阅Matt的回答),尝试打印时节点仍然是NULL,这就是你得到段错误的原因。

答案 2 :(得分:1)

取消注释printf语句时代码段错误的原因是rootNode是一个NULL指针。在函数调用中取消引用此NULL指针会导致段错误。

rootNode是一个NULL指针的原因是它永远不会被代码更改。调用insertNode()会导致局部变量n被设置为rootNode中存储的值(在本例中为NULL)。 n函数中对insertNode()的更改不会更改rootNode

要修复代码,您可以更改insertNodedeleteNode函数以接受指向根节点指针的指针。例如,insertCode()函数将变为:

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);
        }
    }
}

您还必须更改代码,以便通过引用rootNode insertNode()来调用insertNode(num, &rootNode);

我还建议您检查各种scanf调用的返回值。如果scanf("%d",x)返回0,则该值不会转换为int,并且x的内容未定义。理想情况下,代码可以优雅地处理这种情况。

答案 3 :(得分:0)

我认为问题在于插入函数的签名尝试以下代码并且它将起作用

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));
    }

使用Insert Function as

insertNode(num, &rootNode);

之前的更改仍保留在“仅插入”功能中。在里面使用双指针。

相关问题