定义
typedef int BSDataType;
typedef struct BSTreeNode
{
struct BSTreeNode* pLeft;
struct BSTreeNode* pRight;
BSDataType data;
}BSTreeNode, *pBSTreeNode;
创建新结点
pBSTreeNode BSBuyNode(BSDataType d)
{
pBSTreeNode newNode = (pBSTreeNode)malloc(sizeof(BSTreeNode));
if (NULL == newNode)
{
assert(0);
}
newNode->data = d;
newNode->pLeft = NULL;
newNode->pRight = NULL;
return newNode;
}
初始化
void InitBSTree(pBSTreeNode* bstree)
{
assert(bstree);
*bstree = NULL;
}
查找
非递归
pBSTreeNode FindBSTree(pBSTreeNode bstree, BSDataType d)
{
assert(bstree);
pBSTreeNode cur = bstree;
while (NULL != cur)
{
if (cur->data == d)
return cur;
else if (cur->data > d)
cur = cur->pLeft;
else
cur = cur->pRight;
}
return NULL;
}
递归
pBSTreeNode FindBSTreeNor(pBSTreeNode bstree, BSDataType d)
{
if (NULL == bstree)
{
return NULL;
}
else if (bstree->data == d)
{
return bstree;
}
else if (bstree->data > d)
{
return FindBSTreeNor(bstree->pLeft, d);
}
else
{
return FindBSTreeNor(bstree->pRight, d);
}
}
插入
非递归
void InsertBSTree(pBSTreeNode* bstree, BSDataType d)
{
if (NULL == (*bstree))
{
*bstree = BSBuyNode(d);
}
else
{
pBSTreeNode cur = *bstree;
pBSTreeNode parent = NULL;
while (NULL != cur)
{
parent = cur;
if (cur->data > d)
cur = cur->pLeft;
else if (cur->data < d)
cur = cur->pRight;
else
return;
}
cur = BSBuyNode(d);
if (parent->data > d)
parent->pLeft = cur;
else
parent->pRight = cur;
}
}
递归
void InsertBSTreeNor(pBSTreeNode* bstree, BSDataType d)
{
if (NULL == (*bstree))
{
*bstree = BSBuyNode(d);
return;
}
else if ((*bstree)->data > d)
{
InsertBSTreeNor(&((*bstree)->pLeft), d);
}
else if ((*bstree)->data < d)
{
InsertBSTreeNor(&((*bstree)->pRight), d);
}
else
{
return;
}
}
删除
非递归
void DeleteBSTree(pBSTreeNode* bstree, BSDataType d)
{
if (NULL == (*bstree))
{
return;
}
pBSTreeNode cur = *bstree;
pBSTreeNode parent = cur;
while (d != cur->data)
{
parent = cur;
if (cur->data > d)
cur = cur->pLeft;
else
cur = cur->pRight;
}
if (NULL == cur)
return;
if ((NULL == cur->pLeft && NULL != cur->pRight) ||
(NULL == cur->pLeft && NULL == cur->pRight))
{
if (cur == *bstree)
{
*bstree = cur->pRight;
}
else
{
if (cur == parent->pLeft)
parent->pLeft = cur->pRight;
else
parent->pRight = cur->pRight;
}
}
else if (NULL != cur->pLeft && NULL == cur->pRight)
{
if (cur == *bstree)
{
*bstree = cur->pLeft;
}
else
{
if (cur == parent->pLeft)
parent->pLeft = cur->pLeft;
else
parent->pRight = cur->pLeft;
}
}
else if (NULL != cur->pLeft && NULL != cur->pRight)
{
pBSTreeNode str = cur->pLeft;
parent = cur;
while (str->pRight)
{
parent = str;
str = str->pRight;
}
cur->data = str->data;
if (str == parent->pLeft)
parent->pLeft = str->pLeft;
else
parent->pRight = str->pRight;
cur = str;
}
free(cur);
cur = NULL;
}
递归
int DeleteBSTreeNor(pBSTreeNode* bstree, BSDataType d)
{
if (NULL == (*bstree))
{
return -1;
}
if (d < (*bstree)->data)
return DeleteBSTreeNor(&((*bstree)->pLeft), d);
else if (d >(*bstree)->data)
return DeleteBSTreeNor(&((*bstree)->pRight), d);
else
{
pBSTreeNode del = *bstree;
if (NULL == del->pLeft)
{
*bstree = del->pRight;
free(del);
}
else if (NULL == del->pRight)
{
*bstree = del->pLeft;
free(del);
return 1;
}
else
{
pBSTreeNode pcur = del->pRight;
while (pcur->pLeft)
{
pcur = pcur->pLeft;
}
del->data = pcur->data;
return DeleteBSTreeNor(&((*bstree)->pRight), del->data);
}
}
}
销毁
void DestroyBSTree(pBSTreeNode* bstree)
{
assert(bstree);
if (*bstree)
{
DestroyBSTree(&((*bstree)->pLeft));
DestroyBSTree(&((*bstree)->pRight));
free(*bstree);
*bstree = NULL;
}
}