HuffmanNode FindNodeMinWeightExceptNode(List <HuffmanNode> nodes, HuffmanNode nodeToExcept) { HuffmanNode min = nodes[0]; for (var i = 0; i < nodes.Count; i++) { if (nodes[i] != nodeToExcept) { min = nodes[i]; break; } } for (int i = 1; i < nodes.Count; i++) { if (nodes[i].weight < min.weight && nodes[i] != nodeToExcept) { min = nodes[i]; } } return(min); }
public List <bool> GetCharCode(char v, HuffmanNode node, List <bool> tempResult) { if (node.letters.Count == 1 && node.letters[0] == v) { return(tempResult); } else if (node.leftBranch != null && node.leftBranch.letters.Contains(v)) { tempResult.Add(false); return(GetCharCode(v, node.leftBranch, tempResult)); } else if (node.rightBranch != null && node.rightBranch.letters.Contains(v)) { tempResult.Add(true); return(GetCharCode(v, node.rightBranch, tempResult)); } else { throw new Exception(); } }
public string Decode(BitArray bits) { var result = string.Empty; HuffmanNode currentNode = rootOfTree; foreach (bool bit in bits) { if (bit) { currentNode = currentNode.rightBranch; } else { currentNode = currentNode.leftBranch; } if (currentNode.letters.Count == 1) { result += currentNode.letters[0]; currentNode = rootOfTree; } } return(result); }