当前位置:网站首页>C#最优二叉树----哈夫曼树
C#最优二叉树----哈夫曼树
2022-07-30 05:47:00 【Misaki_Me】
C#最优二叉树----哈夫曼树
哈夫曼树定义:对一组带有确定权值的叶结点,构造的具有最小带权路径长度的二叉树。
权值:相当于在这个结点所需要待的时间,即到达这个结点所需要花费的代价。
带权路径长度:某一个结点权值为k。路径长度为w。带权路径长度就是k*w。
树的带权路径长度:从根结点到各个叶结点的路径长度与相应叶结点的权值相乘再进行相加得到树的带权路径长度。
哈夫曼树的构造算法和创建:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tree
{
class HNode
{
private int weight;
private int leftChild;
private int rightChild;
private int parent;
public int Weight
{
get
{
return weight;
}
set
{
weight = value;
}
}
public int LeftChild
{
get
{
return leftChild;
}
set
{
leftChild = value;
}
}
public int RightChild
{
get
{
return rightChild;
}
set
{
rightChild = value;
}
}
public int Parent
{
get
{
return parent;
}
set
{
parent = value;
}
}
public HNode()
{
parent = -1;
weight = 0;
leftChild = -1;
rightChild = -1;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tree
{
class HuffmanTree
{
private HNode[] data;
private int leafNum;
public HNode this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
public int LeafNum
{
get
{
return leafNum;
}
set
{
leafNum = value;
}
}
//构造器
public HuffmanTree(int n)
{
data = new HNode[2 * n - 1];
for (int i = 0; i < 2*n-1; i++)
{
data[i] = new HNode();
}
leafNum = n;
}
public void Create()
{
int m1, m2, x1, x2;
//输入n个叶子结点的权值
for (int i = 0; i < this.leafNum; i++)
{
Console.WriteLine("请输入第{0}个叶子结点的权值:",i+1);
data[i].Weight = Convert.ToInt32(Console.ReadLine());
}
//处理n个叶子结点,建立哈夫曼树
for (int i = 0; i < this.leafNum-1; i++)
{
m1 = m2 = Int32.MaxValue;
x1 = x2 = 0;
//在全部结点中找权值最小的两个结点
for (int j = 0; j < this.leafNum + i; j++)
{
if (data[j].Weight < m1 && data[j].Parent == -1)
{
m2 = m1;
x2 = x1;
m1 = data[j].Weight;
x1 = j;
}
else if (data[j].Weight < m2 && data[j].Parent == -1)
{
m2 = data[j].Weight;
x2 = j;
}
}
data[x1].Parent = this.leafNum + i;
data[x2].Parent = this.leafNum + i;
data[this.leafNum + i].Weight = data[x1].Weight + data[x2].Weight;
data[this.leafNum + i].LeftChild = x1;
data[this.leafNum + i].RightChild = x2;
}
}
}
}
边栏推荐
- Cannnot download sources不能下载源码百分百超详细解决方案
- 二、2队列
- -----博客声明
- QT serial 2: LORA test platform based on QT and STM32H750 (1)
- 租用服务器训练yolov3模型
- 一文盘点五款 BLDC 风机参考方案,建议先马
- 关于报错vscode
- 无法完成包的安装npm ERR! Refusing to install package with name “moment“ under a package also called “moment“
- 爬楼梯C语言
- Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
猜你喜欢

进制详解(二进制、八进制、十进制、十六进制详解及相互转换,位运算)

华秋电子成为开放原子开源基金会openDACS捐赠人,共建 openDACS开源生态

Kunlun On-state Screen Production (serial 1)---Contact

Acwing刷题第一节

QT serial port dynamically displays a large number of data waveform curves in real time (5) ======== "Final perfect solution version"

The IEEE under the specified journal search related papers

二、1稀疏sparsearray数组

创建快捷方式时如何不带“快捷方式“后缀字样?

QT serial and CAN dynamic real-time display the log data

干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
随机推荐
测试第一题
QT serialization 1: readyRead() function, the solution to incomplete data subcontracting
测试第二题
Word使用中常用的快捷键
独立按键控制led
单片机第一步
虚拟机栈帧结构
openssl1.1.1ARM dual compilation
ES6 syntax notes (ES6~ES11)
华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
Explore the efficiency of make_shared
Vim查找字符
pdf和word等文档添加水印
如何判断 PCB 板是否变形?
mysql数据库怎么设置手动提交
openssl 1.1.1 compile statement
FPGA parsing B code----serial 2
查看 word版本号
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
Acwing Brush Questions Section 1