二叉树与哈夫曼编码

  • 格式:doc
  • 大小:355.00 KB
  • 文档页数:33
{
if (!root || &left == &right || left.root || right.root)return;
x = root->element;
left.root = root->lChild;
right.root = root->rChild;
delete root;
root = NULL;
using namespace std;
template<class T>
struct BTNode
{
BTNode(){ lChild = rChild = NULL }
BTNode(const T& x)
{
element = x;
lChild = rChild = NULL;
}
BTNode(const T& x, BTNode<T>* l, BTNode<T>* r)
实验报告
( 2014/ 2015 学年 第 二 学期)
课程名称
数据结构
实验名称
实验二 二叉树的基本操作及哈夫曼编码译码系统的实现
实验时间
2015

10

31

指导单位
计算机软件学院
指导教师
骆健
学生姓名
陈兵
班级学号
B14041126
学院(系)
计软院
专 业
软嵌NIIT
实 验 报 告
实验名称
实验二 二叉树的基本操作及哈夫曼编码译码系统的实现
{
element = x;
lChild = l;
rChild = r;
}
T element;
BTNode<T> *lChild, *rChild;
};
template<class T>
class BinaryTree
{
public :
BinaryTree(){ root = NULL; }
~BinaryTree(){};
bool IsEmpty()const;
void Clear();
bool Root(T& x)const;
void MakeTree(const T& x, BinaryTree<T>& left, BinaryTree<T>& right);
void MakeTree(BTNode<T>* r);
left.root =NULL;
right.root = NULL;
}
template<class T>
void BinaryTree<T>::MakeTree(BTNode<T>* r)
{
root = r;
}
template<class T>
void BinaryTree<T>::BreakTree( T& x, BinaryTree<T>& left, BinaryTree<T>& right)
{
if (t){
Visit(t->element);
三、实验原理及内容
实验题一:在二叉链表上实现二叉树运算
(1)设计递归算法,实现下列二叉树运算:删除一棵二叉树、求一棵二叉树的高度、求一棵二叉树中叶子结点数、复制一棵二叉树、交换一棵二叉树的左右子树。
(2)设计算法,按自上到下,从左到右的顺序,按层次遍历一棵二叉树。
(3)设计main函数,测试上述每个运算。
}
template<class T>
void BinaryTree<T>::PreOrder(void(*Visit)(T& x))
{
PreOrder(Visit, root);
}
template<class T>
void BinaryTree<T>::PreOrder(void(*Visit)(T& x),BTNode<T>* t)
void DeleteTree(BTNode<T> *t);
int GetTreeHeight(BTNode<T> *t)const;
int GetLeavesNumber(BTNode<T> *t)const;
BTNode<T>* CopyTree(const BTNode<T> *t)const;
BTNode<T>* CopyTree()const;
void ExchangeChildTree();
protected:
BTNode<T>* root;
private:
void Clear(BTNode<T>* &t);
void PreOrder(void(*Visit)(T& x),BTNode<T>* t);
void ExchangeChildTree(BTNode<T> *t);
};
template<class T>
bool BinaryTree<T>::Root(T& x)const
{
if (root){
x = root->element; return true;
}
else
{
return false;
内容:1、建立头文件BTree.H,在该文件中定义二叉树的链式存储结构,并编写二叉树的各种基本操作实现函数。同时建立一个验证操作实现的主函数文件Test.cpp,编译并调试程序,直到正确运行。注意:需要用到队列的有关操作。
实 验 报 告(一)概要设计1.流程图及设计思想实 验 报 告
代码:
#include<iostream>
void BreakTree(T& x, BinaryTree<T>& left, BinaryTree<T>& right);
void DeleteTree();
void PreOrder(void(*Visit)(T& x));
int GetTreeHeight()const;
int GetLeavesNumber()const;
指导教师
骆健
实验类型
设计
实验学时
2
实验时间
一、实验目的和要求
(1)掌握二叉链表上实现二叉树基本去处的方法。
(2)学会设计基于遍历的求解二叉树应用问题的递归算法。
(3)理解哈夫曼树的构造算法,学习设计哈夫曼编码和译码系统。
二、实验环境(实验设备)
硬件: 微型计算机
软件: Microsoft Visual C++6.0
}
}
template<class T>
void BinaryTree<T>::MakeTree(const T& x, BinaryTree<T>& left, BinaryTree<T>& right)
{
if (root || &left == &right)return;
root = new BTNode<T>(x, left.root, right.root);