亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于哈夫曼樹的問題

關于哈夫曼樹的問題

鄧垚 2016-11-08 18:28:17
#include<iostream>using namespace std;struct element{?int weight;?int lchild, rchild, parent;};void Select(element hfmt[], int &i1, int &i2, int &num){?element hfmtw[7];?int min;?bool isi1 = false;?for (int i = 0; i<num; i++)?{??hfmtw[i] = hfmt[i];?}?int temp;?for (int a = 0; a<num - 1; a++){??if (hfmtw[a].parent != -1){???continue;??}??min = a;??for (int j = a; j<num; j++){???if (hfmtw[j + 1].weight<hfmtw[min].weight)???{????if (hfmtw[j + 1].parent != -1){?????continue;????}????min = j + 1;???????}??}??if (isi1 == false){???i1 = min;???isi1 = true;??}??else{???i2 = min;???return;??}??temp = hfmtw[a].weight;??hfmtw[a].weight = hfmtw[min].weight;??hfmtw[min].weight = temp;?}?num++;}void Hfmt(element hfmt[], int w[], int n){?for (int i = 0; i<2 * n - 1; i++){??hfmt[i].parent = -1;??hfmt[i].lchild = -1;??hfmt[i].rchild = -1;?}?for (int b = 0; b<n; b++){??hfmt[b].weight = w[b];?}?int i1 = 0, i2 = 0;?int num = n;?for (int k = n; k<2 * n - 1; k++)?{??Select(hfmt, i1, i2, num);??hfmt[i1].parent = k;??hfmt[i2].parent = k;??hfmt[k].weight = hfmt[i1].weight + hfmt[i2].weight;??hfmt[k].lchild = i1;??hfmt[k].rchild = i2;?}}void main(){?element hfmt[7];?int n = 4;?int w[] = { 2, 3, 4, 5 };?Hfmt(hfmt, w, n);?for (int i=0; i<7; i++)?{??cout << hfmt[i].weight << " " << hfmt[i].parent << " " << hfmt[i].lchild << " " << hfmt[i].rchild << endl;?}}怎么跟預計的不一樣
查看完整描述

1 回答

已采納
?
慕運維1139315

TA貢獻84條經驗 獲得超21個贊

我調試了一下num++放在return上面,還有for循環把減1去掉,我覺得select用棧好做一點

查看完整回答
反對 回復 2016-11-08
  • 1 回答
  • 0 關注
  • 1410 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號