C 嵌套结构中二叉搜索树的句柄

C 嵌套结构中二叉搜索树的句柄,c,C,我必须创建二进制搜索树。所以我使用了嵌套结构。我不能保持树的顶端。当我给出输入时,它正在创建每个输入的新节点。帮我处理头部 树 #ifndef __TREE__H__ #define __TREE__H__ typedef struct _BSTNode { int data; struct _BSTNode *left,*right; }BSTNode; typedef struct _BST { BSTNode *tHead; }BST; int insertBST(BST

我必须创建二进制搜索树。所以我使用了嵌套结构。我不能保持树的顶端。当我给出输入时,它正在创建每个输入的新节点。帮我处理头部

#ifndef __TREE__H__
#define __TREE__H__

typedef struct _BSTNode
{
  int data;
  struct _BSTNode *left,*right;
}BSTNode;

typedef struct _BST
{
  BSTNode *tHead;
}BST;

int insertBST(BST ,int);

#endif
mainTree.c

#include"tree.h"
#include<stdio.h>



int main()
{
  FILE *fp;
  BST bst;
  bst.tHead=NULL;
  int element;
  fp = fopen("input","r");


  while(fscanf(fp,"%d",&element)!=EOF)
    {
      insertBST(bst,element);
    }

  return 0;
}

输入:-40 10 30 20

如何定义函数,
bst
通过值传递。英国夏令时也是如此。这两种情况都是不正确的,需要您按地址传递潜在更新的预期目标,或利用函数返回值传递回潜在更新。学习传递地址以利用传递值语言(如C)中的out参数语义是一个常见的障碍,所以不要太过自责。我无法处理树的头部,必须使用引用调用来处理
#include"tree.h"
#include<stdio.h>


int insertBST(BST bst,int element)
{
  return _insertBST(bst.tHead,NULL,element);
}


int _insertBST(BSTNode *p,BSTNode *parent,int e)
{
  if(p==NULL)
    return createBST(p,e);
  if(p->data == e)
    return 0;
  if(p->data < e)
    return _insertBST(p->right,p,e);
  if(p->data > e)
    return _insertBST(p->left,p,e);
  return -1;
 }
#include"tree.h"
#include<stdlib.h>
#include<stdio.h>

int createBST(BSTNode *p,int element)
{
  BSTNode *node = (BSTNode *)malloc(sizeof(BSTNode));
  node->data = element;
  node->left = NULL;
  node->right = NULL;

    if(p== NULL)
    {
      p=node;
      fprintf(stdout,"Im here 1");
      return 0;
      }
    else
   {
      if(p->data < node ->data)
    (p->left) = node;
         fprintf(stdout,"Im here 2");
     return 0;
      if(p->data > node ->data)
    (p->right) = node;
          fprintf(stdout,"Im here 3");
      return 0;
   }
}
@:~/Ds/tree$gcc mainTree.c insertBST.c createBST.c
@:~/Ds/tree$ ./a.out
Im here 1Im here 1Im here 1Im here 1Im here 1