二叉树的各种性质在这里不再重复,本文实现二叉树的基本操作,包括创建、前序输出、中序输出、后序输出、删除二叉树、叶子结点个数、叶子节点的值、交换左右子树
1.首先创建结构体:
typedef struct Tree_Node{
char ch;
struct Tree_Node *left;
struct Tree_Node *right;
}tree;
2.先序创建树
tree *create(){
tree *t;
char ch;
scanf("%c",&ch);
if(ch =='*')
t = NULL;
else{
t = (tree*)malloc(sizeof(tree));
if(!t)
return NULL;
t->ch = ch;
t->left = create();
t->right = create();
}
return t;
}
3.三种打印树的方式
前序输出:
void PreShow(tree *tree){
if(!tree)
return;
printf("%c ",tree->ch);
PreShow(tree->left);
PreShow(tree->right);
}
中序输出
void MidShow(tree *tree){
if(!tree)
return;
MidShow(tree->left);
printf("%c ",tree->ch);
MidShow(tree->right);
}
后序输出
void EndShow(tree *tree){
if(!tree)
return;
EndShow(tree->left);
EndShow(tree->right);
printf("%c ",tree->ch);
}
4.得到树的高度
int getHeight(tree *tree){
if(!tree)
return 0;
return getHeight(tree->left)>getHeight(tree->right)?getHeight(tree->left) + 1:getHeight(tree->right)+1;
}
6.得到树的节点总数
sum = left + right + 1
int getSum(tree *tree){
if(!tree)
return 0;
return getSum(tree->left) + getSum(tree->right) + 1;
}
5.叶子结点的总数
int getleaf(tree *tree){
if(!tree)
return ;
if(tree->left == NULL && tree->right == NULL)
return 1;
return getleaf(tree->left) + getleaf(tree->right);
}
6.打印叶子结点
void printleaf(tree *tree){
if(!tree)
return ;
if(tree->left == NULL && tree->right == NULL)
printf("%c ",tree->ch);
printleaf(tree->left);
printleaf(tree->right);
}
7.左右子树交换
void reverse(tree *tree){
if(!tree)
return;
tree *t = tree->left;
tree->left = tree->right;
tree->right = t;
if(tree->left)
reverse(tree->left);
if(tree->right)
reverse(tree->right);
}
8.删除二叉树
void clear(tree *tree){
if(tree == NULL)
return;
clear(tree->left);
clear(tree->right);
free(tree);
tree = NULL;
}
最后附上完整代码
#include <stdio.h>
#include <stdlib.h>
typedef struct Tree_Node{
char ch;
struct Tree_Node *left;
struct Tree_Node *right;
}tree;
tree *create(){
tree *t;
char ch;
scanf("%c",&ch);
if(ch =='*')
t = NULL;
else{
t = (tree*)malloc(sizeof(tree));
if(!t)
return NULL;
t->ch = ch;
t->left = create(t->left);
t->right = create(t->right);
}
return t;
}
void PreShow(tree *tree){
if(!tree)
return;
printf("%c ",tree->ch);
PreShow(tree->left);
PreShow(tree->right);
}
void MidShow(tree *tree){
if(!tree)
return;
MidShow(tree->left);
printf("%c ",tree->ch);
MidShow(tree->right);
}
void EndShow(tree *tree){
if(!tree)
return;
EndShow(tree->left);
EndShow(tree->right);
printf("%c ",tree->ch);
}
int getHeight(tree *tree){
if(!tree)
return 0;
return getHeight(tree->left)>getHeight(tree->right)?getHeight(tree->left) + 1:getHeight(tree->right)+1;
}
int getSum(tree *tree){
if(!tree)
return 0;
return getSum(tree->left) + getSum(tree->right) + 1;
}
void clear(tree *tree){
if(tree == NULL)
return;
clear(tree->left);
clear(tree->right);
free(tree);
tree = NULL;
}
void reverse(tree *tree){
if(!tree)
return;
tree *t = tree->left;
tree->left = tree->right;
tree->right = t;
if(tree->left)
reverse(tree->left);
if(tree->right)
reverse(tree->right);
}
int getleaf(tree *tree){
if(!tree)
return ;
if(tree->left == NULL && tree->right == NULL)
return 1;
return getleaf(tree->left) + getleaf(tree->right);
}
void printleaf(tree *tree){
if(!tree)
return ;
if(tree->left == NULL && tree->right == NULL)
printf("%c ",tree->ch);
printleaf(tree->left);
printleaf(tree->right);
}
int main(){
tree *t;
t = NULL;
//temp = NULL;
t = create();
printf("前序遍历:");
PreShow(t);
printf("\n后序遍历:");
EndShow(t);
printf("\n中序遍历:");
MidShow(t);
getHeight(t);
printf("\n高度为:%d",getHeight(t));
printf("\n节点总数为:%d\n",getSum(t));
printf("叶子节点数为:%d",getleaf(t));
printf("\n叶子节点为:");
printleaf(t);
// reverse(t);
// PreShow(t);
clear(t);
}