#include<bits/stdc++.h>
using namespace std;
int b[26]={0};
int bb[26]={0};
int kk;
struct node{
int w;
int p,l,r;
};
set <node> se;
string Open(char* s){
string str = "";
ifstream op;
char ch;
op.open(s);
while(!op.eof()){
op>>ch;
str+=ch;
//cout<<ch;
}
//cout<<endl;
op.close();
return str;
}
void Count(string str){
kk = 0;
int ls = str.size();
for(int i=0;i<ls-1;i++){//处理掉多出来的一个字符
str[i] = tolower(str[i]);
b[str[i]-'a']++;
}
for(int i=0;i<26;i++){
if(b[i]){
char c = i+'a';
cout<<c<<':'<<b[i]<<endl;//b[i]即为权值
bb[kk++] = b[i];
}
}
// for(int i=0;i<k;i++){
// cout<<bb[i]<<endl;
// }
}
///选择两个parent为0,且weight最小的结点s1和s2
void Select(node* &ht,int n,int& a,int& b)
{
int i,min;
for(i=1; i<=n; i++){
if(ht[i].p==0){
min=i;
break;
}
}
for(i=1; i<=n; i++){
if(ht[i].p==0){
if(ht[i].w<ht[min].w)
min=i;
}
}
a=min;
for(i=1; i<=n; i++){
if(ht[i].p==0 && i!=a){
min=i;
break;
}
}
for(i=1; i<=n; i++){
if(ht[i].p==0 && i!=a){
if(ht[i].w<ht[min].w)
min=i;
}
}
b=min;
}
void CreateTree(node* &HT,int n){
int k=0;
if(n<=1) return;
int m = 2*n-1;
HT = new node[m+1];
for(int i=1;i<=m;i++){
HT[i].p = 0, HT[i].l = 0, HT[i].r = 0;
}
for(int i=1;i<=n;i++){
HT[i].w = bb[k++];
}
printf("\n哈夫曼树为: \n");
int s1,s2;
for(int i=n+1; i<=m; i++){
Select(HT,i-1,s1,s2);
HT[s1].p=i;
HT[s2].p=i;
HT[i].l=s1;
HT[i].r=s2;
HT[i].w=HT[s1].w+HT[s2].w;
printf("%d (%d, %d)\n",HT[i].w,HT[s1].w,HT[s2].w);
}
printf("\n");
}
void CreateCode(){
}
int main(){
char* s = "C:\\Users\\Answer\\Desktop\\实验6-哈夫曼编码算法的实现\\SourceFile.txt";
//cout<<Open(s)<<endl;//str 比原串多一个H字符
string str = Open(s);
cout<<str<<endl<<endl;
Count(str);
node* HT;
CreateTree(HT,kk);
return 0;
}