二叉树的基本操作及哈夫曼编码译码系统的实现.docx

上传人:b****4 文档编号:12385725 上传时间:2023-04-18 格式:DOCX 页数:15 大小:35.07KB
下载 相关 举报
二叉树的基本操作及哈夫曼编码译码系统的实现.docx_第1页
第1页 / 共15页
二叉树的基本操作及哈夫曼编码译码系统的实现.docx_第2页
第2页 / 共15页
二叉树的基本操作及哈夫曼编码译码系统的实现.docx_第3页
第3页 / 共15页
二叉树的基本操作及哈夫曼编码译码系统的实现.docx_第4页
第4页 / 共15页
二叉树的基本操作及哈夫曼编码译码系统的实现.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

二叉树的基本操作及哈夫曼编码译码系统的实现.docx

《二叉树的基本操作及哈夫曼编码译码系统的实现.docx》由会员分享,可在线阅读,更多相关《二叉树的基本操作及哈夫曼编码译码系统的实现.docx(15页珍藏版)》请在冰豆网上搜索。

二叉树的基本操作及哈夫曼编码译码系统的实现.docx

二叉树的基本操作及哈夫曼编码译码系统的实现

 

实验报告

(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*l,BTNode*r)

{

element=e;

lchild=l;

rchild=r;

}

private:

Telement;

BTNode*lchild,*rchild;

friendclassBTree;

friendvoidVisit(BTNode*);

};

template

voidVisit(BTNode*p)

{

cout<element<<"";

}

template

classBTree

{

public:

BTree(){root=NULL;}

~BTree(){}

boolIsEmpty()const;

boolRoot(T&x)const;

voidMakeTree(constT&e,BTree&left,BTree&right);

voidBreakTree(T&e,BTree&left,BTree&right);

voidPreOrder(void(*Visit)(BTNode*u))

{

PreOrder(Visit,root);

}

voidInOrder(void(*Visit)(BTNode*u))

{

InOrder(Visit,root);

}

voidPostOrder(void(*Visit)(BTNode*u))

{

PostOrder(Visit,root);

}

voidExchange();

voidLayerOrder();

intHigh();

intLeaves();

voidDeltree();

BTreeCopyBTree();

private:

BTNode*root;

voidPreOrder(void(*Visit)(BTNode*u),BTNode*t);

voidInOrder(void(*Visit)(BTNode*u),BTNode*t);

voidPostOrder(void(*Visit)(BTNode*u),BTNode*t);

voidExch(BTNode*);

voidLeaf(BTNode*,int&);

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&left,BTree&right)

{

BTNode*p;

p=newBTNode(e,left.root,right.root);

left.root=right.root=0;

root=p;

}

template//删除二叉树的所有节点

voidBTree:

:

BreakTree(T&e,BTree&left,BTree&right)

{

BTNode*p;

p=root;

if(p)

{

e=p->element;

left.root=p->lchild;

right.root=p->rchild;

}

}

template//线序遍历二叉树

voidBTree:

:

PreOrder(void(*Visit)(BTNode*u),BTNode*t)

{

if(t)

{

Visit(t);

if(t->lchild)

PreOrder(Visit,t->lchild);

if(t->rchild)

PreOrder(Visit,t->rchild);

}

}

template//中序遍历二叉树

voidBTree:

:

InOrder(void(*Visit)(BTNode*u),BTNode*t)

{

if(t)

{

if(t->lchild)

InOrder(Visit,t->lchild);

Visit(t);

if(t->rchild)

InOrder(Visit,t->rchild);

}

}

template//后序遍历二叉树

voidBTree:

:

PostOrder(void(*Visit)(BTNode*u),BTNode*t)

{

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*p=root;

BTNodex;

SeqQueue>sq(10);

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*u)

{

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*t,int&count)

{

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;

BTreeleft,right;

BreakTree(x,left,right);

left.Deltree();

right.Deltree();

delete(root);

root=NULL;

}

template//二叉树的复制

BTreeBTree:

:

CopyBTree()

{

if(root==NULL)

{

BTreezero;

returnzero;

}

Tx;

BTreechleft,chright;

BTreechange,changeleft,changeright;

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*p)

{

if(p!

=NULL)

{

BTNode*temp;

temp=p->lchild;

p->lchild=p->rchild;

p->rchild=temp;

Exch(p->lchild);

Exch(p->rchild);

}

}

voidmain()

{

BTreea,b,c,d,e,f,g,h,j,k,left,right;

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模板 > 动态背景

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1