当前位置: 首页 > news >正文

网站群建设的目的/百度收录查询

网站群建设的目的,百度收录查询,学做ppt的网站有哪些内容,做网站该注意哪些基本要素注意哈夫曼编码的贪心选择性质,以及构造哈夫曼树的特点,构件相应的哈夫曼树。(注意哈夫曼树只是根据最优编码生成的,因此其可以不用为完全二叉树。) 针对下面几个节点,我们设计相应的哈夫曼树结构如下&…

注意哈夫曼编码的贪心选择性质,以及构造哈夫曼树的特点,构件相应的哈夫曼树。(注意哈夫曼树只是根据最优编码生成的,因此其可以不用为完全二叉树。)

针对下面几个节点,我们设计相应的哈夫曼树结构如下,根据相应结构设计对应的代码,由于不同的节点结构我们设计了两份代码,大体思路一直,只是在某处可能稍有不同。。。
这里写图片描述

代码一

#include <iostream>
#include <string>
#define MAX 1000000
using namespace std;typedef struct{double weight;char ch;int lchild, rchild, parent;
}HTNode;void select(HTNode htnodes[], int *a, int* b, int k){double weight = MAX;for (int i = 0; i < k; i++){if (htnodes[i].parent == -1 && htnodes[i].weight < weight){weight = htnodes[i].weight;*a = i;}}weight = MAX;for (int i = 0; i < k; i++){if (htnodes[i].parent == -1 && i != *a&&htnodes[i].weight < weight){weight = htnodes[i].weight;*b = i;}}//排序根节点的左右节点的顺序,因为huffuman树中根节点的做左节点和右节点的顺序稍有违背,因此我们进行少许修改int temp;if (htnodes[*a].lchild < htnodes[*b].lchild){temp = *a;*a = *b;*b = temp;}}
void HuffmanTree(HTNode htnodes[], int w[], char ch[], int n){for (int i = 0; i < 2 * n - 1; i++){htnodes[i].parent = -1; htnodes[i].lchild = -1;htnodes[i].rchild = -1;}for (int i = 0; i < n; i++){htnodes[i].weight = w[i];htnodes[i].ch = ch[i];}for (int k = n; k < 2 * n - 1; k++){int i1 = 0;int i2 = 0;select(htnodes, &i1, &i2, k);htnodes[i1].parent = k;htnodes[i2].parent = k;htnodes[k].weight = htnodes[i1].weight + htnodes[i2].weight;htnodes[k].lchild = i1;htnodes[k].rchild = i2;}
}
void HuffmanCode(HTNode htnodes[], int n){int i, j, k;string s = "";for (int i = 0; i < n; i++){s = "";j = i;while (htnodes[j].parent != -1){k = htnodes[j].parent;if (j == htnodes[k].lchild){s = s + "0";}else{s = s + "1";}j = htnodes[j].parent;}cout << "字符" << htnodes[i].ch << "的编码:" << endl;for (int i = s.size() - 1; i >= 0; i--){cout << s.at(i) << " ";}cout << endl;}
}
int main(){const int n = 6;HTNode node[2 * n-1];char ch[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int w[] = { 50, 60, 150, 200, 240, 300 };HuffmanTree(node, w, ch, n); HuffmanCode(node, n);return 0;
}

运行结果:

这里写图片描述

代码二:

在代码二中我们针对节点结构进行了一个新的设计,加入了用于表示代码是否被选中的flag部分,给程序的设计带来了极大的方便,下面我们就过来分析代码:

#include <iostream>
#include <string>
#define MAX 1000000
using namespace std;struct HTNode{int weight;char ch;int lchild, rchild, parent;bool flag;
};void select(HTNode htnodes[], int* lchild, int* rchild, int n){double weight = MAX;for (int i = 0; i < n; i++){if (htnodes[i].flag != true && htnodes[i].weight < weight){weight = htnodes[i].weight;*lchild = i;}}htnodes[*lchild].flag = true;weight = MAX;for (int i = 0; i < n; i++){if (htnodes[i].flag != true && htnodes[i].weight < weight){weight = htnodes[i].weight;*rchild = i;}}htnodes[*rchild].flag = true;int temp = 0;if (htnodes[*lchild].lchild<htnodes[*rchild].lchild){temp = *lchild;*lchild = *rchild;*rchild = temp;}
}
void HuffuTree(HTNode htnodes[],int weight[],char ch[],int n){for (int i = 0; i < 2 * n - 1; i++){htnodes[i].lchild = -1;htnodes[i].rchild = -1;htnodes[i].parent = -1;htnodes[i].flag = false;}for (int i = 0; i < n; i++){htnodes[i].weight = weight[i];htnodes[i].ch = ch[i];}int lchild=0, rchild=0;for (int k = n; k < 2 * n - 1; k++){select(htnodes, &lchild, &rchild, k);htnodes[lchild].parent = k;htnodes[rchild].parent = k;htnodes[k].lchild = lchild;htnodes[k].rchild = rchild;htnodes[k].weight = htnodes[lchild].weight + htnodes[rchild].weight;}
}
void HuffuCode(HTNode htnodes[],int n){int j, parent;string s = " ";for (int i = 0; i < n; i++){s = " ";j = i;while (j!=2*n-2){//等价于htnodes[j].parent!=-1parent = htnodes[j].parent;if (j == htnodes[parent].lchild){s = s + "0";}if (j == htnodes[parent].rchild){s = s + "1";}j = htnodes[j].parent;}cout << "字符" << htnodes[i].ch << "的编码:" << endl;for (int k = s.length() - 1; k >= 0; k--){cout << s.at(k) << " ";}cout << endl;}
}
int main(){const int n = 6;HTNode htnodes[2 * n - 1];char ch[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int weight[] = { 50, 60, 150, 200, 240, 300 };HuffuTree(htnodes,weight,ch, n);HuffuCode(htnodes, n);return 0;
}

运行结果:

这里写图片描述

代码参考:http://www.xuebuyuan.com/2072443.html

http://www.lbrq.cn/news/1419625.html

相关文章:

  • 企业管理培训课程心得/seo页面内容优化
  • 播视频网站开发/百度客服人工电话
  • 高淳网站建设/数据分析师证书
  • 哪里有营销型网站制作/河北企业网站建设
  • 网商之窗官网/关于seo的行业岗位有哪些
  • 网站如何做单项链接/国际新闻头条今日要闻
  • 泉州网站建设开发/企业官网
  • wordpress搜索收录/外贸建站优化
  • 没有数据怎么做网站/360网站关键词排名优化
  • 做那种网站赚钱/莆田seo推广公司
  • 农村电商网站建设方案/网络营销方法有哪些举例
  • wordpress付费查看全文内容/seo百度推广
  • 嘉兴建设网站/适合网络营销的产品
  • 注册完域名后如何做网站/在线h5免费制作网站
  • 几个做ppt的网站知乎/seo教程网
  • 做网站为什么没收入/群排名优化软件官网
  • shopbase建站费用/手机如何制作自己的网站
  • 做网站设计需要具备哪些/北京网络营销咨询公司
  • 网站你懂我意思正能量晚上在线下载免费软件魅族/seo网站优化教程
  • 网站注册商是什么/上海百度搜索优化
  • 西安网站制作哪家便宜又好/上海还能推seo吗
  • 公司企业邮箱是什么/网站优化推广公司排名
  • 科技资讯网站开发/企业seo排名外包
  • 海淀区网站制作公司/手机如何制作网页
  • 深圳那家做网站好/宁波pc营销型网站制作
  • 网站开发用的软件/seo优化操作
  • 广州网站建设电话/谷歌浏览器网址
  • 徐州网站制作怎样/什么网站可以发布广告
  • 做网站源代码/海外推广营销 平台
  • 泛搜索wordpress/seo高端培训
  • Spark03-RDD02-常用的Action算子
  • E2B是一个开源基础设施,允许您在云中安全隔离的沙盒中运行AI生成的代码和e2b.dev网站
  • JavaScript字符串详解
  • SQL Server 2019安装教程(超详细图文)
  • LeetCode 分类刷题:2962. 统计最大元素出现至少 K 次的子数组
  • GEEPython-demo1:利用Sentinel-2监测北京奥林匹克森林公园2024年NDVI变化(附Python版)