//对二叉树的创建,遍历二叉树,求树的深度,求结点个数,求叶子结点个数
#include <stdio.h>
#include<stdlib.h>
typedef char ElemType; //定义树的结点类型
typedef struct BiTNode //树的结构体定义
{
ElemType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
//创建空二叉树
void InitBiTree(BiTree&T)
{
T = NULL;
}
//二叉树的创建
void CreateBiTree(BiTree&T)
{
char ch;
scanf("%c",&ch);
if(ch == ' ')
{
T=NULL; //截止二叉树的建立
}
else
{
T = (BiTNode *) malloc(sizeof(BiTNode)); //申请结点空间
if(!T)
exit(0);
T->data = ch;
CreateBiTree(T->lchild); //构造左子树
CreateBiTree(T->rchild); //构造右子树
}
}
//二叉树的先序遍历
void PreOrderTraverse(BiTNode *p)
{
if(p != NULL)
{
printf("%c*",p->data);
PreOrderTraverse(p->lchild);
PreOrderTraverse(p->rchild);
}
}
//二叉树的中序遍历
void InOrderTraverse(BiTNode*p)
{
if(p != NULL)
{
InOrderTraverse(p->lchild);
printf("%c*",p->data);
InOrderTraverse(p->rchild);
}
}
//二叉树的后序遍历
voidPostOrderTraverse(BiTNode *p)
{
if(p != NULL)
{
PostOrderTraverse(p->lchild);
PostOrderTraverse(p->rchild);
printf("%c*",p->data);
}
}
//求二叉树的高度
int High(BiTNode *p)
{
int lh=0;
int rh=0;
if(p == NULL)
{
return 0;
}
lh=High(p->lchild);
rh=High(p->rchild);
if(lh>rh)
{
return lh+1;
}
else
{
return rh+1;
}
}
//求二叉树的结点数目
int Count(BiTree T)
{
if(T == NULL)
{
return 0;
}
return Count(T->lchild)+Count(T->rchild)+1;
}
//实现左右子树的交换
void exchange(BiTree T)
{
if(T == NULL)
{
return;
}
else
{
BiTree temp=T->lchild;
T->lchild = T->rchild;
T->rchild = temp;
exchange(T->lchild);
exchange(T->rchild);
}
}
//递归销毁一棵二叉树
void destroyBiTree(BiTree&T)
{
if(T)
{
destroyBiTree(T->lchild);
destroyBiTree(T->rchild);
delete T;
T = NULL;
}
}
//递归求二叉树的叶子结点个数
int leafCountOfBiTree(constBiTree &T)
{
if(T==NULL)
return 0;
if(T->lchild==NULL && T->rchild==NULL)
return 1;
return leafCountOfBiTree(T->lchild) +leafCountOfBiTree(T->rchild);
}
int main()
{
BiTree T;
printf("以先序创建树为:\n");
CreateBiTree(T);
printf("先序遍历为:\n");
PreOrderTraverse(T);
printf("\n");
printf("中序遍历为:\n");
InOrderTraverse(T);
printf("\n");
printf("后序遍历为:\n");
PostOrderTraverse(T);
printf("\n");
printf("树的深度为:\n");
int a=High(T);
printf("%d\n",a);
printf("结点的个数为:\n");
int b=Count(T);
printf("%d\n",b);
printf("叶子结点的个数为:\n");
int count=leafCountOfBiTree(T);
printf("%d\n",count);
printf("左右子树交换:\n");
exchange(T);
printf("左右子树交换后的先序遍历:\n");
PreOrderTraverse(T);
printf("\n");
printf("左右子树交换后的中序遍历:\n");
InOrderTraverse(T);
printf("\n");
printf("左右子树交换后的后序遍历:\n");
PostOrderTraverse(T);
printf("\n");
printf("销毁一棵二叉树:\n");
destroyBiTree(T);
PostOrderTraverse(T);
printf("\n");
return 0;
}
//输入一个先序和中序构造出整个树
#include<iostream>
using namespace std;
struct BinaryTreeNode
{
int m_nvalue;
BinaryTreeNode* m_pleft;
BinaryTreeNode* m_pright;
};
voidPostOrderTraverse(BinaryTreeNode *p)
{
if(p != NULL)
{
PostOrderTraverse(p->m_pleft);
PostOrderTraverse(p->m_pright);
printf("%d*",p->m_nvalue);
}
}
BinaryTreeNode*Construct(int* preorder,int* inorder,int length)
{
BinaryTreeNode* ConstructCore(int* startpreorder,int*endpreorder,int* startinorder,int* endinorder);
if(preorder==NULL||inorder==NULL||length<=0)
return NULL;
returnConstructCore(preorder,preorder+length-1,inorder,inorder+length-1);
}
BinaryTreeNode*ConstructCore(int* startpreorder,int* endpreorder,int* startinorder,int*endinorder)
{
int rootvalue=startpreorder[0];
BinaryTreeNode* root=new BinaryTreeNode();
root->m_nvalue=rootvalue;
root->m_pleft=root->m_pright=NULL;
if(startpreorder==endpreorder)
{
if(startinorder==endinorder&&*startpreorder==*startinorder)
return root;
else
throw std::exception("invalid input");
}
int* rootinorder=startinorder;
while(rootinorder<=endinorder&&*rootinorder!=rootvalue)
++rootinorder;
if(rootinorder==endinorder&& *rootinorder!=rootvalue)
throw std::exception("invalid input");
int leftlength=(int)(rootinorder-startinorder);
int* leftpreorderend=startpreorder+leftlength;
if(leftlength>0)
{
root->m_pleft=ConstructCore(startpreorder+1,leftpreorderend,startinorder,rootinorder-1);
}
if(leftlength<endpreorder-startpreorder)
{
root->m_pright=ConstructCore(leftpreorderend+1,endpreorder,rootinorder+1,endinorder);
}
return root;
}
int main()
{
int preorder[]={1,4,3,5,6,7,8,2};
int inorder[]={3,4,6,5,7,8,1,2};
int length=sizeof(preorder)/sizeof(int);
BinaryTreeNode*root= Construct(preorder,inorder,length);
PostOrderTraverse( root);
return 0;
}