-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClass2.cs
101 lines (94 loc) · 2.21 KB
/
Class2.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace File_Compression
{
public class HuffmanNode
{
string symbol;
int frequency;
string code;
HuffmanNode left;
HuffmanNode right;
HuffmanNode parent;
List<HuffmanNode> nodes = new List<HuffmanNode>();
int Size
{
get
{
return nodes.Count;
}
}
public HuffmanNode()
{
}
public HuffmanNode(string c, int f)
{
this.Symbol = c;
this.Frequency = f;
}
public HuffmanNode(string symbol, int frequency, string code, HuffmanNode left, HuffmanNode right, HuffmanNode parent)
{
this.Symbol = symbol;
this.Frequency = frequency;
this.Code = code;
this.Left = left;
this.Right = right;
this.Parent = parent;
}
public string Symbol { get => symbol; set => symbol = value; }
public int Frequency { get => frequency; set => frequency = value; }
public string Code { get => code; set => this.code = value; }
internal HuffmanNode Left { get => left; set => left = value; }
internal HuffmanNode Right { get => right; set => right = value; }
internal HuffmanNode Parent { get => parent; set => parent = value; }
public List<HuffmanNode> GetList(string text)
{
var charsToRemove = new string[] { "\n", "\r" };
foreach (var c in charsToRemove)
{
text = text.Replace(c, string.Empty);
}
for (int i = 0; i < text.Length; i++)
{
HuffmanNode n = new HuffmanNode();
n.symbol = text[i].ToString();
n.frequency++;
bool flag = false;
for (int j = 0; j < nodes.Count; j++)
{
if (n.symbol == nodes[j].symbol)
{
flag = true;
}
}
if (!flag)
{
nodes.Add(n);
}
else
{
for (int q = 0; q < nodes.Count; q++)
{
if (nodes[q].symbol == text[i].ToString())
{
nodes[q].frequency++;
}
}
}
}
return this.nodes;
}
public void Display()
{
Console.WriteLine(nodes.Count);
Console.WriteLine("Symbol,\tFrequency");
foreach (HuffmanNode n in nodes)
{
Console.WriteLine(n.Symbol + ",\t" + n.Frequency);
}
}
}
}