#include "BinaryTree.h"
BinaryTree::BinaryTree() {
root=NULL;
cout<<"==================================="<<endl;
cout<<"请输入二叉树的扩展先序序列:";
cout<<"(e.g:ABDH E CF G ) "<<endl;
CreateBiTree(&root);
}
BinaryTree::~BinaryTree() {
//
}
int BinaryTree::ex2(int n) {
int result=1;
for(int i=0; i<n; i++)
result*=2;
return result;
}
void BinaryTree::CreateBiTree(BiTree *root) {
char ch;
ch=getchar();
if(ch==' ')
*root=NULL;
else {
*root=new BiTNode;
(*root)->data=ch;
CreateBiTree(&((*root)->lchild));
CreateBiTree(&((*root)->rchild));
}
}
void BinaryTree::porder(BiTree root)const {
if(root) {
cout<<root->data;
porder(root->lchild);
porder(root->rchild);
}
}
void BinaryTree::preorder()const {
cout<<"该二叉树的先序遍历序列:"<<endl;
porder(root);
cout<<endl;
}
void BinaryTree::PrintTree(BiTree root,int nLayer)const {
if(root==NULL) return;
PrintTree(root->rchild,nLayer+1);
for(int i=0; i<nLayer; i++)
cout<<" ";
cout<<root->data<<endl;
PrintTree(root->lchild,nLayer+1);
}
void BinaryTree::print()const {
cout<<"按竖向树状打印二叉树:"<<endl;
PrintTree(root,5);
cout<<endl;
}
void BinaryTree::LayerOrder(queue<char> &Q,queue<Position> &QI,int dw,int sw) {
//层次遍历二叉树,储存结点位置信息
if(root==NULL) return ;
queue<BiTree> temp;
queue<Position> temppos;
BiTree p; Position pos;
temp.push(root);
Q.push(root->data);
pos.layer=0;
pos.blank=sw/2;
temppos.push(pos);
QI.push(pos);
while(!temp.empty()) {
p=temp.front();
temp.pop();
// cout<<Q.front();
Position parentpos=temppos.front();
temppos.pop();
int offset=sw/ex2(parentpos.layer+2);
if(p->lchild) {
temp.push(p->lchild);
Q.push(p->lchild->data);
pos.layer=parentpos.layer+1;
pos.blank=parentpos.blank-offset;
temppos.push(pos);
QI.push(pos);
}
if(p->rchild) {
temp.push(p->rchild);
Q.push(p->rchild->data);
pos.layer=parentpos.layer+1;
pos.blank=parentpos.blank+offset;
temppos.push(pos);
QI.push(pos);
}
}
}
void BinaryTree::displaytree(int datawidth,int screenwidth) {
cout<<"树状显示二叉树:"<<endl;
queue <char> Q;
queue <Position> QI;
LayerOrder(Q,QI,datawidth,screenwidth);
int layer=0,bk=0;
while(!QI.empty()) {
if(layer<QI.front().layer) {
layer++;
cout<<endl<<endl;
bk=0;
}
while(bk<QI.front().blank) {
cout<<" ";
bk++;
}
cout<<Q.front();
bk+=datawidth;
QI.pop();
Q.pop();
}
cout<<endl<<endl;
}
bool BinaryTree::Iscompletebinarytree() {
//判断一颗二叉树是否为完全二叉树
if(root==NULL)
return true;
queue <BiTree> Q;
Q.push(root);
int flag=0;
while(!Q.empty()) {
if(!Q.front())
flag=1;
else if(flag)
return false;
else {
Q.push(Q.front()->lchild);
Q.push(Q.front()->rchild);
}
Q.pop();
}
return true;
}
没有合适的资源?快使用搜索试试~ 我知道了~
树状显示二叉树和完全二叉树判断
共16个文件
pdb:2个
cpp:2个
obj:2个
4星 · 超过85%的资源 需积分: 16 197 下载量 171 浏览量
2009-03-25
20:33:29
上传
评论 4
收藏 924KB RAR 举报
温馨提示
1、树状显示二叉树: 编写函数displaytree(二叉树的根指针,数据值宽度,屏幕的宽度)输出树的直观示意图。输出的二叉树是垂直打印的,同层的节点在同一行上。 问题描述: 假设数据宽度datawidth=2,而屏幕宽度screenwidth为64=26,假设节点的输出位置用 (层号,须打印的空格数)来界定。 第0层:根在(0,32)处输出; 第1层:因为根节点缩进了32个空格,所以下一层的偏移量(offset)为32/2=16=screenwidth/22。即第一层的两个节点的位置为(1,32-offset),(1,32+offset)即(1,16),(1,48)。 第二层:第二层的偏移量offset为screenwidth/23。第二层的四个节点的位置分别是(2,16-offset),(2,16+offset),(2,48-offset),(2,48+offset)即(2,8),(2,24),(2,40),(2,56)。 …… 第i层:第i层的偏移量offset为screenwidth/2i+1。第i层的每个节点的位置是访问第i-1层其双亲节点时确定的。假设其双亲的位置为(i-1,parentpos)。若其第i层的节点是其左孩子,那末左孩子的位置是(i,parentpos-offset),右孩子的位置是(i,parentpos+offset)。 提示:利用二叉树的层次遍历算法实现。利用两个队列Q,QI。队列Q中存放节点信息,队列QI中存相应于队列Q中的节点的位置信息,包括层号和需要打印节点值时需要打印的空格数。当节点被加入到Q时,相应的打印信息被存到QI中。二叉树本身采用二叉链表存储。 2、完全二叉树判断 用一个二叉链表存储的二叉树,判断其是否是完全二叉树。
资源推荐
资源详情
资源评论
收起资源包目录
BinaryTree.rar (16个子文件)
BinaryTree
BinaryTree.cpp 3KB
Main.cpp 249B
BinaryTree.opt 48KB
BinaryTree.ncb 49KB
BinaryTree.dsw 545B
BinaryTree.dsp 4KB
Debug
BinaryTree.obj 135KB
vc60.pdb 148KB
vc60.idb 105KB
BinaryTree.pdb 1.27MB
Main.obj 39KB
BinaryTree.exe 240KB
BinaryTree.pch 2.97MB
BinaryTree.ilk 383KB
BinaryTree.plg 923B
BinaryTree.h 791B
共 16 条
- 1
lsf886
- 粉丝: 2
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页