for(i=1;i<=2*n-1;i++){ ht[i].parent == 0; ht[i].leftchild = 0; ht[i].rightchild = 0; }是什么意思
时间: 2024-05-19 14:11:43 浏览: 67
这段代码是对一个哈夫曼树的结构体数组 ht 的元素进行初始化。哈夫曼树是一种特殊的二叉树,它的每个非叶子节点都有两个子节点,左子节点权值小于等于右子节点权值,且所有叶子节点都在同一层级。初始化代码中,对于每个哈夫曼树节点,都将其父节点的下标设为0(表示没有父节点),左右子节点的下标都设为0(表示没有左右子节点)。其中,2*n-1 表示哈夫曼树的总节点数。
相关问题
#include<cstring> #include<iostream> using namespace std; typedef struct { int weight; int parent,lchild,rchild; }HTNode,*HuffmanTree; typedef char **HuffmanCode; void Select(HuffmanTree HT,int len,int &s1,int &s2) { int i,min1=32767,min2=32767; for(i=1;i<=len;i++) { if(HT[i].weight<min1&&HT[i].parent==0) { s2=s1; min2=min1; min1=HT[i].weight; s1=i; } else if(HT[i].weight<min2&&HT[i].parent==0) { min2=HT[i].weight; s2=i; } } } void CreatHuffmanTree(HuffmanTree &HT,int n) { int m,s1,s2,i; if(n<=1) return; m=2*n-1; HT=new HTNode[m+1]; for(i=1;i<=m;++i) { HT[i].parent=0; HT[i].lchild=0; HT[i].rchild=0; } for(i=1;i<=n;++i) cin >> HT[i].weight; for(i=n+1;i<=m;++i) { ; HT[s1].parent= ; HT[s2].parent= ; HT[i].lchild= ; HT[i].rchild= ; HT[i].weight= ; } } void print(HuffmanTree HT,int n) { for(int i=1;i<=2*n-1;i++) cout << HT[i].weight << " " << HT[i].parent << " " << HT[i].lchild << " " << HT[i].rchild << endl; } int main() { HuffmanTree HT; int n; cin >> n; CreatHuffmanTree(HT,n); print(HT,n); return 0; }填完这段代码
这段代码是一个创建哈夫曼树的函数,需要填写一些缺少的代码段。
在函数中,需要调用 `Select` 函数来选择权值最小的两个节点,并将它们合并成一个新的节点。在合并的过程中,需要给新节点的 `parent`、`lchild`、`rchild`、`weight` 成员变量赋值。
下面是填好代码的函数:
```
#include <cstring>
#include <iostream>
using namespace std;
typedef struct {
int weight;
int parent, lchild, rchild;
} HTNode, *HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree HT, int len, int &s1, int &s2) {
int i, min1 = 32767, min2 = 32767;
for (i = 1; i <= len; i++) {
if (HT[i].weight < min1 && HT[i].parent == 0) {
s2 = s1;
min2 = min1;
min1 = HT[i].weight;
s1 = i;
} else if (HT[i].weight < min2 && HT[i].parent == 0) {
min2 = HT[i].weight;
s2 = i;
}
}
}
void CreatHuffmanTree(HuffmanTree &HT, int n) {
int m, s1, s2, i;
if (n <= 1)
return;
m = 2 * n - 1;
HT = new HTNode[m + 1];
for (i = 1; i <= m; ++i) {
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
}
for (i = 1; i <= n; ++i)
cin >> HT[i].weight;
for (i = n + 1; i <= m; ++i) {
Select(HT, i - 1, s1, s2);
HT[s1].parent = i;
HT[s2].parent = i;
HT[i].lchild = s1;
HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
}
void print(HuffmanTree HT, int n) {
for (int i = 1; i <= 2 * n - 1; i++)
cout << HT[i].weight << " " << HT[i].parent << " " << HT[i].lchild << " " << HT[i].rchild << endl;
}
int main() {
HuffmanTree HT;
int n;
cin >> n;
CreatHuffmanTree(HT, n);
print(HT, n);
return 0;
}
```
以下代码有什么问题#include<stdio.h> #include<stdlib.h> typedef struct HTNode{ int weight; int parent,lchild,rchild; }HTNode,*HuffmanTree; void Select(HuffmanTree HT,int n,int *min1,int *min2){ for(int i=1;i<=n;i++){ if(HT[i].parent==0){ *min1=i; break; } } for(int i=1;i<=n;i++){ if((HT[i].weight<HT[*min1].weight)&&HT[i].parent==0){ *min1=i; } } for(int i=1;i<=n;i++){ if((HT[i].parent==0)&&(i!=*min1)){ *min2=i; break; } for(int j=1;j<=n;j++){ if((HT[j].parent==0)&&(j!=*min1)&&(HT[j].weight<HT[*min2].weight)) *min2=j; } } } void createhuffmantree(HuffmanTree *HT,int n){ if(n<=1) return; int m=2*n-1;//一定要记得写*,最后一个结点的下标 (*HT)=(HuffmanTree)malloc((m+1)*sizeof(HTNode));//0号单元未用 for(int i=1;i<=m;i++) { (*HT)[i].parent=0; (*HT)[i].lchild=0; (*HT)[i].rchild=0; } for(int i=1;i<=n;i++) scanf("%d",&(*HT)[i].weight); int min1,min2; for(int i=n+1;i<=m;i++){ Select(*HT,i-1,&min1,&min2); (*HT)[min1].parent=i; (*HT)[min2].parent=i; (*HT)[i].lchild=min1; (*HT)[i].rchild=min2; (*HT)[i].weight=(*HT)[min1].weight+(*HT)[min2].weight; } } int weight(HuffmanTree HT,int n){ int sum=0; for(int i=1;i<=n;i++){ int j=i; int num=0; while(HT[j].parent!=0){ j=HT[j].parent; num++; } sum+=num*HT[i].weight; } return sum; } int main(){ int n; scanf("%d",&n); HuffmanTree HT; createhuffmantree(&HT,n); int sum=weight(HT,n); printf("%d",sum); return 0; }
在函数Select中,第二个for循环的右括号位置不正确,应该放在第三个for循环的前面,以保证语义正确。具体来说,应该将第二个for循环的右括号放在第14行的后面,即:
```
for(int i=1;i<=n;i++){
if((HT[i].parent==0)&&(i!=*min1)){
*min2=i;
break;
}
}
for(int j=1;j<=n;j++){
if((HT[j].parent==0)&&(j!=*min1)&&(HT[j].weight<HT[*min2].weight))
*min2=j;
}
```
阅读全文