【问题标题】:Unable to print Binary Search Tree无法打印二叉搜索树
【发布时间】:2013-11-24 18:35:14
【问题描述】:
#include<stdio.h>
#include<stdlib.h>

typedef struct BTreeNode BTNode;
struct BTreeNode
{
    int value;
    struct BTreeNode *left_child,*right_child;
};

BTNode* insert(int input_value, BTNode **head_node)
{
    BTNode *temp,*head;
    temp = malloc(sizeof(BTNode));
    temp->value = input_value;
    temp->left_child = NULL;
    temp->right_child = NULL;
    head = *head_node;
    while(1)
    {
        if(head == NULL)
        {
            head = temp;
            printf("Inserted successfully\n");
            return *head_node;
        }
        if(temp->value > head->value)
        {
            head = head->right_child;
        }
        else if(temp->value < head->value)
        {
            head = head->left_child;
        }
        else
        {
            break;
        }
    }
    return head;
}

void printtree(BTNode **head_node)
{
    BTNode *head;
    head = *head_node;
    if(head == NULL)
    {
        printf("Print exit\n");
        return;
    }
    else
    {
        printf("%d\n",head->value);
        printtree(&(head->left_child));
        printtree(&(head->right_child));
    }
}

int main()
{
    BTNode *root=NULL;
    root=insert(23,&root);
    root=insert(32,&root);
    root=insert(230,&root);
    root=insert(3,&root);
    root=insert(2,&root);
    root=insert(50,&root);
    printtree(&root);
}

我正在尝试在二叉搜索树中插入节点并打印该树。 当我调用printtree() 函数时,它将进入NULL 条件,即root 是NULL。 我不明白为什么它不起作用 有人可以帮忙吗???

【问题讨论】:

    标签: c binary-search-tree


    【解决方案1】:

    我修复了你的代码(查看我的代码 cmets!):

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct BTreeNode BTNode;
    struct BTreeNode
    {
    int value;
    struct BTreeNode *left_child,*right_child;
    };
    
    BTNode* insert(int input_value, BTNode **head_node)
    {
        BTNode *temp,*head;
        temp = (BTNode*)malloc(sizeof(BTNode));
        temp->value = input_value;
        temp->left_child = NULL;
        temp->right_child = NULL;
        head = *head_node;
        while(1)
        {
            if(head == NULL)
            {
                *head_node = temp;
                printf("Inserted successfully\n");
    //          break;
                return *head_node;
            }
            if(temp->value > head->value)
            {
                if(head->right_child==NULL)     //<- in case the right child is null    
                {
                    head->right_child=temp;
                    break;
                }
                else
                    head = head->right_child;
            }
            else if(temp->value < head->value)
            {
                if(head->left_child==NULL)     //<- in case the left child is null
                {
                    head->left_child=temp;
                    break;
                }
                else
                 head = head->left_child;
            }
            else
            {
                break;
            }
        }
        return *head_node;                  //<- returb always the Tree root!
    }
    
    void printtree(BTNode **head_node)
    {
        BTNode *head;
        head = *head_node;
        if(head == NULL)
        {
            printf("Print exit\n");
            return;
        }
        else
        {
            printf("%d\n",head->value);
            printtree(&(head->left_child));
            printtree(&(head->right_child));
        }
    }
    
    int main()
    {
        BTNode *root=NULL;
        root=insert(23,&root);
        root=insert(32,&root);
        root=insert(230,&root);
        root=insert(3,&root);
        root=insert(2,&root);
        root=insert(50,&root);
        printtree(&root);
    }
    

    【讨论】:

    • 即使现在它也没有打印节点值。它正在打印语句“打印退出”。可能是什么问题??谢谢
    • 它通过一个小修复工作。必须返回 head 以防 head 为空而不是 *headnode
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多