public int CompareTo(object obj) { HuffmanTree tree = obj as HuffmanTree; Node node = tree.root; if (this.IsLeaf() && tree.IsLeaf()) { int comparisonResult = this.root.weight.CompareTo(node.weight); if (comparisonResult == 0) { return(this.root.symbol.CompareTo(node.symbol)); } else { return(comparisonResult); } } else if (this.IsLeaf() && !tree.IsLeaf()) { int comparisonResult = this.root.weight.CompareTo(node.weight); if (comparisonResult == 0) { return(-1); } else { return(comparisonResult); } } else if (!this.IsLeaf() && tree.IsLeaf()) { int comparisonResult = this.root.weight.CompareTo(node.weight); if (comparisonResult == 0) { return(1); } else { return(comparisonResult); } } else { int comparisonResult = this.root.weight.CompareTo(node.weight); if (comparisonResult == 0) { return(this.counterIndex.CompareTo(tree.counterIndex)); } else { return(comparisonResult); } } }
/// <summary> /// Will print nodes of Huffman tree. /// </summary> /// <param name="Tree"></param> public void PrintTreeNodes(HuffmanTree Tree) { ulong headerPart = 0; byte[] part; if (Tree.IsLeaf()) { paths[Tree.root.symbol] = new BinaryStream(tempStream); headerPart |= (ushort)Tree.root.symbol; headerPart <<= 55; //TODO:aodjsfghnbiausydb headerPart |= (ulong)Tree.root.weight; headerPart <<= 1; headerPart |= 1; part = BitConverter.GetBytes(headerPart); OutStream.Write(part, 0, part.Length); OutStream.Flush(); } else { headerPart |= (ulong)Tree.root.weight; headerPart <<= 1; part = BitConverter.GetBytes(headerPart); OutStream.Write(part, 0, part.Length); OutStream.Flush(); } if (Tree.root.Left != null) { tempStream.NoOfBits++; tempStream.path <<= 1; PrintTreeNodes(Tree.root.Left); tempStream.path ^= 1; //last bit is set form 0 to 1 PrintTreeNodes(Tree.root.Right); tempStream.path >>= 1; tempStream.NoOfBits--; } }
public void Print(HuffmanTree tree, TextWriter writer) { if (!tree.IsLeaf()) { writer.Write("{0} ", tree.root.weight); writer.Flush(); } else { writer.Write("*{0}:{1} ", tree.root.symbol, tree.root.weight); writer.Flush(); } if (tree.root.Left != null) { Print(tree.root.Left, writer); } if (tree.root.Right != null) { Print(tree.root.Right, writer); } }