-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
155 lines (148 loc) · 3.67 KB
/
main.js
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
const botao = document.querySelector("a");
botao.addEventListener("click",(event)=>{
const frase = document.forms["form"]["texto"].value;
if (frase == "") {
alert("Insira a frase");
}
else{
var enc = Huffman.encode(frase);
var dec = Huffman.decode(enc);
document.getElementById("dcod").value = dec;
}
})
//---------------------------------------------------------
log = console.log.bind(console);
var Heap = function(fn) {
this.fn = fn || function(e) {
return e;
};
this.items = [];
};
Heap.prototype = {
swap: function(i, j) {
this.items[i] = [
this.items[j],
this.items[j] = this.items[i]
][0];
},
bubble: function(index) {
var parent = ~~((index - 1) / 2);
if (this.item(parent) < this.item(index)) {
this.swap(index, parent);
this.bubble(parent);
}
},
item: function(index) {
return this.fn(this.items[index]);
},
pop: function() {
return this.items.pop();
},
sift: function(index, end) {
var child = index * 2 + 1;
if (child < end) {
if (child + 1 < end && this.item(child + 1) > this.item(child)) {
child++;
}
if (this.item(index) < this.item(child)) {
this.swap(index, child);
return this.sift(child, end);
}
}
},
push: function() {
var lastIndex = this.items.length;
for (var i = 0; i < arguments.length; i++) {
this.items.push(arguments[i]);
this.bubble(lastIndex++);
}
},
get length() {
return this.items.length;
}
};
var Huffman = {
encode: function(data) {
var prob = {};
var tree = new Heap(function(e) {
return e[0];
});
for (var i = 0; i < data.length; i++) {
if (prob.hasOwnProperty(data[i])) {
prob[data[i]]++;
} else {
prob[data[i]] = 1;
}
}
Object.keys(prob).sort(function(a, b) {
return ~~(Math.random() * 2);
}).forEach(function(e) {
tree.push([prob[e], e]);
});
while (tree.length > 1) {
var first = tree.pop(),
second = tree.pop();
tree.push([first[0] + second[0], [first[1], second[1]]]);
}
var dict = {};
var recurse = function(root, string) {
if (root.constructor === Array) {
recurse(root[0], string + '0');
recurse(root[1], string + '1');
} else {
dict[root] = string;
}
};
tree.items = tree.pop()[1];
recurse(tree.items, '');
var result = '';
for (var i = 0; i < data.length; i++) {
result += dict[data.charAt(i)];
}
var header = Object.keys(dict).map(function(e) {
return e.charCodeAt(0) + '|' + dict[e];
}).join('-') + '/';
document.getElementById("cod").value = result;
return header + result;
},
decode: function(string) {
string = string.split('/');
var data = string[1].split(''),
header = {};
string[0].split('-').forEach(function(e) {
var values = e.split('|');
header[values[1]] = String.fromCharCode(values[0]);
});
var result = '';
while (data.length) {
var i = 0,
cur = '';
while (data.length) {
cur += data.shift();
if (header.hasOwnProperty(cur)) {
result += header[cur];
break;
}
}
}
//objeto
var x,j,txt='';
var i = (Object.keys(header));
var j = (Object.values(header));
txt += '<tr><th>Código</th><th>String</th><tr>';
for (x in i) {
txt += '<tr><td>'+ i[x] + '</td>' + '<td>' + j[x] + '</td></tr>';
/*
{
'10': 'b',
'110': 'n',
'111': 'r',
'00': ' ',
'01': 'u'
}
*/
}
document.getElementById("tb").innerHTML = txt;
return result;
}
};