-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project_DS.cpp
664 lines (562 loc) · 15.7 KB
/
Project_DS.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// class for getting (character) with its (frequency)
class CharFreq
{
public:
char chr;
int freq;
};
// class for getting (array of objects of CharFreq) with the (length)
class CharFreqarrLen
{
public:
CharFreq *arr;
int len;
};
// ------------------------------------------------------------------------------
// (helper function) for checking character present in arrcharfreq
bool checkChar(char *arr, char ch)
{
bool chk = false;
// traversing through the given array and checking for character
for (int i = 0; arr[i]; i++)
{
if (ch == arr[i])
{
chk = true;
}
}
// returning the bool check
return chk;
}
// function to evaluate the frequency of the characters
// it will return an object which will contain number of unique characters and array of object containing charcters anf their frequencies
CharFreqarrLen evaluateFreq(string str)
{
char *arr = new char[100];
int j = 0;
// storing unique characters in array
for (int i = 0; str[i]; i++)
{
char ch = str[i];
if (checkChar(arr, ch))
{
continue;
}
else
{
arr[j] = str[i];
j++;
}
}
arr[j] = '\0';
// fincing the number of unique charcters
int len = 0;
for (int i = 0; arr[i] != '\0'; i++)
{
len++;
}
// making array of objects for storing characters with their frequencies
CharFreq *arrcharfreq = new CharFreq[len];
// storing objects containing characters with their frequencies in the array of objects
for (int i = 0; i < len; i++)
{
int cnt = 0;
for (int j = 0; str[j]; j++)
{
if (arr[i] == str[j])
{
cnt++;
}
}
arrcharfreq[i].chr = arr[i];
arrcharfreq[i].freq = cnt;
}
// Applying "Bubble Sort" technique for sorting the frequency and characters in acending order
// loop to access each arrcharfreq element
for (int i = 0; i < len; i++)
{
// loop to comparearrcharfreq elements
for (int j = i + 1; j < len; j++)
{
// compare two adjacent elements
if (arrcharfreq[i].freq > arrcharfreq[j].freq)
{
CharFreq temp = arrcharfreq[j];
arrcharfreq[j] = arrcharfreq[i];
arrcharfreq[i] = temp;
}
}
}
// initializing the object datamembers
CharFreqarrLen arrlen;
arrlen.arr = arrcharfreq;
arrlen.len = len;
// returning final sorted array of characters with their frequencies
return arrlen;
}
// ------------------------------------------------------------------------------
// class Queue for further implementation
// using template
template <class x>
class Queue
{
public:
x *arr; // array to store Queue elements
int capacity; // maximum capacity of the Queue
int front; // front points to the front element in the Queue (if any)
int rear; // rear points to the last element in the Queue
int count; // ind size of the Queue
public:
Queue(int size = 50); // constructor
// functions
x dequeue();
void enqueue(x);
x peek();
int size();
bool isEmpty();
bool isFull();
};
// constructor to initialize Queue
template <class x>
Queue<x>::Queue(int size)
{
arr = new x[size];
capacity = size;
front = 0;
rear = -1;
count = 0;
}
// function to dequeue the front element
template <class x>
x Queue<x>::dequeue()
{
// check for Queue underflow
if (isEmpty())
{
cout << "Underflow" << endl;
}
int temp = front;
front = (front + 1) % capacity;
count--;
return arr[temp];
}
// function to add an item to the Queue
template <class x>
void Queue<x>::enqueue(x item)
{
// check for Queue overflow
if (isFull())
{
cout << "Overflow" << endl;
}
rear = (rear + 1) % capacity;
arr[rear] = item;
count++;
}
// function to return the front element of the Queue
template <class x>
x Queue<x>::peek()
{
if (isEmpty())
{
cout << "UnderFlow" << endl;
}
return arr[front];
}
// function to return the size of the Queue
template <class x>
int Queue<x>::size()
{
return count;
}
// function to check if the Queue is empty or not
template <class x>
bool Queue<x>::isEmpty()
{
return (size() == 0);
}
// function to check if the Queue is full or not
template <class x>
bool Queue<x>::isFull()
{
return (size() == capacity);
}
// ------------------------------------------------------------------------------
// class for huffmanNode
class huffmanNode
{
public:
huffmanNode *left;
int freq;
char chr;
huffmanNode *right;
// constructor
huffmanNode();
};
// constructor for initializing data members
huffmanNode ::huffmanNode()
{
left = right = NULL;
chr = '$';
freq = -1;
}
// ----------------------------------------------------------------------------------------------
// (helper function) to return the height of the node
int height(huffmanNode *root)
{
// Base case: empty tree has height 0
if (root == nullptr)
{
return 0;
}
// recur for left and right subtree and consider maximum depth
return 1 + max(height(root->left), height(root->right));
}
// checking balance of the overall tree
bool checkBalance(huffmanNode *node)
{
bool chk = true;
if (node == NULL)
{
return chk;
}
int right = height(node->right);
int left = height(node->left);
int balancefac = left - right;
if (balancefac != -1 && balancefac != 0 && balancefac != 1)
{
chk = false;
return chk;
}
return chk;
}
// (helper function) to compute the balance factor
int bfactor(huffmanNode *temp)
{
int lheight = height(temp->left);
int rheight = height(temp->right);
int bfactor = lheight - rheight;
return bfactor;
}
// (helper functions) functions for balancing the nodes
// Right - Right rotation
huffmanNode *rrRotaion(huffmanNode *node)
{
huffmanNode *temp;
temp = node->right;
node->right = temp->left;
temp->left = node;
return temp;
}
// Left - Left rotation
huffmanNode *llRotation(huffmanNode *node)
{
huffmanNode *temp;
temp = node->left;
node->left = temp->right;
temp->right = node;
return temp;
}
// Left - Right rotation
huffmanNode *lrRotation(huffmanNode *node)
{
huffmanNode *temp;
temp = node->left;
node->left = rrRotaion(temp);
return llRotation(node);
}
// Right - Left rotation
huffmanNode *rlRotation(huffmanNode *node)
{
huffmanNode *temp;
temp = node->right;
node->right = llRotation(temp);
return rrRotaion(node);
}
// to balance the nodes
huffmanNode *balance(huffmanNode *temp)
{
int balfac = bfactor(temp);
if (balfac > 1)
{
if (bfactor(temp->left) > 0)
{
temp = llRotation(temp);
}
else
{
temp = lrRotation(temp);
}
}
else if (balfac < -1)
{
if (bfactor(temp->right) > 0)
{
temp = rlRotation(temp);
}
else
{
temp = rrRotaion(temp);
}
}
return temp;
}
// ----------------------------------------------------------------------------------------------
// (helper function) to sort the queue in acending order
// Bubble Sort technique
void sortQueue(Queue<huffmanNode *> &huffque, int len)
{
// Applying "Bubble Sort" technique for sorting the frequency and characters in acending order
// loop to access eacharrcharfreq element
for (int i = huffque.front; i < len; i++)
{
// loop to comparearrcharfreq elements
for (int j = i + 1; j < len; j++)
{
// compare two adjacent elements
if (huffque.arr[i]->freq > huffque.arr[j]->freq)
{
huffmanNode *temp = huffque.arr[j];
huffque.arr[j] = huffque.arr[i];
huffque.arr[i] = temp;
}
}
}
}
// function to traverse the huffman tree and print the codes
void printCode(huffmanNode *root, string str)
{
if (root == NULL)
return;
if (root->chr == '$')
{
printCode(root->left, str + "0");
printCode(root->right, str + "1");
}
if (root->chr != '$')
{
cout << root->chr << " : " << str << "\n";
printCode(root->left, str + "0");
printCode(root->right, str + "1");
}
}
// helper function to compute ABR and Compression Ratio
void SUM_FiLi(huffmanNode *root, string str, float *cr)
{
// base condition
if (root == NULL)
{
return;
}
// using recursion traversing the nodes and concatinating string with 0s and 1s then finding the length of string and multipyloing it by frequency
if (root->chr == '$')
{
SUM_FiLi(root->left, str + "0", cr);
SUM_FiLi(root->right, str + "1", cr);
}
if (root->chr != '$')
{
*cr += root->freq * str.length();
SUM_FiLi(root->left, str + "0", cr);
SUM_FiLi(root->right, str + "1", cr);
}
}
// function to make balanced huffman tree
void balHuffmantree(string s1)
{
// taking object containing array of objects of charcters with their frequencies
CharFreqarrLen arrlen = evaluateFreq(s1);
//
int len = arrlen.len;
// creating huffmannodes and enqueueing them into queue
Queue<huffmanNode *> huffque(len * 2);
for (int i = 0; i < len; i++)
{
huffmanNode *node = new huffmanNode;
node->chr = arrlen.arr[i].chr;
node->freq = arrlen.arr[i].freq;
// enqueueing nodes
huffque.enqueue(node);
}
// making hufftree
huffmanNode *hufftree = new huffmanNode;
while (huffque.front != huffque.rear)
{
// declaring huffman node
huffmanNode *newnode = new huffmanNode;
// initializing node
newnode->freq = 0;
newnode->chr = '$';
newnode->left = NULL;
newnode->right = NULL;
// making nodes for storing dequeued nodes
huffmanNode *temp1 = new huffmanNode;
huffmanNode *temp2 = new huffmanNode;
// dequeueing nodes from queue
temp1 = huffque.dequeue();
temp2 = huffque.dequeue();
// initialing data members of nodes
newnode->left = temp2;
newnode->right = temp1;
newnode->freq = temp1->freq + temp2->freq;
// balancing nodes
newnode = balance(newnode);
// enqueueing newnode in queue
huffque.enqueue(newnode);
len++;
// soritng queue at every iteration (after enqueueing each node)
sortQueue(huffque, len);
hufftree = newnode;
}
// computing total frequency
int totalfreq = 0;
for (int i = 0; i < arrlen.len; i++)
{
totalfreq += arrlen.arr[i].freq;
}
// printing codes and compression ratio
cout << "\n-----------------------" << endl;
cout << "Optimized Huffman Tree: " << endl;
cout << "-----------------------" << endl;
cout << '\n';
printCode(hufftree, "");
// computing compression ratio
float huffcr = 0;
SUM_FiLi(hufftree, "", &huffcr);
float HuffABR = huffcr / totalfreq;
float compressionhuff = 8 / HuffABR;
cout << "\nCompression Ratio: " << compressionhuff << endl;
cout << "---------------------------\n"
<< endl;
}
void huffmanTree(string s1)
{
// taking object containing array of objects of charcters with their frequencies
CharFreqarrLen arrlen = evaluateFreq(s1);
int len = arrlen.len;
// creating huffmannodes and enqueueing them into queue
Queue<huffmanNode *> huffque(len * 2);
for (int i = 0; i < len; i++)
{
huffmanNode *node = new huffmanNode;
node->chr = arrlen.arr[i].chr;
node->freq = arrlen.arr[i].freq;
huffque.enqueue(node);
}
// making hufftree
huffmanNode *hufftree = new huffmanNode;
while (huffque.front != huffque.rear)
{
// declaring huffman node
huffmanNode *newnode = new huffmanNode;
// initializing node
newnode->freq = 0;
newnode->chr = '$';
newnode->left = NULL;
newnode->right = NULL;
// making nodes for storing dequeued nodes
huffmanNode *temp1 = new huffmanNode;
huffmanNode *temp2 = new huffmanNode;
// dequeueing nodes from queue
temp1 = huffque.dequeue();
temp2 = huffque.dequeue();
// initialing data members of nodes
newnode->left = temp2;
newnode->right = temp1;
newnode->freq = temp1->freq + temp2->freq;
// enqueueing newnode in queue
huffque.enqueue(newnode);
len++;
// soritng queue at every iteration (after enqueueing each node)
sortQueue(huffque, len);
hufftree = newnode;
}
// computing total frequency
int totalfreq = 0;
for (int i = 0; i < arrlen.len; i++)
{
totalfreq += arrlen.arr[i].freq;
}
// printing codes and compression ratio
cout << "\n\n-------------" << endl;
cout << "Huffman Tree: " << endl;
cout << "-------------" << endl;
cout << "\n";
printCode(hufftree, "");
// computing compression rate
float huffcr = 0;
SUM_FiLi(hufftree, "", &huffcr);
float HuffABR = huffcr / totalfreq;
float compressionhuff = 8 / HuffABR;
cout << "\nCompression Ratio: " << compressionhuff << endl;
cout << "---------------------------\n"
<< endl;
// checking balance of the tree and printing codes and compression ratio for optimized/balanced huffman tree
if (checkBalance(hufftree) == true)
{
cout << "\nNote: Tree is balanced and in optimal state." << endl;
balHuffmantree(s1);
}
else if (checkBalance(hufftree) == false)
{
balHuffmantree(s1);
}
}
// instead of using main we are using function
void Implementation()
{
// asking for a file or any input from keyborad
int chk;
cout << "\nPress: " << endl;
cout << "1: For File" << endl;
cout << "2: For Input" << endl;
cout << "Enter: ";
cin >> chk;
// variable for taking data from file or from user
string data;
// file
if (chk == 1)
{
cout << "\nEnter the Filename: ";
string filename;
cin >> filename;
// opening file
fstream file;
file.open(filename, ios::in);
// if file exist
if (file.is_open())
{
// getting the text in the file (assuming only one line specifically)
getline(file, data);
huffmanTree(data);
}
// if file don't exist
else
{
cout << "File not found!" << endl;
}
file.close();
}
// input
else if (chk == 2)
{
cout << "\nEnter Text: ";
cin >> data;
huffmanTree(data);
}
// invalid
else
{
cout << "Invalid Input!" << endl;
cout << "Terminated!" << endl;
}
}
int main()
{
Implementation();
return 0;
}