#include <stdio.h>
#include <malloc.h>
typedef struct BSTNode
{
int data;
struct BSTNode *lchild, *rchild;
} BSTNode, *BSTree;
/**
*二叉排序树的插入
* 1、若二叉排序数为空,则待插入结点*s作为根结点插入到空树中
* 2、若二叉排序树非空,则将key与根结点的关键字T->data.key进行比较
* *若key小于T->data.key,则将*s插入左子树
* *若key大于T->data.key,则将*s插入右子树
* @param T
* @param e
*/
void InsertBST(BSTree T, int e)
{
if (T == NULL)
{
T = (BSTree)malloc(sizeof(BSTNode));
T->data = e;
T->lchild = T->rchild = NULL;
}
else if (T->data > e)
{
InsertBST(T->lchild, e);
}
else
{
InsertBST(T->rchild, e);
}
}
void order(BSTree T)
{ //中序遍历
if (T != NULL)
{
order(T->lchild);
printf("%d\n", T->data);
order(T->rchild);
}
}
int main()
{
int i;
int a[5] = {3, 4, 2, 5, 9};
BSTree T;
for (i = 0; i < 5; i++)
{
InsertBST(T, a[i]);
}
order(T);
}