-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoder.cpp
More file actions
59 lines (51 loc) · 1.27 KB
/
decoder.cpp
File metadata and controls
59 lines (51 loc) · 1.27 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
#include <iostream>
#include <string>
#include "decoder.h"
using namespace std;
Node* Decode::getRoot()
{
return root;
}
void Decode::addleaf_at(int val, string strng_bin) {
Node *curr;
curr = root;
for (int i = 0; i < strng_bin.length(); i++) {
if (strng_bin.at(i) == '0') {
if (curr->left == NULL) {
Node* newNode = new (struct Node);
curr->left = newNode;
}
curr = curr->left;
}
else if (strng_bin.at(i) == '1') {
if (curr->right == NULL) {
Node* newNode = new (struct Node);
curr->right = newNode;
}
curr = curr->right;
}
}
curr->value = val;
curr->isLeaf = true;
}
int Decode::extract(string bin_strng) {
Node *curr;
curr = root;
for (int i = 0; i < bin_strng.length(); i++) {
if (curr == NULL) {
return -1;
}
else if (bin_strng.at(i) == '0') {
curr = curr->left;
}
else if (bin_strng.at(i) == '1') {
curr = curr->right;
}
}
if (curr->isLeaf == true) {
return curr->value;
}
else {
return -1;
}
}