没有合适的资源?快使用搜索试试~ 我知道了~
C++链表实现最大堆、二叉树、霍夫曼编码
需积分: 10 3 下载量 86 浏览量
2015-12-21
20:30:18
上传
评论
收藏 12KB TXT 举报
温馨提示
试读
17页
链表实现最大堆、二叉树、霍夫曼编码、注释详细,运行结果很好
资源推荐
资源详情
资源评论
/**
一、实验目的
1、掌握堆和搜索树的基本概念,插入、删除方法。
二、实验内容
1、 创建最大堆类。最大堆的存储结构使用链表。
2、 提供操作:堆的插入、堆的删除。堆的初始化。Huffman树的构造。二叉搜索树的构造。
3、 接收键盘录入的一系列整数,输出其对应的最大堆、Huffman编码以及二叉搜索树。
4、 堆排序。
*/
#include <iostream>
#include <string.h>
#include<cmath>
#include<queue>
using namespace std;
//二叉树节点类
class BinaryTreeNode {
public:
BinaryTreeNode(){
LeftChild=RightChild=0;
}
BinaryTreeNode(const int & e){
data=e;
LeftChild=RightChild=0;
}
BinaryTreeNode(const int& e,BinaryTreeNode *l,BinaryTreeNode *r){
data=e;
LeftChild=l;
RightChild=r;
}
一、实验目的
1、掌握堆和搜索树的基本概念,插入、删除方法。
二、实验内容
1、 创建最大堆类。最大堆的存储结构使用链表。
2、 提供操作:堆的插入、堆的删除。堆的初始化。Huffman树的构造。二叉搜索树的构造。
3、 接收键盘录入的一系列整数,输出其对应的最大堆、Huffman编码以及二叉搜索树。
4、 堆排序。
*/
#include <iostream>
#include <string.h>
#include<cmath>
#include<queue>
using namespace std;
//二叉树节点类
class BinaryTreeNode {
public:
BinaryTreeNode(){
LeftChild=RightChild=0;
}
BinaryTreeNode(const int & e){
data=e;
LeftChild=RightChild=0;
}
BinaryTreeNode(const int& e,BinaryTreeNode *l,BinaryTreeNode *r){
data=e;
LeftChild=l;
RightChild=r;
}
int data;
BinaryTreeNode *LeftChild,*RightChild;
};
//逐层遍历 输出
void Travel(BinaryTreeNode* roots){
queue<BinaryTreeNode *> q;
while(roots){
cout<<roots->data<<" ";
if(roots->LeftChild)q.push(roots->LeftChild);
if(roots->RightChild)q.push(roots->RightChild);
try{
roots=q.front();
q.pop ();
}
catch(...)
{return;}
}
}
//前序遍历 输出
void PrOrder(BinaryTreeNode* roots){
if(roots){
cout<<roots->data<<" ";
PrOrder(roots->LeftChild);
PrOrder(roots->RightChild);
}
}
//中序遍历 输出
void InOrder(BinaryTreeNode* roots){
if(roots){
InOrder(roots->LeftChild);
BinaryTreeNode *LeftChild,*RightChild;
};
//逐层遍历 输出
void Travel(BinaryTreeNode* roots){
queue<BinaryTreeNode *> q;
while(roots){
cout<<roots->data<<" ";
if(roots->LeftChild)q.push(roots->LeftChild);
if(roots->RightChild)q.push(roots->RightChild);
try{
roots=q.front();
q.pop ();
}
catch(...)
{return;}
}
}
//前序遍历 输出
void PrOrder(BinaryTreeNode* roots){
if(roots){
cout<<roots->data<<" ";
PrOrder(roots->LeftChild);
PrOrder(roots->RightChild);
}
}
//中序遍历 输出
void InOrder(BinaryTreeNode* roots){
if(roots){
InOrder(roots->LeftChild);
剩余16页未读,继续阅读
资源评论
小黄鸭and小黑鸭
- 粉丝: 200
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功