private int[] FindSmall(HuffmanTree <T>[] forest) //找到森林中权值最小的两棵树 { int[] result = new int[2]; HuffmanTree <T>[] temp = new HuffmanTree <T> [forest.Length]; for (int i = 0; i < temp.Length; i++) { temp[i] = forest[i]; } for (int i = 0; i < temp.Length; i++) { for (int j = i; j > 0; j--) { if (temp[j].root.Weight < temp[j - 1].root.Weight) { HuffmanTree <T> temp1 = temp[j]; temp[j] = temp[j - 1]; temp[j - 1] = temp1; } } } int x = temp[0].root.Weight, y = temp[1].root.Weight; for (int i = 0; i < forest.Length; i++) { if (forest[i].root.Weight == x) { result[0] = i; } } for (int i = 0; i < forest.Length; i++) { if (forest[i].root.Weight == y && i != result[0]) { result[1] = i; } } return(result); }
private HuffmanTree <char> BuildTree() //建立Huffman树 { char[] temp = message.ToCharArray(); char[] letter1 = new char[temp.Length]; int k = 0; for (int i = 0; i < temp.Length; i++) { if (Contain(letter1, temp[i]) == 0) { letter1[k] = temp[i]; k++; } } int num = 0; for (int i = 0; i < letter1.Length; i++) { if (letter1[i] != '\0') { num++; } } char[] letter = new char[num]; int[] weight = new int[num]; for (int i = 0; i < letter.Length; i++) { letter[i] = letter1[i]; } for (int i = 0; i < weight.Length; i++) { weight[i] = Contain(temp, letter[i]); } HuffmanTree <char> codeTree = new HuffmanTree <char>(letter, weight); return(codeTree); }
private HuffmanTree <T>[] ClearNull(HuffmanTree <T>[] forest) //清除森林中空树 { int x = 0; for (int i = 0; i < forest.Length; i++) { if (forest[i] != null) { x++; } } HuffmanTree <T>[] result = new HuffmanTree <T> [x]; int y = 0; for (int i = 0; i < forest.Length; i++) { if (forest[i] != null) { result[y] = forest[i]; y++; } } return(result); }
public HuffmanTree(T[] data, int[] weight) //唯一构造 { if (data.Length != weight.Length) { Console.WriteLine("输入数据数组与权值数组长度不同!"); } else { HuffmanTree <T>[] forest = new HuffmanTree <T> [data.Length]; int leng = forest.Length; for (int i = 0; i < forest.Length; i++) { forest[i] = new HuffmanTree <T>(); forest[i].root = new HuffmanTreeNode <T>(data[i], weight[i]); } for (int i = 0; i < leng - 1; i++) { int x = FindSmall(forest)[0], y = FindSmall(forest)[1]; HuffmanTree <T> temp1 = forest[x]; HuffmanTree <T> temp2 = forest[y]; forest[x] = null; forest[y] = null; HuffmanTree <T> newtree = new HuffmanTree <T>(default(T), temp1.root.Weight + temp2.root.Weight); if (temp1.root.Weight < temp2.root.Weight) { newtree.root.LeftChild = temp1.root; newtree.root.RightChild = temp2.root; } else if (temp1.root.Weight > temp2.root.Weight) { newtree.root.LeftChild = temp2.root; newtree.root.RightChild = temp1.root; } else { if (GetHeight(temp1.root) < GetHeight(temp2.root)) { newtree.root.LeftChild = temp1.root; newtree.root.RightChild = temp2.root; } else if (GetHeight(temp1.root) > GetHeight(temp2.root)) { newtree.root.LeftChild = temp2.root; newtree.root.RightChild = temp1.root; } else { if (x < y) { newtree.root.LeftChild = temp1.root; newtree.root.RightChild = temp2.root; } else { newtree.root.LeftChild = temp2.root; newtree.root.RightChild = temp1.root; } } } forest[x] = newtree; forest = ClearNull(forest); } root = forest[0].root; FindParent(); } }
public HuffmanCode(string xx) { message = xx; codeTree = BuildTree(); }