65,211
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <string>
using namespace std;
//二叉树链存储结构类型定义
typedef char ElemType;
const int MaxSize = 1000;
const int MaxWidth = 60;
struct BTNode
{
ElemType data;
BTNode *lchild, *rchild;
};
//求二叉树高度运算
//递归模型
//f(bt)=Max{f(bt->lchild), f(bt->rchild)}+1; f(NULL) = 0
int BTHeight(BTNode *bt);
//求二叉树结点个数运算
//递归模型
//f(bt) = f(bt->lchild)+f(bt->rchild)+1;f(NULL) = 0
int NodeCount(BTNode *bt);
//求二叉树叶子结点个数运算
//递归模型
//f(bt) = f(bt->lchild)+f(bt->rchild)
//f(bt) = 0,bt=NULL; f(bt) = 1, bt为叶子结点
int LeafCount(BTNode *&bt);
//二叉树的先根遍历
//若二叉树非空
//1,访问根结点 2,先根遍历左子树 3,先根遍历右子树
void PreOrder(BTNode *bt);
//中根遍历
//若二叉树非空
//1,中根遍历左子树 2,访问根结点 3,中根遍历右子树
void InOrder(BTNode *bt);
//后根遍历
//若二叉树非空
//1,后根遍历左子树 2,后根遍历右子树 3,访问根结点
void PostOrder(BTNode *bt);
//由先根序列和中根序列构造二元树
void PreInOrder(BTNode *&bt, char *pre, char *in);
//由中根序列和后根序列构造二元树
void PostInOrder(BTNode *&bt, char *in, char *post);
//-----------------------------------------------------------------------------
//主函数
int main()
{
BTNode *bt;
char ch[MaxSize];
char pre[100];
char in[100];
char *a, *b;
int n;
cout << "请输入先根序列和中根序列:" << endl;
cin >> pre >> in;
a = pre;
b = in;
PreInOrder(bt, a, b);
cout << "\n叶子结点的个数\n";
cout << LeafCount(bt);
cout << "\n二叉树高度\n";
cout << BTHeight(bt);
cout << "\n二叉树结点个数\n";
cout << NodeCount(bt) << "\n";
cout << "先根遍历:";
PreOrder(bt);
cout << "\n中根遍历:";
InOrder(bt);
cout << "\n后根遍历:";
PostOrder(bt);
cout << endl;
return 0;
}
//求二叉树高度运算
//递归模型
//f(bt)=Max{f(bt->lchild), f(bt->rchild)}+1; f(NULL) = 0
int BTHeight(BTNode *bt)
{
int lchilddep, rchilddep;
if(bt == NULL)
return 0;//空树的高度为0
lchilddep = BTHeight(bt->lchild);//求左子树的高度为lchilddep
rchilddep = BTHeight(bt->rchild);//求右子树的高度为rchilddep
return (lchilddep>rchilddep)?(lchilddep+1):(rchilddep+1);
}
//求二叉树结点个数运算
//递归模型
//f(bt) = f(bt->lchild)+f(bt->rchild)+1;f(NULL) = 0
int NodeCount(BTNode *bt)
{
int num1, num2;
if(bt == NULL)
return 0;
num1 = NodeCount(bt->lchild);
num2 = NodeCount(bt->rchild);
return (num1+num2+1);
}
//求二叉树叶子结点个数运算
//递归模型
//f(bt) = f(bt->lchild)+f(bt->rchild)
//f(bt) = 0,bt=NULL; f(bt) = 1, bt为叶子结点
int LeafCount(BTNode *&bt)
{
int num1, num2;
if(bt == NULL)
return 0;
if(bt->lchild == NULL && bt->rchild == NULL)
return 1;
num1 = LeafCount(bt->lchild);
num2 = LeafCount(bt->rchild);
return (num1+num2);
}
//二叉树的先根遍历
//若二叉树非空
//1,访问根结点 2,先根遍历左子树 3,先根遍历右子树
void PreOrder(BTNode *bt)
{
if(bt != NULL){
cout << bt->data;
PreOrder(bt->lchild);
PreOrder(bt->rchild);
}
}
//中根遍历
//若二叉树非空
//1,中根遍历左子树 2,访问根结点 3,中根遍历右子树
void InOrder(BTNode *bt)
{
if(bt != NULL){
InOrder(bt->lchild);
cout << bt->data;
InOrder(bt->rchild);
}
}
//后根遍历
//若二叉树非空
//1,后根遍历左子树 2,后根遍历右子树 3,访问根结点
void PostOrder(BTNode *bt)
{
if(bt != NULL){
PostOrder(bt->lchild);
PostOrder(bt->rchild);
cout << bt->data;
}
}
//由先根序列和中根序列构造二元树
void PreInOrder(BTNode *&bt, char *pre, char *in)
{
char *rsub, *rsub0;
if(in[0] == '\0')
bt = NULL;
else
{
bt = new BTNode;
bt->data = pre[0];
rsub = strchr(in, pre[0]);
rsub0 = pre+(rsub-in);
*rsub = '\0';
pre++;
rsub++;
rsub0++;
PreInOrder(bt->lchild, pre, in);
PreInOrder(bt->rchild, rsub0, rsub);
}
}
#include <iostream>
using namespace std;
class treenode
{
friend void visittree(treenode *root);
friend treenode * creattree(char pre[], char in[], int n);
friend treenode * constructtree(char pre[], char in[], int pstart,int pend, int istart, int iend);
private:
treenode *left;
treenode *right;
char data;
};
void visittree(treenode *root)
{
if(root)
{
cout << root->data;
visittree(root->left);
visittree(root->right);
}
}
treenode * creattree(char pre[], char in[], int n)
{
return constructtree(pre, in, 0, n, 0, n);
}
treenode * constructtree(char pre[], char in[], int pstart,int pend, int istart, int iend)
{
treenode *root = new treenode;
root->data = pre[pstart];
int mid = istart;
while (in[mid] != pre[pstart])
mid++;
if(mid == istart)
root->left = NULL;
else
root->left = constructtree(pre, in, pstart+1,pstart+mid-istart,istart, mid-1);
if(mid == iend)
root->right = NULL;
else
root->right = constructtree(pre, in, pstart+mid-istart+1,pend,mid+1, iend);
return root;
}
//>>>>>>>>>>>>>>//
//#include "treenode.h"
#define N 8
int main()
{
treenode * A;
char pre[N] = "ABCDEFG";
char in[N] = "BCAEDFG";
/*
cout << "enter the array of pre :";
for(int i=1; i <= N; ++i)
cin >> pre[i];
cout << "enter the array of in :";
for(int j=1; j <= N; ++j)
cin >> in[j];
*/
A = creattree(pre, in, N-1);
visittree(A);
return 0;
}