private static void CreateTable(string str) { Table.Clear(); FreeSpace.Clear(); for (int i = 0; i < str.Length; i++) { bool find = false; for (int j = 0; j < Table.Count; j++) { if (Table[j].Key == str[i]) { find = true; Table[j].Value.Weight++; } } if (!find) { var nw = new TFF_Node(); nw.Weight = 1; FreeSpace.Add(nw); Table.Add(new KeyValuePair <char, TFF_Node>(str[i], nw)); } } FreeSpace.Sort(); }
private static void GrowTree() { while (FreeSpace.Count > 1) { var nw = new TFF_Node(); nw.Left = FreeSpace[0]; nw.Right = FreeSpace[1]; nw.Left.Parent = nw.Right.Parent = nw; nw.Weight = nw.Left.Weight + nw.Right.Weight; nw.Left.Code = '0'; nw.Right.Code = '1'; FreeSpace.Remove(nw.Left); FreeSpace.Remove(nw.Right); FreeSpace.Add(nw); FreeSpace.Sort(); } FreeSpace[0].Code = 'P'; }
public static string getCode(TFF_Node node) { return(node.Code + (node.Parent.Code == 'P' ? null : getCode(node.Parent))); }