-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path4-HuffmanTree.cpp
118 lines (118 loc) · 2.58 KB
/
4-HuffmanTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
using namespace std;
#define MAXSIZE 50
#define END 9999
typedef struct HNode{
struct HNode* lchild;
struct HNode* rchild;
int data;
int count;
}HNode,*HTree;
typedef struct forestNode{
HTree data;
struct forestNode* next;
}forestNode,*forest;
int fun(HTree &T){
return 0;
}
void InitForest(forest &F,int L[]);
void InitHTree(HTree &T,forest &F);
void Print_HTree(HTree T,int tag);
void QSort(int L[],int low,int high);
int main(){
int L[MAXSIZE];
int c;
cin>>c;
int i=0;
for(i=0;i<MAXSIZE&&c!=END;i++){
L[i]=c;
cin>>c;
}
if(i==0)return 0;
L[i]=c;
HTree T=NULL;
forest F=NULL;
//forest p=NULL;
QSort(L,0,i-1);
//带头结点
InitForest(F,L);
InitHTree(T,F);
/*p=F->next;
while(p)
{
cout<<p->data->data<<" ";
p=p->next;
}*/
fun(T);
Print_HTree(T,1);
cout<<endl;
return 0;
}
void QSort(int L[],int low,int high){
if(low<high){
int mid=(low+high)/2;
int tmp=L[low];
L[low]=L[mid];
L[mid]=tmp;
tmp=L[low];
int i=low,j=high;
while(i<j){
for(;i<j&&L[j]>=tmp;j--);
L[i]=L[j];
for(;i<j&&L[i]<tmp;i++);
L[j]=L[i];
}
L[i]=tmp;
QSort(L,low,i-1);
QSort(L,i+1,high);
}
}
void ForestInsert(forest &f,forest &F){
forest pre=F,p=F->next;
for(;p&&p->data->data<=f->data->data;pre=pre->next,p=p->next);
f->next=pre->next;
pre->next=f;
}
void InitHTree(HTree &T,forest &F){
forest p=F;
HTree tmp;
while(F->next&&F->next->next){
p=F->next;
tmp=(HTree)malloc(sizeof(HNode));
tmp->data=p->data->data+p->next->data->data;
tmp->lchild=p->data;
tmp->rchild=p->next->data;
p->next->data=tmp;
F->next=p->next->next;
ForestInsert(p->next,F);
free(p);
}
T=F->next->data;
free(F->next);
free(F);
}
void InitForest(forest &F,int L[]){
forest p,s;
F=(forest)malloc(sizeof(forestNode));
F->next=NULL;
p=F;
for(int i=0;L[i]!=END;i++){
s=(forest)malloc(sizeof(forestNode));
s->data=(HTree)malloc(sizeof(HNode));
s->data->lchild=NULL;
s->data->rchild=NULL;
s->data->data=L[i];
s->next=p->next;
p->next=s;
p=p->next;
}
}
void Print_HTree(HTree T,int tag){
if(!T)return ;
if(tag==1)cout<<T->data<<" ";
Print_HTree(T->lchild,tag);
if(tag==2)cout<<T->data<<" ";
Print_HTree(T->rchild,tag);
if(tag==3)cout<<T->data<<" ";
free(T);
}