标签:数据 sel 二叉链表 abc iostream cas [] delete c#
方法一:利用遍历
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
typedef char TElemType;
//二叉树的二叉链表存储表示
typedef struct BiNode {
TElemType data; //结点数据域
struct BiNode *lchild, *rchild; //左右孩子指针
} BiTNode, *BiTree;
void CreateBiTree(BiTree &T)
{
//按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
TElemType ch;
if(!(cin >> ch)) exit(0);
if(ch == ‘#‘) T = NULL; //递归结束,建空树
else {
T = new BiTNode;
T->data = ch; //生成根结点
CreateBiTree(T->lchild); //递归创建左子树
CreateBiTree(T->rchild); //递归创建右子树
} //else
} //CreateBiTree
int level[200];//假定小于200层
void PreOrderTraverse(BiTree T, int dep)
{
if(T) {
level[dep]++;
PreOrderTraverse(T->lchild, dep + 1);
PreOrderTraverse(T->rchild, dep + 1);
}
}
int width(BiTree T)
{
int i = 1, max;
PreOrderTraverse(T, 1);//根节点是第一层
max = level[1];
while(level[i]) {
if(level[i] > max)
max = level[i];
i++;
}
return max;
}
void DestroyBitree(BiTree& T)
{
if(T) {
DestroyBitree(T->lchild);
DestroyBitree(T->rchild);
delete T;
}
}
int main()
{
BiTree tree;
CreateBiTree(tree);
cout <<width(tree) <<endl;
DestroyBitree(tree);
}
/*test case:
# 0
ABC##DE#G##F### 2
124##5##36### 3
124##5##36##7## 4
124##5##367##8### 3
124##5##36#7#8### 3
*/
方法二:利用队列
[题目分析] 求二叉树高度的算法见上题。求最大宽度可采用层次遍历的方法,记下各层结点数,每层遍历完毕,若结点数大于原先最大宽度,则修改最大宽度。
/***循环队列基本操作***/
#include<cstdlib>
#include<iostream>
using namespace std;
#define MAXQSIZE 1000
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char TElemType;
//二叉树的二叉链表存储表示
typedef struct BiNode {
TElemType data; //结点数据域
struct BiNode *lchild, *rchild; //左右孩子指针
} BiTNode, *BiTree;
typedef BiTree QElemType;
typedef char SElemType;
typedef int Status;
typedef struct {
QElemType *base;//初始化时动态分配存储空间
int front;//头指针
int rear;//尾指针
} SqQueue;
//算法3.11 循环队列的初始化
Status InitQueue(SqQueue &Q) //构造一个空队列Q
{
Q.base = new QElemType[MAXQSIZE]; //为队列分配一个最大容量为MAXSIZE的数组空间
if(!Q.base)
exit(OVERFLOW); //存储分配失败
Q.front = Q.rear = 0; //头指针和尾指针置为零,队列为空
return OK;
}
bool QueueEmpty(SqQueue Q)
{
return Q.front == Q.rear;
}
//算法3.12 求循环队列的长度
int QueueLength(SqQueue Q) //返回Q的元素个数,即队列的长度
{
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
//算法3.13 循环队列的入队
Status EnQueue(SqQueue &Q, QElemType e) //插入元素e为Q的新的队尾元素
{
if((Q.rear + 1) % MAXQSIZE == Q.front) //尾指针在循环意义上加1后等于头指针,表明队满
return ERROR;
Q.base[Q.rear] = e; //新元素插入队尾
Q.rear = (Q.rear + 1) % MAXQSIZE; //队尾指针加1
return OK;
}
//算法3.14 循环队列的出队
Status DeQueue(SqQueue &Q, QElemType &e) //删除Q的队头元素,用e返回其值
{
if(Q.front == Q.rear)
return ERROR; //队空
e = Q.base[Q.front]; //保存队头元素
Q.front = (Q.front + 1) % MAXQSIZE; //队头指针加1
return OK;
}
//销毁队列Q,Q不再存在
void DestroyQueue(SqQueue &Q)
{
if(Q.base)
delete []Q.base;
Q.base = NULL;
Q.front = Q.rear = 0;
}
void CreateBiTree(BiTree &T)
{
//按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
TElemType ch;
if(!(cin >> ch)) exit(0);
if(ch == ‘#‘) T = NULL; //递归结束,建空树
else {
T = new BiTNode;
T->data = ch; //生成根结点
CreateBiTree(T->lchild); //递归创建左子树
CreateBiTree(T->rchild); //递归创建右子树
} //else
} //CreateBiTree
int width(BiTree T)
{
int max = 0;
SqQueue Q;
InitQueue(Q);
if(T == NULL)
return 0;
EnQueue(Q, T);
while(!QueueEmpty(Q)){
int len = QueueLength(Q);
max = (max >= len) ? max : len;
for(int i = 0; i < len; i++){
QElemType t;
DeQueue(Q, t);
if(t->lchild) EnQueue(Q, t->lchild);
if(t->rchild) EnQueue(Q, t->rchild);
}
}
DestroyQueue(Q);
return max;
}
void DestroyBitree(BiTree& T)
{
if(T) {
DestroyBitree(T->lchild);
DestroyBitree(T->rchild);
delete T;
}
}
int main()
{
BiTree tree;
CreateBiTree(tree);
cout <<width(tree) <<endl;
DestroyBitree(tree);
}
/*test case:
# 0
ABC##DE#G##F### 2
124##5##36### 3
124##5##36##7## 4
124##5##367##8### 3
124##5##36#7#8### 3
*/
标签:数据 sel 二叉链表 abc iostream cas [] delete c#
原文地址:http://www.cnblogs.com/4bytes/p/6740648.html