Skip to content

Commit

Permalink
level 1 bug fix,test commit reset
Browse files Browse the repository at this point in the history
  • Loading branch information
NONOThingC committed Mar 20, 2019
1 parent 0435043 commit 1039ec2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdio.h>
void get_threeDigit_Number(const int targetnumber,int *digit_number)
void getThreeDigitNumber(const int targetnumber,int *digit_number)
{
int i=targetnumber;
int j=0;
Expand Down
File renamed without changes.
50 changes: 29 additions & 21 deletions level1/p07_encrypt_decrypt/Huffman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ void HuffmanCoding(HNode *HT, HCode *HC, unsigned long long *w, const int n)
{
//初始化
int m = 2 * n - 1;
HNode* p = HT;
HNode *p = HT;
int i = 0;
for (; i < n; i++, p++, w++)
//听取俸爷建议,重构此处代码
for (; i < n+m; i++, p++)
{
p->weight = *w;
p->lchild = -1;
p->rchild = -1;
p->parent = -1;
}

for (; i < m; i++, p++)
{
p->weight = 0;
if (i >= n)
{
p->weight = 0;
}
else
{
p->weight = *w;
w++;
}
p->lchild = -1;
p->rchild = -1;
p->parent = -1;
Expand All @@ -25,34 +26,41 @@ void HuffmanCoding(HNode *HT, HCode *HC, unsigned long long *w, const int n)
for (i = n; i < m; i++)
{
long long min1 = MAXVALUE, min2 = MAXVALUE;
int min1id = 0, min2id = 0;//min1是最小的,min2次小
int min1id = 0, min2id = 0; //min1是最小的,min2次小
for (int j = 0; j < i; j++)
{
if (HT[j].parent == -1 && HT[j].weight < min1)
{
min2 = min1; min2id = min1id; min1 = HT[j].weight; min1id = j;
min2 = min1;
min2id = min1id;
min1 = HT[j].weight;
min1id = j;
}
else if (HT[j].parent == -1 && HT[j].weight < min2)
{
min2 = HT[j].weight; min2id = j;
min2 = HT[j].weight;
min2id = j;
}
}
HT[min1id].parent = i; HT[min2id].parent = i;
HT[i].lchild = min1id; HT[i].rchild = min2id;
HT[min1id].parent = i;
HT[min2id].parent = i;
HT[i].lchild = min1id;
HT[i].rchild = min2id;
HT[i].weight = min1 + min2;
}
//做字符编码

for (int j = 0; j < n; j++)
{
int start = n - 1;
for (int tempid = j,tempparent = HT[j].parent; tempparent != -1; tempid = tempparent, tempparent = HT[tempparent].parent)
for (int tempid = j, tempparent = HT[j].parent; tempparent != -1; tempid = tempparent, tempparent = HT[tempparent].parent)
{
if (HT[tempparent].lchild == tempid) {
if (HT[tempparent].lchild == tempid)
{
HC[j].bit[start--] = 0;
}
else HC[j].bit[start--] = 1;
else
HC[j].bit[start--] = 1;
}
HC[j].start = start + 1;//记录根节点位置
HC[j].start = start + 1; //记录根节点位置
}
}
File renamed without changes.

0 comments on commit 1039ec2

Please sign in to comment.