二叉树的基本操作及哈夫曼编码译码系统的实现.docx
- 文档编号:12385725
- 上传时间:2023-04-18
- 格式:DOCX
- 页数:15
- 大小:35.07KB
二叉树的基本操作及哈夫曼编码译码系统的实现.docx
《二叉树的基本操作及哈夫曼编码译码系统的实现.docx》由会员分享,可在线阅读,更多相关《二叉树的基本操作及哈夫曼编码译码系统的实现.docx(15页珍藏版)》请在冰豆网上搜索。
二叉树的基本操作及哈夫曼编码译码系统的实现
实验报告
(2014/2015学年第二学期)
课程名称
数据结构A
实验名称
二叉树的基本操作
实验时间
2015
年
4
月
23
日
指导单位
计算机学院计算机科学与技术系
指导教师
学生姓名
班级学号
学院(系)
专业
实验报告
实验名称
二叉树的基本操作
指导教师
实验类型
设计
实验学时
2
实验时间
一、实验目的和要求
在二叉链表上设计和实现下列二叉树运算的算法
①设计递归算法,实现:
删除二叉树,求二叉树的高度,求二叉树中叶子结点数,复制二叉树,交换二叉树的左右子树。
②设计算法,按自上到下,自左向右的次序,即按层次遍历一棵二叉树。
③设计main函数,测试上述每个运算。
提示:
二叉树的按层次遍历需要利用队列作为辅助的数据结构,队列的元素类型是指向二叉树中结点的指针类型。
二、实验环境(实验设备)
硬件:
微型计算机
软件:
Windows操作系统、MicrosoftVisualC++6.0
三、实验原理及内容
实验源代码:
#include
#include
template
classQueue
{
public:
Queue(){};
~Queue(){};
virtualvoidEnQueue(constT&x)=0;
virtualvoidDeQueue()=0;
virtualTFront()=0;
virtualboolIsEmpty()const=0;
virtualboolIsFull()const=0;
};
template
classSeqQueue:
publicQueue
{
public:
SeqQueue(intMaxQueSize);
~SeqQueue(){delete[]q;}
voidEnQueue(constT&x);
voidDeQueue();
TFront();
boolIsEmpty()const;
boolIsFull()const;
private:
intfront,rear;
intMaxSize;
T*q;
};
template
SeqQueue
:
SeqQueue(intMaxQueSize)
{
MaxSize=MaxQueSize;
q=newT[MaxSize];
front=rear=0;
}
template
boolSeqQueue
:
IsEmpty()const
{
returnfront==rear;
}
template
boolSeqQueue
:
IsFull()const
{
return(rear+1)%MaxSize==front;
}
template
voidSeqQueue
:
EnQueue(constT&x)
{
assert(!
IsFull());
q[(rear=(rear+1)%MaxSize)]=x;
}
template
voidSeqQueue
:
DeQueue()
{
assert(!
IsEmpty());
front=(front+1)%MaxSize;
}
template
TSeqQueue
:
Front()
{
assert(!
IsEmpty());
returnq[(front+1)%MaxSize];
}
template
classBTree;
template
classBTNode
{
public:
BTNode(){lchild=rchild=0;}
BTNode(constT&e)
{
element=e;
lchild=rchild=0;
}
BTNode(constT&e,BTNode
{
element=e;
lchild=l;
rchild=r;
}
private:
Telement;
BTNode
friendclassBTree
friendvoidVisit(BTNode
};
template
voidVisit(BTNode
{
cout<
}
template
classBTree
{
public:
BTree(){root=NULL;}
~BTree(){}
boolIsEmpty()const;
boolRoot(T&x)const;
voidMakeTree(constT&e,BTree
voidBreakTree(T&e,BTree
voidPreOrder(void(*Visit)(BTNode
{
PreOrder(Visit,root);
}
voidInOrder(void(*Visit)(BTNode
{
InOrder(Visit,root);
}
voidPostOrder(void(*Visit)(BTNode
{
PostOrder(Visit,root);
}
voidExchange();
voidLayerOrder();
intHigh();
intLeaves();
voidDeltree();
BTree
private:
BTNode
voidPreOrder(void(*Visit)(BTNode
voidInOrder(void(*Visit)(BTNode
voidPostOrder(void(*Visit)(BTNode
voidExch(BTNode
voidLeaf(BTNode
intHighs(BTNode
};
template
boolBTree
:
IsEmpty()const
{
returnroot==NULL;
}
template
boolBTree
:
Root(T&x)const
{
if(root)
{
x=root->element;
returntrue;
}
elsereturnfalse;
}
template
voidBTree
:
MakeTree(constT&e,BTree
{
BTNode
p=newBTNode
left.root=right.root=0;
root=p;
}
template
voidBTree
:
BreakTree(T&e,BTree
{
BTNode
p=root;
if(p)
{
e=p->element;
left.root=p->lchild;
right.root=p->rchild;
}
}
template
voidBTree
:
PreOrder(void(*Visit)(BTNode
{
if(t)
{
Visit(t);
if(t->lchild)
PreOrder(Visit,t->lchild);
if(t->rchild)
PreOrder(Visit,t->rchild);
}
}
template
voidBTree
:
InOrder(void(*Visit)(BTNode
{
if(t)
{
if(t->lchild)
InOrder(Visit,t->lchild);
Visit(t);
if(t->rchild)
InOrder(Visit,t->rchild);
}
}
template
voidBTree
:
PostOrder(void(*Visit)(BTNode
{
if(t)
{
if(t->lchild)
PostOrder(Visit,t->lchild);
if(t->rchild)
PostOrder(Visit,t->rchild);
Visit(t);
}
}
template
voidBTree
:
LayerOrder()
{
if(root==0)
{
cout<<"TreeisEmpty!
!
!
"< return; } BTNode BTNode SeqQueue sq.EnQueue(*p); while(! sq.IsEmpty()) { x=sq.Front(); cout< if(x.lchild) sq.EnQueue(*x.lchild); if(x.rchild) sq.EnQueue(*x.rchild); sq.DeQueue(); } } template intBTree : High() { inth=Highs(root); returnh; } template intBTree : Highs(BTNode { if(u==0)return0; intlch,rch; lch=Highs(u->lchild); rch=Highs(u->rchild); if(lch>rch)returnlch+1; elsereturnrch+1; } template intBTree : Leaves() { intcount=0; Leaf(root,count); returncount; } template voidBTree : Leaf(BTNode { if(t) { if((t->lchild==0)&&(t->rchild==0)) {Visit(t); count++; } Leaf(t->lchild,count); Leaf(t->rchild,count); } } template voidBTree : Deltree() { if(root==NULL)return; Tx; BTree BreakTree(x,left,right); left.Deltree(); right.Deltree(); delete(root); root=NULL; } template BTree : CopyBTree() { if(root==NULL) { BTree returnzero; } Tx; BTree BTree BreakTree(x,chleft,chright); changeleft=chleft.CopyBTree(); changeright=chright.CopyBTree(); change.MakeTree(x,changeleft,changeright); returnchange; } template voidBTree : Exchange() { Exch(root); } template voidBTree : Exch(BTNode { if(p! =NULL) { BTNode temp=p->lchild; p->lchild=p->rchild; p->rchild=temp; Exch(p->lchild); Exch(p->rchild); } } voidmain() { BTree j.MakeTree('J',left,right); g.MakeTree('G',left,right); f.MakeTree('F',j,g); h.MakeTree('H',left,right); e.MakeTree('E',h,f); d.MakeTree('D',e,right); k.MakeTree('K',left,right); c.MakeTree('C',k,right); b.MakeTree('B',left,c); a.MakeTree('A',d,b); cout<<"LayerOrder: "; a.LayerOrder(); cout< cout<<"树A高度: "< cout<<"树A叶子: "; cout< "< cout< "< cout<<"CheckTreeB: "< b=a.CopyBTree(); cout<<"ThePreOrderofTreeBis: "; b.PreOrder(Visit); cout< cout<<"TheInOrderofTreeBis: "; b.InOrder(Visit); cout< cout<<"DeleteTreeA: "; a.Deltree(); cout< cout<<"CheckTreeA: "< cout<<"树A高度: "< cout<<"树A叶子结点数: "< cout< "< b.Exchange(); cout<<"ChecktheresultofTreeB: "< cout<<"LayerOrder: "; b.LayerOrder(); cout< } 实验报告 四、实验小结(包括问题和解决方法、心得体会、意见与建议等) 五、指导教师评语 成绩 批阅人 日期
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二叉 基本 操作 哈夫曼 编码 译码 系统 实现
![提示](https://static.bdocx.com/images/bang_tan.gif)