#include<stdio.h>
#include <cstdlib>
#include <iostream>
#include <stack>
#include<queue>
using namespace std;
//二叉树定义
typedef char ElementType;
typedef struct Node *Position;
typedef Position BinTree;
struct Node
{
ElementType data;
BinTree Left;
BinTree Right;
} BiTreeNode, *BiTree;
//递归的建立一棵二叉树
//输入为二叉树的先序序列
void CreateBinTree(BinTree &T)
{
ElementType data;
data = getchar();
if(data == ‘#‘)
T = NULL;
else
{
T = new Node;
T->data = data;
CreateBinTree(T->Left);
CreateBinTree(T->Right);
}
}
void CreatBinTree_2(BinTree &rt,int n)///层序遍历建树
{
char root[2];
scanf("%s",root);
rt=new Node;
rt->data=root[0];
for(int i=2; i<=n; i++)
{
BinTree T=rt;
char tmp[2];
int num=0,a[16];
scanf("%s",tmp);
int ii=i;
while(ii)
{
a[num++]=ii%2;
ii/=2;
}
BinTree node=new Node;
node->data=tmp[0];
node->Right=NULL;
node->Left=NULL;
for(int j=num-2; j>0; j--)
if(a[j])T=T->Right;
else T=T->Left;
if(a[0])T->Right=node;
else T->Left=node;
}
}
int Nodenum(BinTree root)//二叉树节点数目
{
if(root == NULL) return 0;
else return 1+Nodenum(root->Left)+Nodenum(root->Right);
}
//递归销毁一棵二叉树
void destroyBinTree(BinTree &T)
{
if(T)
{
destroyBinTree(T->Left);
destroyBinTree(T->Right);
delete T;
T = NULL;
}
}
//递归先序遍历二叉树
void preOrderTraverse(const BinTree &T)
{
if(T)
{
// cout<<T->data<<" ";//输出根节点值
printf("%c ",T->data);
preOrderTraverse(T->Left);//遍历左子树
preOrderTraverse(T->Right);//遍历右子树
}
}
//递归中序遍历二叉树
void inOrderTraverse(const BinTree &T)
{
if(T)
{
inOrderTraverse(T->Left);//遍历左子树
// cout<<T->data<<" ";//输出根节点值
printf("%c ",T->data);
inOrderTraverse(T->Right);//遍历右子树
}
}
//递归后序遍历二叉树
void postOrderTraverse(const BinTree &T)
{
if(T)
{
postOrderTraverse(T->Left);//遍历左子树
postOrderTraverse(T->Right);//遍历右子树
printf("%c ",T->data);
// cout<<T->data<<" ";//输出根节点值
}
}
//递归求树的深度
int depthOfBinTree(const BinTree &T)
{
int ldepth;
int rdepth;
if(T==NULL)//空树
return 0;
ldepth = depthOfBinTree(T->Left);
rdepth = depthOfBinTree(T->Right);
return (ldepth>rdepth)?(ldepth+1):(rdepth+1);
}
//递归求二叉树的叶子结点个数
int leafCountOfBinTree(const BinTree &T)
{
if(T==NULL)
return 0;
if(T->Left==NULL && T->Right==NULL)
return 1;
return leafCountOfBinTree(T->Left) + leafCountOfBinTree(T->Right);
}
void exchangeChild(BinTree &T)///交换左右子树
{
if(T)
{
BinTree temp = NULL;
if(T->Left ||T->Right)
{
temp = T->Left;
T->Left = T->Right;
T->Right = temp;
exchangeChild(T->Left);
exchangeChild(T->Right);
}
}
}
void LevelOrderTraverse(BinTree T)///层序遍历
{
//Visit是对节点操作的应用函数,
//在这里,对每个数据元素调用函数Visit,也即是遍历了该节点
queue<BinTree>q;
BinTree p;
if(T)
{
q.push(T);
while(!q.empty())
{
p=q.front();
q.pop();
printf("%c ",p->data);
if(p->Left!=NULL) q.push(p->Left);
if(p->Right!=NULL) q.push(p->Right);
}
printf("\n");
}
}
int main()
{
BinTree T = NULL;
CreateBinTree(T);//建立二叉树 AB#D##CE###
printf("先序遍历: "); //先序遍历
preOrderTraverse(T);
printf("\n");
printf("中序遍历: ");//中序遍历
inOrderTraverse(T);
printf("\n");
printf("后序遍历: ");//后序遍历
postOrderTraverse(T);
printf("\n");
printf("层序遍历: ");
LevelOrderTraverse(T);
printf("交换左右子树: \n");
exchangeChild(T);
printf("先序遍历: "); //先序遍历
preOrderTraverse(T);
printf("\n");
printf("中序遍历: ");//中序遍历
inOrderTraverse(T);
printf("\n");
printf("后序遍历: ");//后序遍历
postOrderTraverse(T);
printf("\n");
printf("层序遍历: ");
LevelOrderTraverse(T);
printf("深度: %d\n",depthOfBinTree(T));//树的高度
printf("叶子结点数: %d\n",leafCountOfBinTree(T));//叶子结点数
printf("总结点数量: %d\n",Nodenum(T));
destroyBinTree(T);//销毁二叉树,释放空间
///================================================================
int n;
scanf("%d",&n);
BinTree TT=NULL;
CreatBinTree_2(TT,n);
printf("先序遍历: "); //先序遍历
preOrderTraverse(TT);
printf("\n");
printf("中序遍历: ");//中序遍历
inOrderTraverse(TT);
printf("\n");
printf("后序遍历: ");//后序遍历
postOrderTraverse(TT);
printf("\n");
printf("深度: %d\n",depthOfBinTree(TT));//树的高度
printf("叶子结点数: %d\n",leafCountOfBinTree(TT));//叶子结点数
printf("总结点数量: %d\n",Nodenum(TT));
destroyBinTree(TT);//销毁二叉树,释放空间
system("PAUSE");
return EXIT_SUCCESS;
}
原文地址:https://www.cnblogs.com/astonc/p/9906518.html