public byte[] DecodeTree(CompNode tree, bool[] data) { List <byte> result = new List <byte>(); CompNode selectedNode = tree; foreach (bool singleBit in data) { CompNodeBase currentNode = selectedNode.Next(singleBit); if (currentNode is CompNodeValue vNode) { result.Add(vNode.Value); selectedNode = tree; } else { selectedNode = currentNode as CompNode; } } return(result.ToArray()); }
public void CreateTree(byte[] data, out CompNode tree, out CompNodeValue[] valueNodes) { CompNodeComparer nodeComparer = new CompNodeComparer(); CountedValue[] countedValues = CountValues(data); List <CompNodeBase> nodes = new List <CompNodeBase>(); foreach (CountedValue cv in countedValues) { nodes.Add(new CompNodeValue(cv.Value, cv.Count)); } valueNodes = nodes.Cast <CompNodeValue>().ToArray(); while (nodes.Count > 1) { nodes.Add(new CompNode(nodes[0], nodes[1])); nodes.RemoveAt(0); nodes.RemoveAt(0); nodes.Sort(nodeComparer); } tree = nodes[0] as CompNode; }