build method
Implementation
void build(List<int> data) {
final freq = <int, int>{};
for (var b in data) {
freq[b] = (freq[b] ?? 0) + 1;
}
final heap = <_Node>[];
for (var e in freq.entries) {
heap.add(_Node(e.key, e.value));
}
heap.sort();
// simple binary heap operations
while (heap.length > 1) {
final a = heap.removeAt(0);
final b = heap.removeAt(0);
final merged = _Node(null, a.freq + b.freq, a, b);
heap.add(merged);
heap.sort();
}
_root = heap.isNotEmpty ? heap.first : null;
_codes.clear();
if (_root != null) _buildCodes(_root!, '');
}