功能
二叉树操作:
- 创建二叉树
- 遍历二叉树(前序,中序,后续)
- 计算高度
- 计算结点数目
- 清空二叉树
- 空树判断
二叉搜索树操作:
- 插入
- 最值(最大值,最小值)
- 删除
代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct _BinNode BinNode;
struct _BinNode{
char ch;
BinNode * left;
BinNode * right;
};
//创建二叉树 指针的指针
void CreateNode(BinNode ** binNode){
char ch;
scanf("%c",&ch);
if(ch=='*'){
*binNode=NULL;
}
else{
*binNode=(BinNode*)malloc(sizeof(BinNode));
(*binNode)->ch=ch;
CreateNode(&(*binNode)->left);
CreateNode(&(*binNode)->right);
}
}
//前序遍历
void PreShow(BinNode *binNode){
if(!binNode){
return;
}
else{
printf("%c",binNode->ch);
PreShow(binNode->left);
PreShow(binNode->right);
}
}
//中序遍历
void MidShow(BinNode *binNode){
if(!binNode){
return;
}
else{
MidShow(binNode->left);
printf("%c",binNode->ch);
MidShow(binNode->right);
}
}
//后序遍历
void BackShow(BinNode *binNode){
if(!binNode){
return;
}
else{
BackShow(binNode->left);
BackShow(binNode->right);
printf("%c",binNode->ch);
}
}
//二叉树高度-最大值函数
int Max(int a,int b){
return a>b?a:b;
}
//二叉树高度
int BinHight(BinNode * binNode){
if(binNode){
return Max(BinHight(binNode->left),BinHight(binNode->right))+1;
}
else{
return 0;
}
}
//二叉树结点个数
int NodeNum(BinNode * binNode){
if(!binNode){
return 0;
}
else{
return NodeNum(binNode->left)+NodeNum(binNode->right)+1;
}
}
//清空二叉树
void BinFree(BinNode **pBinNode){
if(!*pBinNode){
return;
}
else{
BinFree(&(*pBinNode)->left);
BinFree(&(*pBinNode)->right);
free(*pBinNode);
*pBinNode=NULL;
}
}
//空树判断
bool BinIsEmpty(BinNode *binNode){
if(binNode){
return false;
}
else{
return true;
}
}
//二叉搜索树搜索
BinNode* BinSFind(char data,BinNode * binNode){
if(binNode==NULL){
return NULL;
}
if(data>binNode->ch){
return BinSFind(data,binNode->right);
}
else if(data<binNode->ch){
return BinSFind(data,binNode->left);
}
else{
return binNode;
}
}
//二叉搜索树最值
BinNode * BinSMax(BinNode * binNode){
if(binNode!=NULL)
while (binNode->right!=NULL)
binNode=binNode->right;
return binNode;
}
//二叉搜索树最小值
BinNode * BinSMin(BinNode * binNode){
if(binNode==NULL)return NULL;
else if(binNode->left==NULL)return binNode;
else return BinSMin(binNode->left);
}
//二叉搜索树插入算法
BinNode * BinSInsert(char data,BinNode * binNode){
if(binNode==NULL){
binNode=(BinNode*)malloc(sizeof(BinNode));
if(binNode){
binNode->ch=data;
binNode->left=binNode->right=NULL;
}
else{
printf("OUT OF SPACE");
}
}
else if(data>binNode->ch){
binNode->right=BinSInsert(data,binNode->right);
}
else if(data<binNode->ch){
binNode->left=BinSInsert(data,binNode->left);
}
return binNode;
}
//二叉搜索树 删除
BinNode * BinSDelete(char data,BinNode * binNode){
BinNode * TmpCell;
if(binNode==NULL){
printf("NOT FOUND!");
}
else if(data<binNode->ch){
binNode->left=BinSDelete(data,binNode->left);
}
else if(data>binNode->ch){
binNode->right=BinSDelete(data,binNode->right);
}
else if(binNode->left&&binNode->right){
TmpCell=BinSMin(binNode->right);
binNode->ch=TmpCell->ch;
binNode->right=BinSDelete(binNode->ch,binNode->right);
}
else {
TmpCell=binNode;
if(binNode->left==NULL){
binNode=binNode->right;
}
else if(binNode->right==NULL){
binNode=binNode->left;
}
free(TmpCell);
}
return binNode;
}
int main(){
char data='3';
BinNode * binNode;
printf("请输入二叉树前序遍历结果:");
CreateNode(&binNode);
printf("前序遍历结果:");
PreShow(binNode);
printf("\n中序遍历结果:");
MidShow(binNode);
printf("\n后序遍历结果:");
BackShow(binNode);
printf("\n高度:%d",BinHight(binNode));
printf("\n结点个数:%d\n",NodeNum(binNode));
//清空二叉树
//BinFree(&binNode);
//if(BinIsEmpty(binNode)){printf("\n二叉树已清空");}
//二叉搜索树
//************************************************************
//前序遍历输入:531**4**86*7**9**
setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区
//查找实现
printf("\n请输入查找字符:");
scanf("%c",&data);
BinNode * get=BinSFind(data,binNode);
if(get!=NULL){
printf("找到在%p位置,结果为%c",get,*get);
}
else{
printf("未找到!");
}
//最值
printf("\n最大值为:%c",BinSMax(binNode)->ch);
printf("\n最小值为:%c",BinSMin(binNode)->ch);
//插入
//BinSInsert('2',binNode);
//printf("\n插入2后遍历结果:");
//PreShow(binNode);
BinSDelete('8',binNode);
printf("\n删除8后前序遍历:");
PreShow(binNode);
BinSDelete('4',binNode);
printf("\n删除4后前序遍历:");
PreShow(binNode);
return 0;
}
结果
参考资料:数据结构预算法-C语言描述(原书第二版)