Ejemplo n.º 1
0
        /// <summary>
        /// Merges two huffman tree nodes and creates a new non-leaf node which quantity
        /// is equal to sum of quantities of given nodes.
        /// </summary>
        /// <param name="first">first node to merge</param>
        /// <param name="second">second node to merge</param>
        /// <returns>a node representing a parent of given nodes</returns>
        private HuffmanTreeNode <T> Merge(HuffmanTreeNode <T> left, HuffmanTreeNode <T> right)
        {
            var parent = new HuffmanTreeNode <T>(
                value: left.Value,
                quantity: left.Quantity + right.Quantity,
                subTreeDepth: Math.Max(left.SubTreeDepth, right.SubTreeDepth) + 1
                )
            {
                LeftChild  = left,
                RightChild = right
            };

            left.Parent = right.Parent = parent;
            return(parent);
        }
Ejemplo n.º 2
0
        public int Compare(HuffmanTreeNode <T> x, HuffmanTreeNode <T> y)
        {
            if (x.Quantity != y.Quantity)
            {
                return(x.Quantity - y.Quantity);
            }
            if (x.SubTreeDepth != y.SubTreeDepth)
            {
                return(x.SubTreeDepth - y.SubTreeDepth);
            }
            var valueComparerResult = valueComparer.Compare(x.Value, y.Value);

            if (valueComparerResult == 0)
            {
                throw new Exception("Two huffman nodes cannot be equal to each other");
            }
            return(valueComparerResult);
        }