BST树

int BstInsertNonRecur(BstTree &T, int val)
{
BstTreeNode *pre = NULL;
BstTreeNode *tmp = T;
while (tmp != NULL)
{
pre = tmp;
if (val < T->val)
tmp = T->lchild;
else if (val>T->val)
{
tmp = T->rchild;
}
else
{
return 0;
}
}
BstTreeNode *node = new BstTreeNode(val);
node->parent = pre;


//此时node为bst树的第一个根节点
if (pre == NULL)
{
T = node;
}
else
{
if (val < pre->val)
pre->rchild = node;
else
{
pre->lchild = node;
}
}
}


//插入
int BstInsert(BstTree &T, int val, BstTreeNode *parent)
{
//如果T为NULL
if (T == NULL)
{
T = (BstTree)malloc(sizeof(BstTreeNode));
T->val = val;
T->parent = parent;
T->lchild = NULL;
T->rchild = NULL;
return 1;
}
else if (val < T->val) //左子树
{
return BstInsert(T->lchild, val, T);
}
else if (val > T->val) //右子树
{
return BstInsert(T->rchild, val, T);
}
else //等于节点T
{
return 0;
}
}


//构造
void CreatBst(BstTree &T, int arr[], int n)
{
T = NULL;
for (int i = 0; i < n; i++)
{
BstInsert(T, arr[i],T);
}
}


//查找
BstTreeNode *BstSearch(BstTree &T, int val)
{
if (T == NULL || T->val == val)
{
return T;
}
else if (T->val < val)
{
return BstSearch(T->rchild, val);
}
else if (T->val > val)
{
return BstSearch(T->lchild, val);
}
}
//非递归查找
BstTreeNode *BstSearchNonRecur(BstTree &T, int val)
{
while (T!=NULL && val!=T->val)
{
if (T->val < val)
{
T = T->rchild;
}
else if (T->val > val)
{
T = T->lchild;
}
}
return T;
}


//最大值
BstTreeNode *MaxNode(BstTree T)
{
if (T == NULL)
return NULL;
while (T->rchild!=NULL)
{
T = T->rchild;
}
return T;
}
//最小值
BstTreeNode *MinNode(BstTree T)
{
if (T == NULL)
return NULL;
while (T->lchild!=NULL)
{
T = T->lchild;
}
return T;
}
//查找给定节点的后继
BstTreeNode *BstSuccessor(BstTreeNode *node)
{
if (node->rchild != NULL)
return MinNode(node->rchild);
BstTreeNode *tmp = node->parent;
while (tmp && tmp->rchild == node)
{
node = tmp;
tmp = tmp->parent;
}
return tmp;
}
//查找给定节点的前驱
BstTreeNode *BstPreSuccessor(BstTreeNode *node)
{
if (node->lchild != NULL)
return MaxNode(node->lchild);
BstTreeNode *tmp = node->parent;
while (tmp && tmp->lchild == node)
{
node = tmp;
tmp = tmp->lchild;
}
return tmp;
}


//删除
int DeleteBst(BstTree &T, BstTreeNode *z)
{
if (T == NULL)
{
return -1;
}
if (T->val == z->val)
{
if (T->lchild != NULL && T->rchild != NULL)
{
BstTreeNode *right_min = BstSuccessor(T);
T->val = right_min->val;
if (right_min->parent->lchild == right_min)
{
right_min->parent->lchild = right_min->rchild;
}
else
{
right_min->parent->rchild = right_min->rchild;
}
if (right_min->rchild != NULL)
{
right_min->rchild->parent = right_min->parent;
}
delete right_min;
}
else
{
if (T->lchild != NULL)
{
if (T->parent->lchild == T)
{
T->parent->lchild = T->lchild;
}
else
{
T->parent->rchild = T->lchild;
}
T->lchild->parent = T->parent;
}
else if (T->rchild != NULL)
{
if (T->parent->lchild == T)
{
T->parent->lchild = T->rchild;
}
else
{
T->parent->rchild = T->rchild;
}
T->rchild->parent = T->parent;
}
else
{
if (T->parent->lchild == T)
{
T->parent->lchild = NULL;
}
else
{
T->parent->rchild = NULL;
}
}
delete T;
}
}
else if (T->val > z->val)
{
return DeleteBst(T->lchild, z);
}
else
{
return DeleteBst(T->rchild, z);
}

}

具体关于BST树操作方面的描述请点击:http://www.cnblogs.com/bizhu/archive/2012/08/19/2646328.html 查看。个人感觉这篇文章讲的真心不错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值