Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在二叉树中插入节点-将迭代转换为递归_C_Algorithm_Recursion_Binary Search Tree - Fatal编程技术网

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

C 在二叉树中插入节点-将迭代转换为递归,c,algorithm,recursion,binary-search-tree,C,Algorithm,Recursion,Binary Search Tree,重复问题: 最近我在阅读数据结构(二进制搜索树),我非常了解递归,并且可以跟踪它 我使用了一种对我一直有效的方法,即编写一个带有循环的程序,然后消除循环并编写一个递归函数,基本条件将与循环退出条件相同 但是,当谈到写一个没有我的循环方法时,我就失败了。 我无法编写递归函数在二叉搜索树中插入节点(尽管我通过引用解决方案正确理解了它) 请指导我,如何改进它 #include<stdio.h> #include<stdlib.h> struct node { int d

重复问题:

最近我在阅读数据结构(二进制搜索树),我非常了解递归,并且可以跟踪它

我使用了一种对我一直有效的方法,即编写一个带有循环的程序,然后消除循环并编写一个递归函数,基本条件将与循环退出条件相同

但是,当谈到写一个没有我的循环方法时,我就失败了。 我无法编写递归函数在二叉搜索树中插入节点(尽管我通过引用解决方案正确理解了它)

请指导我,如何改进它

#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;
}
#包括
#包括
结构节点
{
int数据;
struct node*left;//存储左边子节点的地址
struct node*right;//存储正确子级的地址
};
结构节点*根;
结构节点*createnewnode(int x)
{
结构节点*n=(结构节点*)malloc(sizeof(结构节点));
n->data=x;
n->left=NULL;
n->right=NULL;
返回n;
}
空白插入(整数x)
{
结构节点*a,*b;
结构节点*temp=root;
if(root==NULL)
root=createnewnode(x);
其他的
{
而(1)
{
if(扩展数据)
{
如果(临时->左!=空)
温度=温度->左侧;
其他的
{
a=createnewnode(x);
温度->左=a;
打破
}
}
其他的
{
如果(临时->右侧!=NULL)
温度=温度->右侧;
其他的
{
a=createnewnode(x);
温度->右侧=a;
打破
}
}  
}
}
}
int main()
{
root==NULL;//空树
插入(15);
插入(10);
插入(20);
插入(25);
返回0;
}
编辑:很抱歉之前没有发布代码。
这是我为插入节点而编写的代码,现在如何将其转换为递归方法?

递归插入总是提出以下问题:我可以在当前
根目录中插入节点吗?如果不是因为
root
notnull
,那么我必须检查是否必须在左子树或右子树上递归,并递归调用
Insert

下面的内容应该足以让您了解如何做

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);
}
节点*插入(节点*根,int x){
if(root==NULL)
返回createnewnode(x);
其他的
if(x数据)
根->左=插入(根->左);
其他的
根->右=插入(根->右);
}

您不应该将指针传递给指针吗?否则,你所做的就是泄漏内存!您缺少一个返回语句。