版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35691619/article/details/78907340
只写一个先序遍历,中序和后序就顺序不同
结构体,基本数据类型和两个指向左右节点的指针
typedef struct TNode * Position;
typedef Position BinTree;
struct TNode {
int data;
BinTree left;
BinTree right;
};
插入
BinTree insert(BinTree BST, int x) {
if (!BST) {
BST = (BinTree)malloc(sizeof(struct TNode)); //为空则申请空间
BST->data = x;
BST->left = BST->right = NULL; //将子节点置为空
} else {
if (x < BST->data) {
BST->left = insert(BST->left, x); //左递归
} else if (x > BST->data){
BST->right = insert(BST->right, x);
}
return BST;
}
}
最大值
//比BST->data大的都在右子树,递归遍历
Position findMax(BinTree BST) {
if (!BST) {
return NULL;
} else if (!BST->right) { //右子树为空
return BST;
} else {
return findMax(BST->right);
}
}
最小值
//比BST->data小的都在左子树,递归遍历
Position findMin(BinTree BST) {
if (!BST) {
return NULL;
} else if (!BST->left) { //左子树为空
return BST;
} else {
return findMin(BST->left);
}
}
删除
BinTree deleteValue(BinTree BST, int x) {
Position pos;
if (!BST) {
printf("未找到指定元素\n");
} else {
if (x < BST->data) {
BST->left = deleteValue(BST->left, x);
} else if (x > BST->data) {
BST->right = deleteValue(BST->right, x);
} else {
if (BST->left && BST->right) { //存在两个子节点时
pos = findMin(BST->right); //找到右子树的最小值
BST->data = pos->data;
BST->right = deleteValue(BST->right, BST->data);
} else { //存在一个子节点或者无子节点的时候
pos = BST;
if (!BST->left) {
BST = BST->right;
} else {
BST = BST->left;
}
free(pos);
}
}
}
return BST;
}
查找特定元素
Position find(BinTree BST, int x) {
if (!BST) {
return NULL;
}
if (BST->data < x) { //小于在左,大于在右
return find(BST->right, x);
} else if (BST->data > x) {
return find(BST->left, x);
} else {
return BST;
}
}
具体实现如下
#include<stdio.h>
#include<stdlib.h>
typedef struct TNode * Position;
typedef Position BinTree;
struct TNode {
int data;
BinTree left;
BinTree right;
};
void preOrder(BinTree BST) {
if (!BST) {
return ;
} else {
printf("%d ", BST->data);
preOrder(BST->left);
preOrder(BST->right);
}
}
BinTree insert(BinTree BST, int x) {
if (!BST) {
BST = (BinTree)malloc(sizeof(struct TNode)); //为空则申请空间
BST->data = x;
BST->left = BST->right = NULL; //将子节点置为空
} else {
if (x < BST->data) {
BST->left = insert(BST->left, x);
} else if (x > BST->data){
BST->right = insert(BST->right, x);
}
return BST;
}
}
Position findMin(BinTree BST) {
if (!BST) {
return NULL;
} else if (!BST->left) {
return BST;
} else {
return findMin(BST->left);
}
}
Position findMax(BinTree BST) {
if (!BST) {
return NULL;
} else if (!BST->right) {
return BST;
} else {
return findMax(BST->right);
}
}
Position find(BinTree BST, int x) {
if (!BST) {
return NULL;
}
if (BST->data < x) { //小于在左,大于在右
return find(BST->right, x);
} else if (BST->data > x) {
return find(BST->left, x);
} else {
return BST;
}
}
BinTree deleteValue(BinTree BST, int x) {
Position pos;
if (!BST) {
printf("未找到指定元素\n");
} else {
if (x < BST->data) {
BST->left = deleteValue(BST->left, x);
} else if (x > BST->data) {
BST->right = deleteValue(BST->right, x);
} else {
if (BST->left && BST->right) { //存在两个子节点时
pos = findMin(BST->right); //找到右子树的最小值
BST->data = pos->data;
BST->right = deleteValue(BST->right, BST->data);
} else { //存在一个子节点或者无子节点的时候
pos = BST;
if (!BST->left) {
BST = BST->right;
} else {
BST = BST->left;
}
free(pos);
}
}
}
return BST;
}
int main(void)
{
BinTree BST, MinP, MaxP, Tmp;
int x;
int N, i, flag;
BST = NULL;
printf("输入初始元素个数:\n");
scanf("%d", &N);
printf("输入各个元素:\n");
for (int i = 0; i < N; i++) {
scanf("%d", &x);
BST = insert(BST, x);
}
printf("1.插入元素 2.删除元素 3.查找最小值\n");
printf("4.查找最大值 5.查找指定元素 6.先序输出 7.退出\n");
while (scanf("%d", &flag) != EOF && flag != 7) {
switch (flag) {
case 1: printf("输入插入元素:\n");scanf("%d", &x);BST = insert(BST, x);break;
case 2: printf("输入删除元素:\n");scanf("%d", &x);BST = deleteValue(BST, x);break;
case 3: MinP = findMin(BST); printf("最小值为:%d\n\n", MinP->data);break;
case 4: MaxP = findMax(BST); printf("最大值为:%d\n\n", MaxP->data);break;
case 5: printf("输入查找元素:\n");scanf("%d", &x);Tmp = find(BST, x); //用一个容器,不然在一个元素未找到时会被置为NULL,先序遍历无法输出
if (!Tmp) {printf("未找到该元素\n\n");} else {printf("找到%d\n\n", Tmp->data);} break;
case 6: printf("先序遍历如下:\n"); preOrder(BST);printf("\n\n");break;
default: printf("请输入合法操作!\n\n");break;
}
printf("1.插入元素 2.删除元素 3.查找最小值\n");
printf("4.查找最大值 5.查找指定元素 6.先序输出 7.退出\n");
}
return 0;
}
测试如下