
#pragma once
#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#define T int
#define FALST 0
#define TRUE 1
#define BOOL int
typedef struct BSTreeNode
{
T data;
BSTreeNode *leftChild;
BSTreeNode *rightChild;
}BSTreeNode;
typedef struct BSTree
{
BSTreeNode *root;
}BSTree;
void InitBSTree(BSTree *bst);
BOOL InsertBSTree(BSTree *bst, T x);
//BOOL InsertBSTree(BSTreeNode **t, T x);
BOOL InsertBSTree(BSTreeNode *&t, T x);
//求最大最小值
T Min(BSTree *bst);
T Min(BSTreeNode *t);
T Max(BSTree *bst);
T Max(BSTreeNode *t);
//排序,即进行中序遍历
void Sort(BSTree *bst);
void Sort(BSTreeNode *t);
//搜索值为key的节点,找不到则返回空
BSTreeNode* SearchBST(BSTree *bst,T key);
BSTreeNode* SearchBST(BSTreeNode *t,T key);
//制空二叉树
void MakeEmptyBSTree(BSTree *bst);
void MakeEmptyBSTree(BSTreeNode **t);
//删除值为key的节点
BOOL RemoveBSTree(BSTree *bst,T key);
BOOL RemoveBSTree(BSTreeNode *&t,T key);
#include"BSTree.h"
void InitBSTree(BSTree *bst)
{
bst->root = NULL;
}
//利用二级指针完成插入
//BOOL InsertBSTree(BSTree *bst, T x)
//{
// return InsertBSTree(&bst->root, x);
//}
//BOOL InsertBSTree(BSTreeNode **t, T x)
//{
// if ((*t) == NULL)
// {
// (*t) = (BSTreeNode*)malloc(sizeof(BSTreeNode));
// assert((*t) != NULL);
// (*t)->data = x;
// (*t)->leftChild = NULL;
// (*t)->rightChild = NULL;
// }
// else if ((*t)->data > x)
// return InsertBSTree(&(*t)->leftChild, x);
// else if ((*t)->data < x)
// return InsertBSTree(&(*t)->rightChild, x);
// return TRUE;
//}
//利用c++的引用完成插入操作
BOOL InsertBSTree(BSTree *bst, T x)
{
return InsertBSTree(bst->root, x);
}
BOOL InsertBSTree(BSTreeNode *&t, T x)
{
if (t == NULL)
{
t = (BSTreeNode*)malloc(sizeof(BSTreeNode));
assert(t != NULL);
t->data = x;
t->leftChild = NULL;
t->rightChild = NULL;
}
else if (t->data > x)
return InsertBSTree(t->leftChild, x);
else if (t->data < x)
return InsertBSTree(t->rightChild, x);
return TRUE;
}
//求最大值,即二叉搜索树的最右节点的值,求最下值,即二叉搜索树的最左节点的值
T Min(BSTree *bst)
{
assert(bst->root != NULL);
return Min(bst->root);
}
T Min(BSTreeNode *t)
{
BSTreeNode *p = t->leftChild;
while (p->leftChild != NULL)
{
p = p->leftChild;
}
return p->data;
}
T Max(BSTree *bst)
{
assert(bst->root != NULL);
return Max(bst->root);
}
T Max(BSTreeNode *t)
{
BSTreeNode *p = t->rightChild;
while (p->rightChild != NULL)
p = p->rightChild;
return p->data;
}
void Sort(BSTree *bst)
{
Sort(bst->root);
printf("\n");
}
void Sort(BSTreeNode *t)
{
if (t != NULL)
{
Sort(t->leftChild);
printf("%d ", t->data);
Sort(t->rightChild);
}
}
BSTreeNode* SearchBST(BSTree *bst,T key)
{
return SearchBST(bst->root,key);
}
BSTreeNode* SearchBST(BSTreeNode *t,T key)
{
if (t == NULL)
return NULL;
if (t->data == key)
return t;
//比key大,在左子树早,比key小在右子数找
if (t->data > key)
{
return SearchBST(t->leftChild, key);
}
else
{
return SearchBST(t->rightChild, key);
}
}
void MakeEmptyBSTree(BSTree *bst)
{
MakeEmptyBSTree(&bst->root);
}
void MakeEmptyBSTree(BSTreeNode **t)
{
if ((*t) != NULL)
{
MakeEmptyBSTree(&(*t)->leftChild);
MakeEmptyBSTree(&(*t)->rightChild);
free((*t));
(*t) = NULL;
}
}
BOOL RemoveBSTree(BSTree *bst,T key)
{
return RemoveBSTree(bst->root, key);
}
BOOL RemoveBSTree(BSTreeNode *&t, T key)
{
if (t == NULL)
return FALST;
if (t->data > key)
return RemoveBSTree(t->leftChild, key);
else if (t->data < key)
return RemoveBSTree(t->rightChild, key);
else
{
BSTreeNode *p;
//如果删除节点有左右子树,则删除该节点,下右子数中找最小节点代替
if (t->leftChild != NULL && t->rightChild != NULL)
{
p = t->rightChild;
while (p->leftChild != NULL)
p = p->leftChild;
t->data = p->data;
//在这个子树中执行递归,知道转换为第二种情况h
RemoveBSTree(t->rightChild, p->data);
}
else
{
p = t;
if (t->leftChild == NULL)
//这一句使得t的右子数指向t的父节点
t = t->rightChild;
else if (t->rightChild == NULL)
t = t->leftChild;
free(p);
p = NULL;
}
}
return TRUE;
}
#include"BSTree.h"
void main()
{
BSTree bst;
InitBSTree(&bst);
T ar[] = { 45, 12, 53, 3, 37, 100, 24, 61, 90, 78 };
int n = sizeof(ar) / sizeof(int);
for (int i = 0; i < n; ++i)
{
InsertBSTree(&bst, ar[i]);
}
T min = Min(&bst);
T max = Max(&bst);
printf("min=%d,max=%d\n", min, max);
Sort(&bst);
RemoveBSTree(&bst, 37);
printf("hello");
}