#include<malloc.h>
#include<assert.h>
typedef int BSTreeNodeDataType;
typedef struct BSTreeNode
{
struct BSTreeNode* _left;
struct BSTreeNode* _right;
BSTreeNodeDataType _key;
}BSTreeNode;
BSTreeNode* BuyBSTreeNode(BSTreeNodeDataType x);// 创建二叉树节点
int BSTreeInsert(BSTreeNode** tree, BSTreeNodeDataType x);//插入二叉树节点
BSTreeNode* BSTreeFind(BSTreeNode* tree, BSTreeNodeDataType x);//查找节点
int BSTreeRemove(BSTreeNode** tree, BSTreeNodeDataType x);//删除节点 成功1 失败-1
void BSTreeDestory(BSTreeNode** tree);//销毁二叉树
void BSTreeInOrder(BSTreeNode* tree);//中序遍历二叉树
int BSTreeInsertR(BSTreeNode** tree, BSTreeNodeDataType x);//递归插入二叉树结点
const BSTreeNode* BSTreeFindR(BSTreeNode* tree, BSTreeNodeDataType x);//递归查找二叉树节点
int BSTreeRemoveR(BSTreeNode** tree, BSTreeNodeDataType x);//递归删除二叉树节点
BSTreeNode* BuyBSTreeNode(BSTreeNodeDataType x)
{
BSTreeNode* root = (BSTreeNode*)malloc(sizeof(BSTreeNode));
assert(root);
root->_left = NULL;
root->_right = NULL;
root->_key = x;
return root;
}
int BSTreeInsert(BSTreeNode** tree, BSTreeNodeDataType x)
{
if (*tree == NULL)
{
*tree = BuyBSTreeNode(x);
return 1;
}
else
{
BSTreeNode*pur = *tree;
BSTreeNode*cur = *tree;
while (cur)
{
if (x > cur->_key)
{
pur = cur;
cur = cur->_right;
}
else
{
pur = cur;
cur = cur->_left;
}
}
cur = BuyBSTreeNode(x);
if (cur->_key < pur->_key)
{
pur->_left = cur;
}
else
{
pur->_right = cur;
}
return 1;
}
return -1;
}
BSTreeNode* BSTreeFind(BSTreeNode* tree, BSTreeNodeDataType x)
{
if (tree == NULL)
{
return NULL;
}
else
{
BSTreeNode* cur = tree;
while (cur)
{
if (x > cur->_key)
{
cur = cur->_right;
}
else if (x < cur->_key)
{
cur = cur->_left;
}
else if (x == cur->_key)
return cur;
}
return NULL;
}
return NULL;
}
int BSTreeRemove(BSTreeNode** tree, BSTreeNodeDataType x)
{
BSTreeNode* de1;
BSTreeNode* sub;
BSTreeNode* cur = *tree;
BSTreeNode* parant = *tree;
if (cur == NULL)
{
return -1;
}
else
{
while (cur)
{
if (x < cur->_key)
{
parant = cur;
cur = cur->_left;
}
else if (x > cur->_key)
{
parant = cur;
cur = cur->_right;
}
else if (x == cur->_key)
{
if (cur->_left == NULL && cur->_right)//左为空有,右不为空
{
de1 = cur;
if (parant->_left == cur)
{
parant->_left = cur->_right;
}
else if (parant->_right == cur)
{
parant->_right = cur->_right;
}
else
{
*tree = cur->_right;
}
free(cur);
cur = NULL;
return 1;
}
else if (cur->_right == NULL && cur->_left)//左不为空,右为空
{
if (parant->_left == cur)
{
parant->_left = cur->_left;
}
else if (parant->_right == cur)
{
parant->_right = cur->_left;
}
else
{
*tree = cur->_left;
}
free(cur);
cur = NULL;
return 1;
}
else if (cur->_left == NULL && cur->_right == NULL)//左右都为空
{
if (parant->_left == cur)
{
parant->_left = NULL;
}
else if (parant->_right == cur)
{
parant->_right = NULL;
}
else
{
*tree = NULL;
}
free(cur);
cur = NULL;
return 1;
}
else//左和右均不为空
{
sub = cur;
sub = cur->_left;
if (sub->_right == NULL)
{
cur->_key = sub->_key;
cur->_left = sub->_left;
free(sub);
sub = NULL;
return 1;
}
else
{
de1 = sub;
while (de1->_right)
{
sub = de1;
de1 = de1->_right;
}
cur->_key = de1->_key;
sub->_right = de1->_left;
free(de1);
de1 = NULL;
return 1;
}
}
}
}
return -1;
}
}
void BSTreeDestory(BSTreeNode** tree)
{
if ((*tree) == NULL)
{
return;
}
BSTreeDestory(&(*tree)->_left);
BSTreeDestory(&(*tree)->_right);
free(*tree);
*tree = NULL;
}
void BSTreeInOrder(BSTreeNode* tree)
{
if (tree == NULL)
{
return;
}
else
{
BSTreeInOrder(tree->_left);
printf("%d", tree->_key);
BSTreeInOrder(tree->_right);
}
}
int BSTreeInsertR(BSTreeNode** tree, BSTreeNodeDataType x)
{
if (*tree == NULL)
{
*tree = BuyBSTreeNode(x);
return 1;
}
else if ((*tree)->_key > x)
{
return BSTreeInsertR(&((*tree)->_left), x);
}
else if ((*tree)->_key < x)
{
return BSTreeInsertR(&((*tree)->_right), x);
}
else
{
return -1;
}
}
const BSTreeNode* BSTreeFindR(BSTreeNode* tree, BSTreeNodeDataType x)
{
if (tree == NULL)
{
return NULL;
}
if (tree->_key > x)
{
return BSTreeFindR(tree->_left, x);
}
else if (tree->_key < x)
{
return BSTreeFindR(tree->_right, x);
}
else if (tree->_key == x)
{
return tree;
}
return NULL;
}
int BSTreeRemoveR(BSTreeNode** tree, BSTreeNodeDataType x)
{
if (*tree == NULL)
{
return -1;
}
else if ((*tree)->_key > x)
{
return BSTreeRemoveR(&((*tree)->_left), x);
}
else if ((*tree)->_key < x)
{
return BSTreeRemoveR(&((*tree)->_right), x);
}
else if((*tree)->_key == x)
{
BSTreeNode* de1 = *tree;
if ((*tree)->_left == NULL && (*tree)->_right == NULL)//左为空且右为空
{
*tree = NULL;
free(de1);
return 1;
}
else if ((*tree)->_left == NULL && (*tree)->_right)//左为空,右不为空
{
(*tree) = (*tree)->_right;
free(de1);
return 1;
}
else if ((*tree)->_right == NULL && (*tree)->_left)//左不为空,右不为空
{
(*tree) = (*tree)->_left;
free(de1);
return 1;
}
else
{
BSTreeNode* sub = de1;
de1 = de1->_left;
if (de1->_right == NULL)
{
sub->_key = de1->_key;
sub->_left = de1->_left;
free(de1);
return 1;
}
else
{
while (de1->_right)
{
sub = de1;
de1 = de1->_right;
}
(*tree)->_key = de1->_key;
sub->_right = NULL;
free(de1);
return 1;
}
}
}
return -1;
}
数据结构—搜索二叉树key模型(数据类型int)
最新推荐文章于 2024-06-17 00:00:26 发布