示例#1
0
        private static HuffmanLinkedNode BuildTree(HuffmanLinkedNode tail)
        {
            Contract.Requires(tail != null);
            Contract.Ensures(Contract.Result <HuffmanLinkedNode>() != null);

            while (tail != null)
            {
                var child0 = tail;
                var child1 = tail.Prev;

                if (child1 == null)
                {
                    break;
                }

                var parent = new HuffmanLinkedNode(0, child0.Weight + child1.Weight);

                parent.Child0 = child0;
                child0.Parent = parent;
                child1.Parent = parent;

                tail.Insert(parent);
                tail = tail.Prev.Prev;
            }

            Contract.Assume(tail != null);
            return(tail);
        }
示例#2
0
        private static HuffmanLinkedNode BuildList(IList <byte> primeData)
        {
            Contract.Requires(primeData != null);
            Contract.Ensures(Contract.Result <HuffmanLinkedNode>() != null);

            var root = new HuffmanLinkedNode(byte.MaxValue, 1);

            root = root.Insert(new HuffmanLinkedNode(byte.MaxValue + 1, 1));

            for (var i = 0; i < primeData.Count; i++)
            {
                if (primeData[i] != 0)
                {
                    root = root.Insert(new HuffmanLinkedNode(i, primeData[i]));
                }
            }

            return(root);
        }