private char?ReadChar(int index, string code, out int count) { Node current = Root; count = 0; while (true) { count++; if (current == _nyt) { return(null); } if (current.IsLeaf()) { count--; return(current.Symbol); } char bit = code[index++]; if (bit == '0') { current = current.Left; } else if (bit == '1') { current = current.Right; } } }
public string Decode(BitArray bits) { Node current = Root; string decoded = ""; foreach (bool bit in bits) { if (bit) { if (current.Right != null) { current = current.Right; } } else { if (current.Left != null) { current = current.Left; } } if (Node.IsLeaf(current)) { decoded += current.Symbol; current = Root; } } return(decoded); }
private void GenerateBitSequence(Node node) { if (node == null) { throw new NullReferenceException(); } if (node == root) { node.code = new List <bool>(); } if (node.left != null) { node.left.code = new List <bool>(node.code); node.left.code.Add(false); GenerateBitSequence(node.left); } if (node.right != null) { node.right.code = new List <bool>(node.code); node.right.code.Add(true); GenerateBitSequence(node.right); } if (node.IsLeaf()) { codes[node.Key] = node.code.ToBitArray(); } }
private static void PrintRec(Node root) { if (!root.IsLeaf()) { Console.Write(root.Value + " "); if (root.left != null) { PrintRec(root.left); } if (root.right != null) { PrintRec(root.right); } } else { Console.Write("*" + (int)root.Key + ":" + root.Value + " "); } }
public void Preorder(Node node, Code huffCode) { if (node != null) { if (node.IsLeaf()) { codeTable.Add(node.Symbol, huffCode); decodeTable.Add(huffCode, node.Symbol); return; } Code _huffCodeL = new Code(); _huffCodeL.AddRange(huffCode); Code _huffCodeR = new Code(); _huffCodeR.AddRange(huffCode); _huffCodeL.Add(false); Preorder(node.Left, _huffCodeL); _huffCodeR.Add(true); Preorder(node.Right, _huffCodeR); } }
private static void DecodeAndPrintTree(HuffmanTree huffmanTree) { Node current = huffmanTree.root; while (readerIn.BaseStream.Position != readerIn.BaseStream.Length) { byte[] bufferOfBytes = readerIn.ReadBytes(MAX_BUFFER_SIZE / 8); BitArray bitArray = new BitArray(bufferOfBytes); for (int i = 0; i < bitArray.Length; i++) { if (bitArray[i] == false) { if (current.left == null) { Exit(); } current = current.left; } else { if (current.right == null) { Exit(); } current = current.right; } if (current.IsLeaf()) { if (current.Value != 0) { writerOut.Write(current.Key); current.Value--; } current = huffmanTree.root; } } } }
private static void PrintEncodedNode(Node root) { byte[] encodedNode = root.Encode().GetByteArray(); if (!root.IsLeaf()) { writerOut.Write(encodedNode); if (root.left != null) { PrintEncodedNode(root.left); } if (root.right != null) { PrintEncodedNode(root.right); } } else { writerOut.Write(encodedNode); } }
public void ProduceByteArrayNew(BitArray bitArray, Node root, int size, int start) { byteArray = new Byte[size]; byteArrayElement = 0; Node node = root; while (byteArrayElement < size) { if (node != null) { if (node.IsLeaf()) { byteArray[byteArrayElement] = node.Symbol; byteArrayElement++; if (start < bitArray.Count) { node = root; } else { return; } } else if (start < bitArray.Count) { if (!bitArray[bitArray.Count - start - 1]) { node = node.Left; start++; } else { node = node.Right; start++; } } } } }