C 我能';t将节点插入到bin搜索树

C 我能';t将节点插入到bin搜索树,c,insert,binary-search-tree,C,Insert,Binary Search Tree,我想写一个BST,但是插入函数不起作用。调试它时,我发现它没有插入 /* Binary Search Tree (BST).demo */ #include <stdio.h> #include <stdlib.h> typedef struct treeNode{ int data; struct treeNode* lChild; struct treeNode* rChild; } treeNode; treeNode* createN

我想写一个
BST
,但是插入函数不起作用。调试它时,我发现它没有插入

/* Binary Search Tree (BST).demo */
#include <stdio.h>
#include <stdlib.h>

typedef struct treeNode{
    int data;
    struct treeNode* lChild;
    struct treeNode* rChild;
 } treeNode;

treeNode* createNode(){
        treeNode *nNode;
        nNode=(struct treeNode*)malloc(sizeof(treeNode));
        nNode->data=0;
        nNode->lChild=NULL;
        nNode->rChild=NULL;

        return nNode;
}

void insert(treeNode* rt,int idata)
{
  if(rt==NULL){
    treeNode* nNode;
    nNode=createNode();
    nNode->data=idata;
    rt=nNode;
    rt->lChild=NULL;
    rt->rChild=NULL;
  }else{
    if(idata < rt->data)
        insert(rt->lChild,idata);
    else insert(rt->rChild,idata);

  }
}

int main()
{
    treeNode *root;
    root=(treeNode*)malloc(sizeof(treeNode));
    root->data=15;
    root->lChild=NULL;
    root->rChild=NULL;

    insert(root,13);
    if(root->lChild==NULL)
          printf("root no l child\n");
     // printf("root lchild data :%d",root->lChild->data);
   return 0;
}
/*二进制搜索树(BST).demo*/
#包括
#包括
类型定义结构树节点{
int数据;
结构树节点*lChild;
结构树节点*rChild;
}三烯醇;
treeNode*createNode(){
treeNode*nNode;
nNode=(struct treeNode*)malloc(sizeof(treeNode));
nNode->data=0;
nNode->lChild=NULL;
nNode->rChild=NULL;
返回nNode;
}
无效插入(treeNode*rt,int idata)
{
if(rt==NULL){
treeNode*nNode;
nNode=createNode();
nNode->data=idata;
rt=nNode;
rt->lChild=NULL;
rt->rChild=NULL;
}否则{
如果(idatadata)
插入(rt->lChild,idata);
否则插入(rt->rChild,idata);
}
}
int main()
{
树根;
根=(treeNode*)malloc(sizeof(treeNode));
根->数据=15;
root->lChild=NULL;
root->rChild=NULL;
插入(根,13);
if(root->lChild==NULL)
printf(“根目录号l子目录\n”);
//printf(“根lchild数据:%d”,根->lchild->数据);
返回0;
}

将其用作插入功能:

void insert(treeNode** rt,int idata)
{
     if((*rt)==NULL)
     {
       treeNode* nNode;
       nNode=createNode();
       nNode->data=idata;
       *rt=nNode;
       (*rt)->lChild=NULL;
       (*rt)->rChild=NULL;
     }
     else
     {
         if(idata < (*rt)->data)
             insert(&((*rt)->lChild),idata);
         else 
             insert(&((*rt)->rChild),idata);
     }
}

我通常是这样实现的:

void insert(treeNode* rt, int idata) {
    // assert( rt != NULL );
    if(idata < rt->data){
        if( rt->lChild != NULL)
            insert(rt->lChild, idata);
        else {
            rt->lChild = createNode();
            rt->lChild->data = idata;
        }
    } else {
        if( rt->rChild != NULL)
            insert(rt->rChild, idata);
        else {
            rt->rChild = createNode();
            rt->rChild->data = idata;
        }
    }
}
因此,
insert
将如下所示:

void insert(treeNode* rt, int idata) {
    // ...
            rt->lChild = createNode(idata);
    // ...
            rt->rChild = createNode(idata);
    // ...
}

将“根”作为参数传递给插入函数时,请使用对“根”的引用。但根本身是指针,是否需要引用?如果不使用对“根”的引用,则将其通过“值”传递给插入函数。在这种情况下,如果您正在创建一个新节点并将其分配给“根节点”(正如您在这里所做的),此更改将不会反映在insert函数之外,从而导致错误行为。谢谢您,我现在理解了为什么节点的结构中有*leftchild&*rightchild。谢谢@NithinBhaskar@michael_stackof没问题。享受编码乐趣:)
treeNode* createNode(int idata){
    // ...
    nNode->data = idata;
    // ...
}
void insert(treeNode* rt, int idata) {
    // ...
            rt->lChild = createNode(idata);
    // ...
            rt->rChild = createNode(idata);
    // ...
}