Example #1
0
 public HuffmanNode CreateHuffmanTree()
 {
     while (!_IsEmpty())
     {
         var left  = DeQueue();
         var right = DeQueue();
         var cur   = new HuffmanNode()
         {
             weight = left.weight + right.weight, left = left, right = right
         };
         EnQueueByOrder(cur);
     }
     return(front.next.node);
 }
Example #2
0
        public int CodeHuffman(HuffmanNode root)
        {
            EnQueue(root);

            while (!IsEmpty())
            {
                var cur = DeQueue();
                if (cur.left == null && cur.right == null)
                {
                    Console.WriteLine($"{cur.ch}, {cur.weight}, {cur.code}");
                }
                else
                {
                    cur.left.code = cur.code + "0";
                    EnQueue(cur.left);

                    cur.right.code = cur.code;
                    EnQueue(cur.right);
                }
            }
            return(0);
        }
Example #3
0
        public int EnQueueByOrder(HuffmanNode node)
        {
            var n = front.next;
            var f = front;

            while (n != null)
            {
                if (n.node.weight < node.weight)
                {
                    n = n.next;
                    f = n.next;
                }
                else
                {
                    break;
                }
            }

            if (n != null)
            {
                var temp = new ListNode()
                {
                    node = node
                };
                f.next = temp;
                rear   = temp;
                return(0);
            }

            var temp1 = new ListNode()
            {
                node = node
            };

            f.next     = temp1;
            temp1.next = n;
            return(0);
        }