public TreeNode() { value = 0; left = null; right = null; code = ""; }
public TBinarySTree() { root = null; }
/* Build the Huffman tree for the two tables */ /* Algorithm in which we host are, always trying to add value to the left branch. * If it is busy, then the right. And if there is no room, then go back up a level and try out. * Stop at the level necessary to equal the length of the code. The left branch corresponds to a value of 0, the right one. * / /* Do not need every time to start from the top. Added value - go back to a higher level. * The right branch there? If yes, then go up again. * If not, create the right branch and go there */ private void BuildHuffmanTree(ref TreeNode root, ref int ctr, int acc, ref int path, ref int i, string code) { while (ctr < acc) // until we have completed all the tree { if (root == null) { root = new TreeNode(); path++; // If we have reached the desired depth of the tree (that is, the length of the code, which falls on value) if (path == property.newlength[i]) { root.value = property.HUFFVAL[i]; root.code = code; property.hcodes.Add(code); ctr++; i++; path--; return; } } else { BuildHuffmanTree(ref root.left, ref ctr, acc, ref path, ref i, code + "0"); // left if (root.right == null) { BuildHuffmanTree(ref root.right, ref ctr, acc, ref path, ref i, code + "1"); // straight } path--; return; } } }
/* Traversing the tree Huffman while reading ECS */ private void GoGoTree(ref TreeNode root, byte bit, ref bool node, ref int val) { if (bit == 0) { if (root.left != null) { root = root.left; if (root.left == null && root.right == null) { node = true; val = root.value; return; } } } if (bit == 1) { if (root.right != null) { root = root.right; if (root.left == null && root.right == null) { node = true; val = root.value; return; } } } }
/* Построение дерева Хаффмана для двух таблиц*/ /* Алгоритм: в каком бы мы узле не находились, всегда пытаемся добавить значение в левую ветвь. * А если она занята, то в правую. А если и там нет места, то возвращаемся на уровень выше, и пробуем оттуда. * Остановиться надо на уровне равном длине кода. Левым ветвям соответствует значение 0, правым 1. */ /*Не нужно каждый раз начинать с вершины. Добавила значение - вернись на уровень выше. * Правая ветвь существует? Если да, то иду опять вверх. * Если нет, то создаю правую ветвь и иду туда*/ private void BuildHuffmanTree(ref TreeNode root, ref int ctr, int acc, ref int path, ref int i, string code) { while (ctr < acc) // пока мы не заполнили все дерево { if (root == null) { root = new TreeNode(); path++; // если мы достигли нужной глубины дерева (то есть длины кода, которая приходится на значение) if (path == property.newlength[i]) { root.value = property.HUFFVAL[i]; root.code = code; property.hcodes.Add(code); ctr++; i++; path--; return; } } else { BuildHuffmanTree(ref root.left, ref ctr, acc, ref path, ref i, code+"0"); // налево if (root.right == null) { BuildHuffmanTree(ref root.right, ref ctr, acc, ref path, ref i, code+"1"); // направо } path--; return; } } }