public void LevelOrder() //层序遍历 { Console.WriteLine("该Huffman树的层序遍历为:"); Queue <HuffmanTreeNode <T> > exam = new Queue <HuffmanTreeNode <T> >(); HuffmanTreeNode <T> p = root; if (p != null) { exam.Enqueue(p); } while (exam.Count != 0) { p = exam.Dequeue(); Visit(p); if (p.LeftChild != null) { exam.Enqueue(p.LeftChild); } if (p.RightChild != null) { exam.Enqueue(p.RightChild); } } Console.WriteLine(); }
private void FindParent() //确定双亲指针 { Stack <HuffmanTreeNode <T> > exam = new Stack <HuffmanTreeNode <T> >(); HuffmanTreeNode <T> p = root; while (p != null || exam.Count != 0) { if (p != null) { exam.Push(p); if (p.LeftChild != null) { p.LeftChild.Parent = p; } p = p.LeftChild; } else { p = exam.Pop(); if (p.RightChild != null) { p.RightChild.Parent = p; } p = p.RightChild; } } }
public string TranslateCode(string xx) //翻译Huffman编码至原始信息 { char[] temp = xx.ToCharArray(); string result = ""; int i = 0; HuffmanTreeNode <char> p = codeTree.Root; while (!CharArrayJuddge(temp)) { int x = i; while (!p.IsLeaf()) { if (temp[i] == '0') { p = p.LeftChild; } else { p = p.RightChild; } i++; } result += p.Data; p = codeTree.Root; for (int j = x; j < i; j++) { temp[j] = '\0'; } } return(result); }
public int GetHeight(HuffmanTreeNode <T> xx) //获取二叉树高度 { if (xx == null) { return(0); } return(Math.Max(GetHeight(xx.LeftChild), GetHeight(xx.RightChild)) + 1); }
private string FindCode(char xx) //将单字符翻译成Huffman编码 { string result = ""; string result1 = ""; HuffmanTreeNode <char> p = codeTree.Root; while (p != null) { if (p.Tag == 0) { p.Tag = 1; if (p.LeftChild != null) { p = p.LeftChild; result += "0"; } } else if (p.Tag == 1) { p.Tag = 2; if (p.RightChild != null) { p = p.RightChild; result += "1"; } } else { p.Tag = 0; if (p.Data == xx) { result1 = result; } p = p.Parent; if (p != codeTree.Root || (p == codeTree.Root && codeTree.Root.Tag == 1)) { result = result.Substring(0, result.Length - 1); } } } return(result1); }
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 HuffmanTree(T data, int weight) { root = new HuffmanTreeNode <T>(data, weight); }
public HuffmanTree() { root = null; }
private void Visit(HuffmanTreeNode <T> node) //访问结点 { Console.Write("{0}&{1}", node.Data, node.Weight); Console.Write(' '); }