-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmanTree.cs
132 lines (105 loc) · 3.6 KB
/
HuffmanTree.cs
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
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Node {
public int Symbol { get; set; }
public int Frequency { get; set; }
public Node Right { get; set; }
public Node Left { get; set; }
public List<bool> Traverse(int symbol, List<bool> data) {
// Leaf
if (Right == null && Left == null) {
if (symbol.Equals(Symbol)) {
return data;
} else {
return null;
}
} else {
List<bool> left = null;
List<bool> right = null;
if (Left != null) {
List<bool> leftPath = new List<bool>();
leftPath.AddRange(data);
leftPath.Add(false);
left = Left.Traverse(symbol, leftPath);
}
if (Right != null) {
List<bool> rightPath = new List<bool>();
rightPath.AddRange(data);
rightPath.Add(true);
right = Right.Traverse(symbol, rightPath);
}
if (left != null) {
return left;
} else {
return right;
}
}
}
}
public class HuffmanTree {
private List<Node> nodes = new List<Node>();
public Node Root { get; set; }
public Dictionary<int, int> Frequencies = new Dictionary<int, int>();
public void Build(List<int> source) {
for (int i = 0; i < source.Count; i++) {
if (!Frequencies.ContainsKey(source[i])) {
Frequencies.Add(source[i], 0);
}
Frequencies[source[i]]++;
}
foreach (KeyValuePair<int, int> symbol in Frequencies) {
nodes.Add(new Node() { Symbol = symbol.Key, Frequency = symbol.Value });
}
while (nodes.Count > 1) {
List<Node> orderedNodes = nodes.OrderBy(node => node.Frequency).ToList();
if (orderedNodes.Count >= 2) {
// Take first two items
List<Node> taken = orderedNodes.Take(2).ToList();
// Create a parent node by combining the frequencies
Node parent = new Node() {
Symbol = '*',
Frequency = taken[0].Frequency + taken[1].Frequency,
Left = taken[0],
Right = taken[1]
};
nodes.Remove(taken[0]);
nodes.Remove(taken[1]);
nodes.Add(parent);
}
Root = nodes.FirstOrDefault();
}
}
public BitArray Encode(List<int> source) {
List<bool> encodedSource = new List<bool>();
for (int i = 0; i < source.Count; i++) {
List<bool> encodedSymbol = Root.Traverse(source[i], new List<bool>());
encodedSource.AddRange(encodedSymbol);
}
BitArray bits = new BitArray(encodedSource.ToArray());
return bits;
}
public List<int> Decode(BitArray bits) {
Node current = Root;
List<int> decoded = new List<int>();
foreach (bool bit in bits) {
if (bit) {
if (current.Right != null) {
current = current.Right;
}
} else {
if (current.Left != null) {
current = current.Left;
}
}
if (IsLeaf(current)) {
decoded.Add(current.Symbol);
current = Root;
}
}
return decoded;
}
public bool IsLeaf(Node node) {
return (node.Left == null && node.Right == null);
}
}