public BinaryTree(Node i_node, ref BinaryTree i_left, ref BinaryTree i_right) { IsInTheCurrentLevel = true; Node = i_node; Left = i_left; Right = i_right; }
public BinaryTree(Node i_node) { IsInTheCurrentLevel = true; Node = i_node; Left = null; Right = null; }
public BinaryTree() { IsInTheCurrentLevel = true; Node = new Node(); Left = null; Right = null; }
private Node BuildHuffmanTree(string[] dictionary) { Node root = new Node(); int length = dictionary.Length; for (int i = 0; i < length; i++) { string element = dictionary[i]; Node work = root; foreach (char bit in element) { if (bit == '0') { if (work.Left0 == null) { work.Left0 = new Node(); } work = work.Left0; } else if (bit == '1') { if (work.Right1 == null) { work.Right1 = new Node(); } work = work.Right1; } else { //this should never happen System.Diagnostics.Debug.Assert(false, "Invalid bit in dictionary. 0 or 1 expected. Actual value: " + bit); } } work.Value = i; } return root; }
// Utworzenie nowego węzła private void MyCreateNewNode(List<Node> NodesList, IEnumerable<Node> TakenNodes) { var NewNode = new Node(); NewNode.ChildLeft = TakenNodes.ElementAt(0); NewNode.ChildLeft.Parent = NewNode; NewNode.ChildRight = TakenNodes.ElementAt(1); NewNode.ChildRight.Parent = NewNode; NewNode.Probability = NewNode.ChildLeft.Probability + NewNode.ChildRight.Probability; NodesList.Add(NewNode); NodesList.Remove(NewNode.ChildLeft); NodesList.Remove(NewNode.ChildRight); }
public void GetHuffmanCode(Node node, ref List<KeyValuePair<byte, List<bool>>> l_kvp, List<bool> way) { // On vérifie si on est en présence d'un Sum ou d'une Occurence if (Object.ReferenceEquals(node.GetType(), typeof(Sum))) { // Récupération des noeud de la Sum ArrayList al = ((Sum)node).GetNodes(); // On ajoute un 0 (false) et on récursive List<bool> l1 = new List<bool>(way); l1.Add(false); GetHuffmanCode(((Node)al[0]), ref l_kvp, l1); // On ajoute un 1 (true) et on récursive List<bool> l2 = new List<bool>(way); l2.Add(true); GetHuffmanCode(((Node)al[1]), ref l_kvp, l2); } else { // On ajoute à la liste la pair <ASCII, code Huffman> Occurence o = (Occurence) node; l_kvp.Add(new KeyValuePair<byte, List<bool>>(o.GetKVP().Key, way)); } }