繁体   English   中英

C递归编程二进制树插入(不是二进制搜索树)

[英]C Programming Binary Tree Insertion Recursively (Not Binary Search Tree)

这是我的代码在这里。 我想将项目递归插入到二叉树中。 它不是二叉搜索树(左子级不必是<parent或右子级不需要是> parent)。

它只是一个二叉树,每个节点最多可以有两个孩子。 当我执行遍历时,它只是无限循环地无休止地打印出起始节点(5-> 5-> 5-> ....)。 请帮帮我。

我已经搜索了堆栈溢出,但没有任何内容基于此。 大多数是二进制搜索树。 如果这是一个不好的问题,我感到抱歉。

struct node {
    int info;
    struct node* left;
    struct node* right;
}*temp, *ptr, *prev;

struct node *root, *start=NULL;

void insert(struct node*);
void inorder(struct node*);

void insert(struct node* ptr)
{
    int ch;

    if(start==NULL)  // if start is null, new node is made as start node.
        start=ptr;
    else
    {
        temp=(struct node*)malloc(sizeof(struct node)); //new node created
        temp->left=NULL;
        temp->right=NULL;
        puts("Enter value");
        scanf("%d", &temp->info);
        ptr=temp;     //ptr is set as new node
    }

    printf("Does %d have a left node? (1/0)\n", ptr->info);
    scanf("%d", &ch);
    if(ch==1)
    {
        prev=ptr;
        if(ptr==start)
            insert(start->left); //start->left will be the new 'ptr' in the next insertion scenario
        else
            insert(ptr->left);  //same principle as above
    }

    printf("Does %d have a right node? (1/0)\n", ptr->info);
    scanf("%d", &ch);
    if(ch==1)
    {
        prev=ptr;
        if(start==ptr)
            insert(start->left);
        else
            insert(ptr->right);
    }

}

void inorder(struct node* ptr)
{
    while(ptr!=NULL)
    {
        inorder(ptr->left);
        printf("%d -> ", ptr->info);
        inorder(ptr->right);
    }
}

void main(){
    int ch;
    do{
        puts("1. Insert 2.Traverse 3.Exit");
        scanf("%d",&ch);
        switch(ch){
            case 1:
                puts("Enter root node");
                root=(struct node *)malloc(sizeof(struct node));
                root->left=NULL;
                root->right=NULL;
                scanf("%d", &root->info);
                insert(root);
                break;
            case 2:
                inorder(start);
        }
    }while(ch!=3);
}

预先谢谢大家。

您的遍历创建无限循环,则应将while更改为if

void inorder(struct node* ptr)
{
    if (ptr != NULL)
    {
        inorder(ptr->left);
        printf("%d -> ", ptr->info);
        inorder(ptr->right);
    }
}

当执行ptr=temp;时,在insert(struct node* ptr) ptr=temp; 它只是在函数作用域内更改ptr,因此实际上您永远不会为根分配左节点和右节点

尝试通过以下方式添加节点:

struct node *createTree()
{
    struct node *node;
    int resp;

    node=malloc(sizeof(struct node)); //new node created
    node->left=NULL;
    node->right=NULL;
    puts("Enter value");
    scanf("%d", &node->info);

    printf("Does %d have a left node? (1/0)\n", node->info);
    scanf("%d", &resp);
    if(resp==1)
        node->left = createTree();

    printf("Does %d have a right node? (1/0)\n", node->info);
    scanf("%d", &resp);
    if(resp==1)
        node->right = createTree();

    return node;
}

然后在main()执行:

root = createTree();

请注意,如果以"%d"格式扫描,则resp变量的类型为int 对于char类型,应使用格式"%c"

我已经找到解决方案。 很抱歉浪费您的时间。 我的代码的问题是:

1)在遍历函数中使用while而不是if 2)其次,当我传递参数ptr时,它不知道将该ptr链接到何处。 我一直在做的只是ptr = temp。 它必须链接到上一个节点的左/右,对吗?

@huxley对功能范围的解释是错误的。 它必须指向同一对象-这就是为什么我们使用指针,对吧? 但是,它在我的头上摇了铃。 所以这是下面的解决方案:

void insert(struct node* ptr, int side)
{
    int ch;

    if(start==NULL)  // if start is null, new node is made as start node.
        start=ptr;
    else
    {
        temp=(struct node*)malloc(sizeof(struct node)); //new node created
        temp->left=NULL;
        temp->right=NULL;
        puts("Enter value");
        scanf("%d", &temp->info);
        ptr=temp;
        if(side==1)
            prev->left=ptr;
        else
            prev->right=ptr;
    }

    printf("Does %d have a left node? (1/0)\n", ptr->info);
    scanf("%d", &ch);
    if(ch==1)
    {
        prev=ptr;
        insert(ptr->left, 1);
    }

    printf("Does %d have a right node? (1/0)\n", ptr->info);
    scanf("%d", &ch);
    if(ch==1)
    {
        prev=ptr;
        insert(ptr->right, 2);
    }

}

void inorder(struct node* ptr)
{
    if(ptr!=NULL)
    {
        inorder(ptr->left);
        printf("%d -> ", ptr->info);
        inorder(ptr->right);
    }
}

我详细解释了它,因为没有使用递归的二进制树的正确插入代码。 我希望每个人都能理解。

谢谢大家的帮助。

干杯,阿基尔

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM