简体   繁体   English

在二叉树中插入节点-将迭代转换为递归

[英]Insert a node in a binary tree - Convert iterative to recursive

Repeated Question: 重复问题:

Recently I'm reading Data Structure(Binary Search Trees), I understand recursion very well and can trace it as well. 最近,我正在阅读数据结构(二叉搜索树),我非常了解递归并且也可以对其进行跟踪。

I used an approach which always worked for me ie write a program with a loop, then eliminate loop and write a recursive function, the base condition will be same as loop exit condition. 我使用了一种始终对我有用的方法,即编写带有循环的程序,然后消除循环并编写递归函数,基本条件将与循环退出条件相同。

But when it comes to writing one without my loop method, am getting failed. 但是,如果要编写一个没有我的循环方法的代码,就会失败。 I wasn't able to write a recursive function to insert a node in Binary Search Tree.(Though I understood it properly by referring the solution). 我无法编写一个递归函数以在Binary Search Tree中插入一个节点。

Kindly guide me, How to improve it? 请指导我,如何改进它?

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *left;//To store the address of the left child
    struct node *right;//To store the address of the Right child
};
struct node * root;
struct node *createnewnode(int x)
{
    struct node *n=(struct node *)malloc(sizeof(struct node));
    n->data=x;
    n->left=NULL;
    n->right=NULL;
    return n;
}
void Insert(int x)
{
    struct node *a,*b;
    struct node *temp=root;
    if(root==NULL)
        root=createnewnode(x);
    else
    {
        while(1)
         {
            if(x<=temp->data)
              {
                 if(temp->left!=NULL)
                    temp=temp->left;
                 else
                 {
                   a=createnewnode(x);
                   temp->left=a;
                   break;   
                 }
              }
            else
              {
                if(temp->right!=NULL)
                    temp=temp->right;
                 else
                 {
                   a=createnewnode(x);
                   temp->right=a;
                   break;   
                 }
              }  
         }
    }

}
int main()
{
    root==NULL;//Empty Tree
    Insert(15);
    Insert(10);
    Insert(20);
    Insert(25);
    return 0;
}

Edit: Sorry for not posting the code previously. 编辑:对不起,您以前没有发布代码。 This is the code I have written for inserting a node, now how do I convert this into a recursive method? 这是我编写的用于插入节点的代码,现在如何将其转换为递归方法?

The recursive Insert always asks the following question: can I insert a node in the current root ? 递归插入始终会问以下问题:是否可以在当前root插入节点? If not because root is not null then I have to check whether I have to recurse on the left or right subtree and call Insert recursively. 如果不是因为root not null那么我必须检查是否必须在左侧或右侧子树上递归并递归调用Insert

Something like the following should be enough to give you an idea on how to do it 如下所示的内容应该足以使您了解如何执行此操作

Node* Insert(Node* root, int x) {
  if (root == NULL)
    return createnewnode(x);
  else 
    if (x <= root->data) 
      root->left=Insert(root->left);
    else
      root->right=Insert(root->right);
}

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

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