#include<stdlib.h>
#include<stdio.h>
typedef struct Node //结构体定义
{
int data;
struct Node *lchild;
struct Node *rchild;
}LNode,*BiTree;
int BST_Insert(BiTree &T,int n) //二叉排序树插入操作
{
if(T==NULL)
{
T=(BiTree)malloc(sizeof(Node));
T->data=n;
T->lchild=T->rchild=NULL;
return 1;
}
else if(n==T->data)
return -1;
else if(n>T->data)
{
return BST_Insert(T->rchild,n);
}
else if(n<T->data)
{
return BST_Insert(T->lchild,n);
}
}
void Create_BST(BiTree &T ,int str[],int n) 创建二叉排序树
{
T=NULL;
for(int i=0;i<n;i++)
{
BST_Insert(T,str[i]);
}
}
void mid_Tree(BiTree T) //中序遍历二叉排序树,检验是否正确
{
if(T!=NULL)
{
mid_Tree(T->lchild);
printf("%d ",T->data);
mid_Tree(T->rchild);
}
}
int main() //主入口
{
BiTree T;
int n=5;
int str[10]={2,1,5,4,6};
Create_BST(T,str,n); //创建二叉排序树T
mid_Tree(T); //中序遍历进行校验
}