示例#1
0
 public void Enqueue(HuffmanNode n)
 {
     nodes.Add(n);
     heapSize++;
     BuildHeap(heapSize);
 }
示例#2
0
        public void Compress(string text)
        {
            HuffmanNode            node     = new HuffmanNode();
            List <HuffmanNode>     nodeList = node.GetList(text);
            PriorityQueue <string> PQ       = new PriorityQueue <string>();

            foreach (HuffmanNode n in nodeList)
            {
                PQ.Enqueue(n);
            }
            int size = PQ.Size;

            while (size >= 1)
            {
                HuffmanNode hNode = new HuffmanNode();
                HuffmanNode a     = new HuffmanNode();
                HuffmanNode b     = new HuffmanNode();
                if (!PQ.isEmpty())
                {
                    a = PQ.Dequeue();
                }
                if (!PQ.isEmpty())
                {
                    b = PQ.Dequeue();
                }
                bool isA = true;
                bool isB = true;
                foreach (HuffmanNode n in nList)
                {
                    if (n == a)
                    {
                        isA = false;
                    }
                    if (n == b)
                    {
                        isB = false;
                    }
                }

                if (isA && a.Symbol != null)
                {
                    nList.Add(a);
                }
                if (isB && b.Symbol != null)
                {
                    nList.Add(b);
                }

                hNode.Left  = a;
                hNode.Right = b;

                hNode.Frequency   = a.Frequency + b.Frequency;
                hNode.Symbol      = a.Symbol + b.Symbol;
                hNode.Left.Parent = hNode.Left.Parent = hNode;
                PQ.Enqueue(hNode);
                size--;
            }
            nList.Reverse();
            foreach (HuffmanNode n in nList)
            {
                AssignCode();
            }
        }
示例#3
0
 public HuffmanNode(string symbol, int frequency, string code, HuffmanNode left, HuffmanNode right, HuffmanNode parent)
 {
     this.Symbol    = symbol;
     this.Frequency = frequency;
     this.Code      = code;
     this.Left      = left;
     this.Right     = right;
     this.Parent    = parent;
 }