Ejemplo n.º 1
0
        private int setPosition(bool right, int level, List <HuffmanTree> huffmanTree, int idx, int prevX, int pivot)
        {
            if (idx == -1 || huffmanTree[idx].count == 0)
            {
                return(0);
            }

            HuffmanTree node = huffmanTree[idx];

            if (right)
            {
                prevX = setPosition(right, level + 1, huffmanTree, node.left, prevX - 1, pivot) + 1;
                if (prevX <= pivot)
                {
                    prevX = pivot + 1;
                }

                node.x = prevX;
                node.y = level;

                prevX = Math.Max(setPosition(right, level + 1, huffmanTree, node.right, prevX + 1, prevX), prevX);
            }
            else
            {
                prevX = setPosition(right, level + 1, huffmanTree, node.right, prevX + 1, pivot) - 1;
                if (prevX >= pivot)
                {
                    prevX = pivot - 1;
                }

                node.x = prevX;
                node.y = level;

                prevX = Math.Min(setPosition(right, level + 1, huffmanTree, node.left, prevX - 1, prevX), prevX);
            }

            return(prevX);
        }
Ejemplo n.º 2
0
        public static List <HuffmanTree> BuildTree(string file, int radius)
        {
            List <HuffmanTree> huffmanTree = new List <HuffmanTree>();

            FileStream   fs = new FileStream(file, FileMode.OpenOrCreate);
            StreamReader sr = new StreamReader(fs);

            string count = sr.ReadLine();

            string[] countArray = count.Split(' ');

            if (countArray[countArray.Length - 1].Equals(""))
            {
                countArray = countArray.Take(countArray.Length - 1).ToArray();
            }

            string values = sr.ReadLine();

            string[] valuesArray = values.Split(' ');

            if (valuesArray[valuesArray.Length - 1].Equals(""))
            {
                valuesArray = valuesArray.Take(valuesArray.Length - 1).ToArray();
            }

            HuffmanTree node;

            node = new HuffmanTree("", int.Parse(countArray[0]));
            huffmanTree.Add(node);

            for (int i = 1, j = 0, cur = 0; i < countArray.Length; i += 2, cur++)
            {
                if (huffmanTree[cur].count == 0)
                {
                    i -= 2;
                    continue;
                }

                HuffmanTree nodeF;
                HuffmanTree nodeS;
                nodeF = new HuffmanTree("", int.Parse(countArray[i]));
                nodeS = new HuffmanTree("", int.Parse(countArray[i + 1]));

                huffmanTree[cur].left  = i;
                huffmanTree[cur].right = i + 1;

                huffmanTree.Add(nodeF);
                huffmanTree.Add(nodeS);

                if (countArray[i].Equals("0"))
                {
                    node = huffmanTree.ElementAt(cur);
                    node.SetValue(valuesArray[j++]);
                }
            }

            sr.Close();
            fs.Close();

            return(huffmanTree);
        }