If the original tree is empty, generate and return a binary search tree
with a node. If it is not empty, X is compared with the key value of the root node of the tree.
If X is large, insert to the right subtree.
If X is small, then to the left Subtree insertion
//定义二叉树
typedef struct TreeNode *BinTree;
typedef BinTree Position;
typedef int ElementType;
struct TreeNode
{
ElementType data;
BinTree left;
BinTree right;
};
BinTree Insert(ElementType x,BinTree bst)
{
if(!bst) //如果为空,生成并返回只有一个结点的二叉树
{
bst=(BinTree)malloc(sizeof(TreeNode));
bst->data=x;
bst->left=bst->right=NULL;
}
else
{
if(x<bst->data)
bst->left=Insert(x,bst->left);
else if(x>bst->data)
bst->right=Insert(x,bst->right);
//x已经存在,什么都不做
}
return bst;
}