【问题标题】:C Programming Binary Tree Insertion Recursively (Not Binary Search Tree)C编程二叉树递归插入(不是二叉搜索树)
【发布时间】:2015-06-16 04:26:12
【问题描述】:

这是我的代码。我想将项目递归地插入二叉树。它不是二叉搜索树(左孩子不必是父母)。

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

我已经通过 Stack Overflow 进行了搜索,但没有任何内容基于此。大多数是二叉搜索树。如果这是一个不好的问题,我很抱歉。

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

提前谢谢各位。

【问题讨论】:

  • 你的遍历函数看起来没问题,但是你的插入代码到处都是。我建议两件事:1.停止使用全局变量。这通常是不好的做法,尤其是在处理链表时。 2. 使用一个(或多个)函数来操作您的树并添加/插入节点。
  • 如果您以前从未使用过调试器,这看起来是一个很好的学习机会。如果您的系统上有可用的 gdb,请参阅 thegeekstuff.com/2010/03/debug-c-program-using-gdb
  • @Eregrith 遍历函数看起来不太好——如果ptr != NULL,它将永远循环。
  • @CiaPan 真的很抱歉,我想我需要多睡点>_while

标签: c binary-tree insertion


【解决方案1】:

你的遍历创建了无限循环,你应该把while改成if

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

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

【讨论】:

  • 感谢指出遍历函数的错误。关于插入功能,您所说的也有道理。什么是合适的解决方案?
【解决方案2】:

试试这种添加节点的方式:

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

注意resp 变量的类型为int,如果您使用"%d" 格式扫描它。对于char 类型,您应该使用格式"%c"

【讨论】:

    【解决方案3】:

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

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

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

    感谢所有帮助过的人。

    干杯, 阿基尔

    【讨论】:

      猜你喜欢
      • 2013-04-14
      • 1970-01-01
      • 2011-12-15
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      相关资源
      最近更新 更多