示例#1
0
        private static HuffmanTreeNode CombineNextNodes(Queue <HuffmanTreeNode> nodesToProcess, Queue <HuffmanTreeNode> treesToProcess)
        {
            HuffmanTreeNode node1 = GetNextNode(nodesToProcess, treesToProcess);
            HuffmanTreeNode node2 = GetNextNode(nodesToProcess, treesToProcess);

            return(new HuffmanTreeNode {
                Left = node1,
                Right = node2,
                TreeWeight = (node1.Weight ?? node1.TreeWeight) + (node2.Weight ?? node2.TreeWeight)
            });
        }
示例#2
0
        public static HuffmanTreeNode CalculateHuffmanCodes(List <int> data)
        {
            Queue <HuffmanTreeNode> nodesToProcess     = new Queue <HuffmanTreeNode>();
            IOrderedEnumerable <HuffmanTreeNode> nodes = data.Select((weight, index) => new HuffmanTreeNode
            {
                Id     = index,
                Weight = weight
            }).OrderBy(n => n.Weight);

            foreach (HuffmanTreeNode node in nodes)
            {
                nodesToProcess.Enqueue(node);
            }

            Queue <HuffmanTreeNode> treesToProcess = new Queue <HuffmanTreeNode>();

            while (nodesToProcess.Count > 0 || treesToProcess.Count > 1)
            {
                HuffmanTreeNode combinedNode = CombineNextNodes(nodesToProcess, treesToProcess);
                treesToProcess.Enqueue(combinedNode);
            }
            return(treesToProcess.Dequeue());
        }
示例#3
0
        private static int DepthEvaluator(HuffmanTreeNode tree, Func <int, int, int> tiebreaker)
        {
            if (tree == null)
            {
                return(0);
            }

            if (tree.Left == null && tree.Right == null)
            {
                return(1);
            }

            if (tree.Left == null)
            {
                return(DepthEvaluator(tree.Right, tiebreaker) + 1);
            }

            if (tree.Right == null)
            {
                return(DepthEvaluator(tree.Left, tiebreaker) + 1);
            }

            return(tiebreaker(DepthEvaluator(tree.Right, tiebreaker), DepthEvaluator(tree.Left, tiebreaker)) + 1);
        }
示例#4
0
 private static int MaxDepth(HuffmanTreeNode tree)
 {
     return(DepthEvaluator(tree, Math.Max));
 }
示例#5
0
 public static (int, int) GetTreeDepths(HuffmanTreeNode tree)
 {
     return(MinDepth(tree), MaxDepth(tree));
 }