BST.h
#pragma once
#include<stdio.h>
typedef char BTreeType;
/*创建一个二叉搜索树的结构*/
typedef struct BTreeNode{
BTreeType data;
struct BTreeNode* left;
struct BTreeNode* right;
}BTreeNode;
/*初始化一个二叉搜索树*/
void BTreeInit(BTreeNode** btree);
/*在二叉搜索树中插入一个元素*/
void BTreeInsert1(BTreeNode** btree, BTreeType value);
/*使用迭代的方法插入*/
void BTreeInsert2(BTreeNode** btree, BTreeType value);
/*在二叉搜索树中删除一个元素*/
void RemoveBST(BTreeNode** btree, BTreeType del_value);
void DeleteNode(BTreeNode** pnode);
/*在二叉搜索树中查找一个元素*/
BTreeNode* BTreeFind1(BTreeNode* btree, BTreeType sel_value);
BTreeNode* BTreeFind2(BTreeNode* btree, BTreeType sel_value);
BST.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"BinaryTree.h"
/*初始化一个二叉搜索树*/
void BTreeInit(BTreeNode** btree) {
if (btree == NULL) {
return;
}
/*初始化,把根结点初始化为空*/
(*btree) = NULL;
}
/*创建一个二叉树结点*/
BTreeNode* CreateNode(BTreeType value) {
BTreeNode* ptr = (BTreeNode*)malloc(sizeof(BTreeNode));
if (ptr != NULL) {
ptr->data = value;
ptr->left = NULL;
ptr->right = NULL;
}
return ptr;
}
/*在二叉搜索树中插入一个元素*/
void BTreeInsert1(BTreeNode** btree, BTreeType value) {
if (btree == NULL) {
return;
}
/*
**若树为空,则把插入节点作为根结点插入空树
**若树不为空
**1.若插入结点的数据小于根节点的数据,则将其插入左子树
**2.若插入结点的数据大于根节点的数据,则将其插入右子树
*/
if (*btree == NULL) {
BTreeNode* insert = CreateNode(value);
(*btree) = insert;
}
else {
if (value < (*btree)->data) {
BTreeInsert1(&((*btree)->left), value);
}
else {
/*判断是不是相等*/
if (value == (*btree)->data) {
return;
}
BTreeInsert1(&((*btree)->right), value);
}
}
}
/*使用迭代的方法插入*/
void BTreeInsert2(BTreeNode** btree, BTreeType value) {
if (btree == NULL) {
return;
}
/*
**创建两个指针,curr用于检查树中的结点,link用于插入结点
**因为不能改变树根,所以用了两个指针
**cur是查找当前结点的插入位置,*link是指向cur指针的指针
*/
BTreeNode* cur;
BTreeNode** link = btree;
while ((cur = *link)) {
if (value < cur->data) {
link = &cur->left;
}
else {
if (value == cur->data) {
return;
}
link = &cur->right;
}
}
/*如果正常循环,到了这里,*cur已经是叶结点的某个子树了*/
BTreeNode* insert = CreateNode(value);
*link = insert;
}
/*在二叉搜索树中查找一个元素*/
BTreeNode* BTreeFind1(BTreeNode* btree, BTreeType sel_value) {
if (btree == NULL) {
return;
}
BTreeNode* cur = btree;
while (cur != NULL && sel_value != cur->data) {
if (sel_value < cur->data) {
/*查找的值在左子树*/
cur = cur->left;
}
else {
cur = cur->left;
}
}
if (cur != NULL) {
return cur;
}
else {
return NULL;
}
}
BTreeNode* BTreeFind2(BTreeNode* btree, BTreeType sel_value) {
if (btree == NULL) {
return NULL;
}
else if (sel_value == btree->data) {
return btree;
}
else if (sel_value < btree->data) {
BTreeFind2(btree->left, sel_value);
}
else if (sel_value > btree->data) {
BTreeFind2(btree->right, sel_value);
}
}
/*在二叉搜索树中删除一个元素*/
/*
**三种情况
**1.删除的元素结点为叶节点,直接删除
**2.删除的元素结点只有一个左子树结点或者右子树结点,直接删除然后链接删除元素结点的双亲结点
**3.删除的元素结点有两个子结点
** 方法1:找出需要删除的元素结点的左子树中最大的元素结点,并用这个值代替被删除的结点
** 方法2:暂时还不会
*/
void RemoveBST(BTreeNode** btree, BTreeType value) {
if (btree == NULL) {
return;
}
if (*btree == NULL) {
return;
}
else {
if (value == (*btree)->data) {
DeleteNode(btree);
printf("删除成功!");
}
else if (value < (*btree)->data) {
RemoveBST(&(*btree)->left, value);
}
else if (value >(*btree)->data) {
RemoveBST(&(*btree)->right, value);
}
}
}
/*销毁某个结点*/
void DestoryNode(BTreeNode* del_node) {
if (del_node == NULL) {
return;
}
free(del_node);
}
/*删除某个结点*/
void DeleteNode(BTreeNode** pnode) {
if (pnode == NULL) {
return;
}
if (*pnode == NULL) {
return;
}
BTreeNode* qnode, *snode;
if (!(*pnode)->left && !(*pnode)->right) {
/*当前结点为叶节点*/
(*pnode) = NULL;
}
else if (!(*pnode)->left) {
/*
**当前结点左子树为空
**直接删除把右子树的结点链接到当前结点的双亲结点上
*/
//传值删除
qnode = (*pnode)->right;
(*pnode)->data = qnode->data;
(*pnode)->left = qnode->left;
(*pnode)->right = qnode->right;
DestoryNode(qnode);
}
else if (!(*pnode)->right) {
/*当前结点右子树为空*/
qnode = (*pnode)->left;
(*pnode)->data = qnode->data;
(*pnode)->left = qnode->left;
(*pnode)->right = qnode->right;
DestoryNode(qnode);
}
else {
/*
**当前结点左右子树都不为空
**找出当前结点的左子树中最大的结点
*/
qnode = *pnode;
snode = (*pnode)->left;
while (snode->right) {
qnode = snode;
snode = snode->right;
}
(*pnode)->data = snode->data;
/*
**如果执行循环,表示需要删除的结点的左子树没有右孩子,此时需要接左子树
**如果未执行循环,此时需要接右子树
*/
if (qnode == (*pnode)) {
/*此时表示未进入循环*/
qnode->left = snode->left;
DestoryNode(snode);
}
else {
qnode->right = snode->left;
DestoryNode(snode);
}
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"BinaryTree.h"
#include<stdlib.h>
#define TESTHEAD printf("------------------%s------------------\n\n",__FUNCTION__)
void TestInsert() {
BTreeNode* btree;
BTreeInit(&btree);
TESTHEAD;
BTreeInsert1(&btree, 'd');
BTreeInsert1(&btree, 'b');
BTreeInsert1(&btree, 'e');
BTreeInsert1(&btree, 'a');
BTreeInsert1(&btree, 'g');
BTreeInsert1(&btree, 'f');
BTreeInsert2(&btree, 'c');
BTreeInsert2(&btree, 'h');
BTreeInsert2(&btree, 'i');
}
void TestFind() {
BTreeNode* btree;
BTreeInit(&btree);
TESTHEAD;
BTreeInsert1(&btree, 'd');
BTreeInsert1(&btree, 'b');
BTreeInsert1(&btree, 'e');
BTreeInsert1(&btree, 'a');
BTreeInsert1(&btree, 'g');
BTreeInsert1(&btree, 'f');
BTreeNode* value;
value = BTreeFind1(btree, 'a');
printf("expect 'a', actual %c\n", value->data);
value = BTreeFind2(btree, 'e');
printf("expect 'e', actual %c \n", value->data);
}
void TestDelete() {
BTreeNode* btree;
BTreeInit(&btree);
TESTHEAD;
BTreeInsert1(&btree, 'g');
BTreeInsert1(&btree, 'd');
BTreeInsert1(&btree, 'b');
BTreeInsert1(&btree, 'a');
BTreeInsert1(&btree, 'c');
BTreeInsert1(&btree, 'e');
BTreeInsert1(&btree, 'f');
BTreeInsert1(&btree, 'h');
BTreeInsert1(&btree, 'j');
BTreeInsert1(&btree, 'i');
BTreeInsert1(&btree, 'k');
RemoveBST(&btree, 'b');
}
int main() {
TestInsert();
TestFind();
TestDelete();
system("pause");
return 0;
}