头文件 LinkQueue.h:
#include<iostream>
using namespace std;
typedef char DataType;
typedef struct bnode
{
DataType data;
struct bnode *lchild,*rchild;
} Bnode,*BTree;
typedef struct node
{
BTree data;
struct node *next;
}QNode,*PQNode;
typedef struct
{
PQNode front,rear;
}LinkQueue,*PLinkQueue;
//初始化队列
PLinkQueue Init_LinkQueue(void)
{
PLinkQueue Q;
Q=(PLinkQueue)malloc(sizeof(LinkQueue));
if(Q)
{
Q->front=NULL;
Q->rear=NULL;
}
return Q;
}
//判断队空(是否有元素)
int Empty_LinkQueue(PLinkQueue Q)
{
if(Q&&Q->front==NULL&&Q->rear==NULL)
return 1;
else
return 0;
}
//入队
int In_LinkQueue(PLinkQueue Q,BTree x)
{
PQNode p;
p=(PQNode)malloc(sizeof(QNode));
if(!p)
{
cout<<"内存溢出";
return 0;
}
p->data=x;
p->next=NULL;
if(Empty_LinkQueue(Q))
{
Q->rear=Q->front=p;
}
else
{
Q->rear->next=p;
Q->rear=p;
}
return 1;
}
//出队
int Out_LinkQueue(PLinkQueue Q,BTree *x)
{
PQNode p;
if(Empty_LinkQueue(Q))
{
cout<<"队空";
return 0;
}
*x=Q->front->data;
p=Q->front;
Q->front=Q->front->next;
free(p);
if(!Q->front)
{
Q->rear=NULL;
}
return 1;
}
//读队头元素
int GetTop_LinkQueue(PLinkQueue Q,BTree *x)
{
if(Empty_LinkQueue(Q))
{
cout<<"队空";
return 0;
}
*x=Q->front->data;
return 1;
}
//销毁队列
void Destroy_LinkQueue(PLinkQueue *Q)
{
PQNode p,s;
if(*Q)
{
s=(*Q)->front;
while(s)
{
p=s;
s=s->next;
free(p);
}
/*while((*Q)->front)
{
p=(*Q)->front;
(*Q)->front=(*Q)->front->next;
free(p);
}*/
free(*Q);
}
*Q=NULL;
return ;
}
源文件:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "LinkQueue.h"
#define MaxTreeNodeNum 100
using namespace std;
int count=0;
void Visit(char data)
{
cout<<data<<" ";
if(data=='H')
{
}
}
void PreOrder(BTree t)//先序遍历
{
if(t)
{
Visit(t->data);
PreOrder(t->lchild);
PreOrder(t->rchild);
}
}
void InOrder(BTree t)//中序遍历
{
if(t)
{
InOrder(t->lchild);
Visit(t->data);
InOrder(t->rchild);
}
}
void PostOrder(BTree t)//后序遍历
{
if(t)
{
PostOrder(t->lchild);
PostOrder(t->rchild);
Visit(t->data);
}
}
BTree CreatBinTree()//以加入空结点的先序序列输入,构建二叉链表
{
BTree t;//ABD#G###CE##FH###
char ch;
ch=getchar();
if(ch=='#') t=NULL;
else
{
t=(Bnode *)malloc(sizeof(Bnode));//生成节点空间
t->data=ch;
t->lchild=CreatBinTree();//左子树
t->rchild=CreatBinTree();//右子树
}
return t;
}
void Count_Tree(BTree t)//计数二叉树的结点个数
{
//static int count=0;Dev不支持全局标量
if(t)
{
Count_Tree(t->lchild);
//Visit(t->data);
count++;
Count_Tree(t->rchild);
}
}
/*递归方法实现计数二叉树的结点个数
int Count(BTree t)
{
int lcount,rcount;
if(t==NULL) return 0;
lcount=Count(t->lchild);
rcount=Count(t->rchild);
return lcount+rcount+1;
}
*/
int Height(BTree t)//计算二叉树的高度(深度)
{
int h1,h2;
if(t==NULL) return 0;
else
{
h1=Height(t->lchild);
h2=Height(t->rchild);
return (h1>h2)?h1+1:h2+1;
}
}
BTree CopyTree(BTree t)//复制二叉树
{
BTree p,q,s;
if(t==NULL) return NULL;
p=CopyTree(t->lchild);
q=CopyTree(t->rchild);
s=(Bnode *)malloc(sizeof(Bnode));
s->data=t->data;
s->lchild=p;
s->rchild=q;
return s;
}
void Levcount(BTree t,int L,int num[])//求链式二叉树每层结点个数 (L为当前层数,初始值为1,t初始指向根节点,num[]初始值为0)
{
if(t)
{
Visit(t->data);num[L]++;
Levcount(t->lchild,L+1,num);
Levcount(t->rchild,L+1,num);
}
}
void LayerOrder(PLinkQueue Q,BTree t)//层次遍历
{
BTree u;
u=(BTree)malloc(sizeof(Bnode));
In_LinkQueue(Q,t);//t进队列
while(!Empty_LinkQueue(Q))
{
Out_LinkQueue(Q,&u);
cout<<u->data<<" ";
if(u->lchild) In_LinkQueue(Q,u->lchild);
if(u->rchild) In_LinkQueue(Q,u->rchild);
}
Destroy_LinkQueue(&Q);
}
int main()
{
BTree t=CreatBinTree();ABD#G###CE##FH###//AB#D##CE##F##
PostOrder(t);后序遍历 //PreOrder(t);//InOrder(t);
Count_Tree(t);//结点数
cout<<count<<endl;//cout<<Count(t)<<endl;
PLinkQueue Q=Init_LinkQueue();
LayerOrder(Q,t);//层次遍历
cout<<Height(t)<<endl;//深度
system("pause");
return 0;
}