二叉查找树的实现
1、头文件
(二叉查找树.h)
#pragma once
/*
* 二叉查找树ADT代码实现
*/
#include <stdio.h>
#include <stdlib.h>
struct Tree* makeEmpty(struct Tree* t);
struct Tree* find(int x, struct Tree* t);
struct Tree* findMin(struct Tree* t);
struct Tree* findMax(struct Tree* t);
struct Tree* insert(int x, struct Tree* t);
struct Tree* myDelete(int x, struct Tree* t);
void printTree(struct Tree* t);
struct Tree
{
int element;
struct Tree* left;
struct Tree* right;
};
2、main函数
(main.c)
#include "二叉查找树.h"
int main()
{
struct Tree* min;
struct Tree* max;
struct Tree* node;
struct Tree* root;
root = insert(6, NULL);
insert(4, root);
insert(3, root);
insert(2, root);
insert(1, root);
insert(8, root);
printTree(root);
//查找关键字为4的节点
node = find(4, root);
if (NULL == node) {
printf("not find!\n");
}
else {
printf("\nfind node is:%d\n", node->element);
}
//查找key最小的节点
min = findMin(root);
printf("min is:%d\n", min->element);
//查找key最大的节点
max = findMax(root);
printf("max is:%d\n", max->element);
//删除节点
myDelete(2, root);
printTree(root);
//清空树
makeEmpty(root);
return 0;
}
3、函数实现
3.1 makeEmpty.c
#include "二叉查找树.h"
struct Tree* makeEmpty(struct Tree* t)
{
if (t != NULL) {
makeEmpty(t->left);
makeEmpty(t->right);
free(t);
}
return NULL;
}
3.2 find.c
#include "二叉查找树.h"
struct Tree* find(int x, struct Tree* t)
{
if (NULL == t) {
return NULL;
}
if (x > t->element) {
return find(x, t->right);
}
else if (x < t->element) {
return find(x, t->left);
}
return t;
}
3.3 findMin.c
#include "二叉查找树.h"
struct Tree* findMin(struct Tree* t)
{
if (NULL == t) {
return NULL;
}
if (t->left != NULL) {
return findMin(t->left);
}
return t;
}
3.4 findMax.c
#include "二叉查找树.h"
struct Tree* findMax(struct Tree* t)
{
if (NULL == t) {
return NULL;
}
if (NULL != t->right)
{
return findMax(t->right);
}
return t;
}
3.5 insert.c
#include "二叉查找树.h"
struct Tree* insert(int x, struct Tree* t)
{
if (NULL == t) {
t = malloc(sizeof(struct Tree));
if (NULL == t) {
printf("内存空间不足,插入失败\n");
return NULL;
}
t->element = x;
t->left = NULL;
t->right = NULL;
}
else if (x > t->element) {
t->right = insert(x, t->right);
}
else if (x < t->element) {
t->left = insert(x, t->left);
}
return t;
}
3.6 myDelete.c
#include "二叉查找树.h"
struct Tree* myDelete(int x, struct Tree* t)
{
struct Tree* temp;
if (NULL == t) {
printf("Tree is empty!\n");
}
else if (x > t->element) {
t->right = myDelete(x, t->right);
}
else if (x < t->element) {
t->left = myDelete(x, t->left);
}
else if (t->left && t->right) {
temp = findMin(t->right);
t->element = temp->element;
t->right = myDelete(temp->element, t->right);
}
else {
temp = t;
if (t->left == NULL) {
t = t->right;
}
else if (t->right == NULL) {
t = t->left;
}
free(temp);
}
return t;
}
3.7 printTree.c
#include "二叉查找树.h"
//中序遍历打印树中的KEY
void printTree(struct Tree* t)
{
if (NULL == t) {
printf("Empty tree!\n");
return;
}
//打印左子树
if (t->left != NULL) {
printTree(t->left);
}
//打印节点
printf("%d ", t->element);
//打印右子树
if (t->right != NULL) {
printTree(t->right);
}
}
4、写在最后
为方便大家学习,我已经将原码上传到了GitHub,欢迎大家下载。链接:link(文件名:二叉查找树)。