public HuffmanTreeNode(HuffmanTreeNode left, HuffmanTreeNode right) { Left = left; Right = right; Left.Parent = Right.Parent = this; if (ByteCount == null) { ByteCount = new ByteCount(); } ByteCount.Count = Left.ByteCount.Count + Right.ByteCount.Count; }
public HuffmanTree(ByteCount[] bytesFreqs) { var nodes = new List <HuffmanTreeNode>(); for (int i = 0; i < bytesFreqs.Length; i++) { nodes.Add(new HuffmanTreeNode { ByteCount = new ByteCount { Byte = bytesFreqs[i].Byte, Count = bytesFreqs[i].Count } }); } HuffmanTreeNode left, right; while (nodes.Count > 1) { int min = nodes[0].ByteCount.Count; int minInd = 0; for (int j = 1; j < nodes.Count; j++) { if (min > nodes[j].ByteCount.Count) { min = nodes[j].ByteCount.Count; minInd = j; } } left = nodes[minInd]; nodes.RemoveAt(minInd); min = nodes[0].ByteCount.Count; minInd = 0; for (int j = 1; j < nodes.Count; j++) { if (min > nodes[j].ByteCount.Count) { min = nodes[j].ByteCount.Count; minInd = j; } } right = nodes[minInd]; nodes.RemoveAt(minInd); nodes.Add(new HuffmanTreeNode(left, right)); } Root = nodes[0]; CalculateBytes(); }
private void TraverseTree(HuffmanTreeNode node, int length, int value) { if (node.Left != null) { length++; TraverseTree(node.Left, length, value); value |= (1 << (length - 1)); TraverseTree(node.Right, length, value); } else { CompressedBytes[node.ByteCount.Byte] = new CompressedByte { Length = length, Value = value } }; } }
public static byte GetValue(HuffmanTreeNode root, byte[] bytes, ref int bitPos) { int curBytePos = bitPos / 8; int curBitInBytePos = bitPos % 8; int bit; var curNode = root; while (curNode.Left != null) { bit = bytes[curBytePos] & (128 >> curBitInBytePos); if (bit == 0) { curNode = curNode.Left; } else { curNode = curNode.Right; } curBytePos += (curBitInBytePos + 1) / 8; curBitInBytePos = (curBitInBytePos + 1) % 8; } bitPos = curBytePos * 8 + curBitInBytePos; return(curNode.ByteCount.Byte); }
public HuffmanTree(ByteCount[] bytesFreqs) { var nodes = new List<HuffmanTreeNode>(); for (int i = 0; i < bytesFreqs.Length; i++) nodes.Add(new HuffmanTreeNode { ByteCount = new ByteCount { Byte = bytesFreqs[i].Byte, Count = bytesFreqs[i].Count } }); HuffmanTreeNode left, right; while (nodes.Count > 1) { int min = nodes[0].ByteCount.Count; int minInd = 0; for (int j = 1; j < nodes.Count; j++) if (min > nodes[j].ByteCount.Count) { min = nodes[j].ByteCount.Count; minInd = j; } left = nodes[minInd]; nodes.RemoveAt(minInd); min = nodes[0].ByteCount.Count; minInd = 0; for (int j = 1; j < nodes.Count; j++) if (min > nodes[j].ByteCount.Count) { min = nodes[j].ByteCount.Count; minInd = j; } right = nodes[minInd]; nodes.RemoveAt(minInd); nodes.Add(new HuffmanTreeNode(left, right)); } Root = nodes[0]; CalculateBytes(); }
public HuffmanTreeNode(HuffmanTreeNode left, HuffmanTreeNode right) { Left = left; Right = right; Left.Parent = Right.Parent = this; if (ByteCount == null) ByteCount = new ByteCount(); ByteCount.Count = Left.ByteCount.Count + Right.ByteCount.Count; }
private void TraverseTree(HuffmanTreeNode node, int length, int value) { if (node.Left != null) { length++; TraverseTree(node.Left, length, value); value |= (1 << (length - 1)); TraverseTree(node.Right, length, value); } else CompressedBytes[node.ByteCount.Byte] = new CompressedByte { Length = length, Value = value }; }
public static byte GetValue(HuffmanTreeNode root, byte[] bytes, ref int bitPos) { int curBytePos = bitPos / 8; int curBitInBytePos = bitPos % 8; int bit; var curNode = root; while (curNode.Left != null) { bit = bytes[curBytePos] & (128 >> curBitInBytePos); if (bit == 0) curNode = curNode.Left; else curNode = curNode.Right; curBytePos += (curBitInBytePos + 1) / 8; curBitInBytePos = (curBitInBytePos + 1) % 8; } bitPos = curBytePos * 8 + curBitInBytePos; return curNode.ByteCount.Byte; }