-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoffman1.cpp
More file actions
156 lines (132 loc) · 3.81 KB
/
hoffman1.cpp
File metadata and controls
156 lines (132 loc) · 3.81 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<iostream>
//#include<conio.h>
#include<cstring>
using namespace std;
class Huffman{
private:
class Node{
public:
char c;
double info;
Node* left;
Node* right;
};
int size;
//char* symbols;
//double* prob;
Node* root;
Node** arr;
char* code;
public:
//constructor
Huffman(int num);
void insertsort(int begindex);//to sort probability array
Node* getnode(double n,char sym){
Node* ptr;
ptr=new Node;
ptr->info=n;
ptr->left=ptr->right=NULL;
ptr->c=sym;
return ptr;
return 0;
}
void formTree();
void gethuffcode(Node* ptr);
void inorder(Node* p);
Node* getroot(){return root;}
void printarr(int begindex);
};
Huffman::Huffman(int num){
size=num;
arr=new Node*[size];
double prob;
char sym;
cout<<"Enter the symbols followed by their probabilities."<<endl;
for(int i=0;i<size;i++){
cin>>sym;
cin>>prob;
arr[i]=getnode(prob,sym);
}
root=NULL;
code=new char[num];
// code[0]=' ';
//code[1]='\0';
}
void Huffman:: insertsort(int begindex){
for(int k=begindex+1;k<size;k++){
Node* element=arr[k];
int i;
for(i=k-1;i>=begindex && (arr[i]->info)>(element->info);i--){
arr[i+1]=arr[i];
}
arr[i+1]=element;
}
}
void Huffman::formTree(){
for(int i=0;i<size-1;i++){
insertsort(i);
cout<<"The sorted array is: "<<flush;
printarr(i);
Node* ptr=new Node;
ptr->info=(arr[i]->info)+(arr[i+1]->info);
ptr->c='\0';
ptr->left=arr[i];
ptr->right=arr[i+1];
root=ptr;
arr[i+1]=ptr;
// getch();
}
cout<<"Tree formation is complete."<<endl;
cout<<"The value at root is: "<<root->info<<endl;
// getch();
}
void Huffman::inorder(Node* p){
//if(p==NULL){
// cout<<"Root is empty."<<endl;
//}
if(p!=NULL){
inorder(p->left);
// getch();
cout<<p->info<<" "<<flush;
inorder(p->right);
}
}
void Huffman::printarr(int begindex){
//cout<<"The prob array is: "<<flush;
for(int i=begindex;i<size;i++){
cout<<arr[i]->info<<" "<<flush;
}
cout<<endl;
}
void Huffman::gethuffcode(Node* ptr){
if((ptr->left==NULL)&&(ptr->right==NULL)){
cout<<"Code for "<<(ptr->c)<<" is : "<<code<<endl;
}
if(ptr->left!=NULL){
code=strcat(code,"0");
gethuffcode(ptr->left);
}
if(ptr->right!=NULL){
code=strcat(code,"1");
gethuffcode(ptr->right);
}
}
int main(){
int num;
//char* s;
cout<<"Enter the number of symbols: "<<flush;
cin>>num;
//s=new char[num];
//s="\0";
Huffman obj(num);
obj.formTree();
if(obj.getroot()==NULL)cout<<"The root is NULL."<<endl;
else{
cout<<"Value at root is: "<<(obj.getroot())->info<<endl;
obj.inorder(obj.getroot());
cout<<"Value at root is: "<<(obj.getroot())->info<<endl;
cout<<"Moving on to gethuffcode."<<endl;
obj.gethuffcode(obj.getroot());
}
// getch();
}