#孩子表示法定义二叉搜索树
struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
}
#结点创建
struct TreeNode* creatNode(int val){
struct TreeNode* node=(struct TreeNode*)malloc(sizeof(struct TreeNode));
node->val=val;
node->left=null;
node->right=null;
return node;
}
#二叉搜索树的查找
bool BSFFind(struct TreeNode *root,int val){
if(root==null){
return false;
}
if(root->val==val){
return true;
}
if(root->val>val){
return BBFFind(root->right,val);
}else{
return BBFFind(root->left,val);
}
}
#二叉搜索树的插入
struct TreeNode *BSTInsert(struct TreeNode *root,int val){
if(root==null){
return creatNode(val);
}
if(val==root->val){
return root;
}
if(val<root->val){
return BSTInsert(root->left,val);
}else{
return BSTInsert(root->right,val);
}
return root;
}
#查找最小节点
int BSTFindMin(struct TreeNode *root){
if(root->left){
return BFSFindMin(root->left);
}
return root->val;
}
#删除给定节点
struct TreeNode *BSTDelete(struct TreeNode *root,int val){
if(root==null){
return null;
if(val==root->val){
return delete(root);
}
if(val<root->val){
return BSTDelete(root->left,val);
}else{
return BSTDelete(root->right,val);
}
return root;
}
#删除给定二叉搜索树的新节点
struct TreeNode *Delete(struct TreeNode* root)
{
struct TreeNode *delnode,*retnode;
if(root->left==null){
delnode=root,retnode=root->right,free(delnode);
}else if(root->right==null){
delnode=root,retnode=root->left,free(delnode);
}else{
retnode=(struct TreeNode)malloc(sizeof(struct TreeNode));
retnode->val=BSTFindMin(root->right);
retnode->right=BSTDelete(root->right,retnode->val);
retnode->left=noot->left;
}
return retnode;
}
#二叉搜索树的构造
struct TreeNode *BSTConstrct(int *val,int valsize){
int i;
struct TreeNode *root=null;
for(i=0;i<valsize;i++){
BSTInsert(root,val[i]);
}
return root;
}