public static Node Build_Huffman(List <Node> Component) { Priority_Queue.Build_MinHeap(Component); int C_Heap = Component.Count(); int n = Component.Count() - 1; for (int i = 0; i < n; i++) { Node temp = new Node(0, 0); Node right = Priority_Queue.Extract_HeapMin(Component, ref C_Heap); Node left = Priority_Queue.Extract_HeapMin(Component, ref C_Heap); temp.Frequency = (left.Frequency + right.Frequency); temp.Left = left; temp.Right = right; Priority_Queue.Min_Heap_Insert(Component, temp, ref C_Heap); } return(Priority_Queue.Extract_HeapMin(Component, ref C_Heap)); }
private void button1_Click(object sender, EventArgs e) { Stopwatch sp = new Stopwatch(); sp.Start(); long[] Red_array = new long[256]; long[] Green_array = new long[256]; long[] Blue_array = new long[256]; Priority_Queue Red_priority = new Priority_Queue(); Priority_Queue Green_priority = new Priority_Queue(); Priority_Queue Blue_priority = new Priority_Queue(); Red_Huffman = new HuffmanTree(); Green_Huffman = new HuffmanTree(); Blue_Huffman = new HuffmanTree(); List <HuffmanNode> Red_Nodes = new List <HuffmanNode>(); List <HuffmanNode> Green_Nodes = new List <HuffmanNode>(); List <HuffmanNode> Blue_Nodes = new List <HuffmanNode>(); Red_Root = new HuffmanNode(); Green_Root = new HuffmanNode(); Blue_Root = new HuffmanNode(); int Height = ImageOperations.GetHeight(ImageMatrix); int width = ImageOperations.GetWidth(ImageMatrix); for (int h = 0; h < Height; h++) { for (int w = 0; w < width; w++) { Red_array[ImageMatrix[h, w].red]++; Green_array[ImageMatrix[h, w].green]++; Blue_array[ImageMatrix[h, w].blue]++; } } for (int k = 0; k < 256; k++) { if (Red_array[k] > 0) { Red_Nodes.Add(new HuffmanNode { value = Convert.ToByte(k), freq = Red_array[k], CoLoR_CoDe = "" }); } if (Green_array[k] > 0) { Green_Nodes.Add(new HuffmanNode { value = Convert.ToByte(k), freq = Green_array[k], CoLoR_CoDe = "" }); } if (Blue_array[k] > 0) { Blue_Nodes.Add(new HuffmanNode { value = Convert.ToByte(k), freq = Blue_array[k], CoLoR_CoDe = "" }); } } Red_priority.Build_Min_Heap(ref Red_Nodes); Green_priority.Build_Min_Heap(ref Green_Nodes); Blue_priority.Build_Min_Heap(ref Blue_Nodes); Red_Root = Red_Huffman.BuildHuffmanTree(ref Red_Nodes); Green_Root = Green_Huffman.BuildHuffmanTree(ref Green_Nodes); Blue_Root = Blue_Huffman.BuildHuffmanTree(ref Blue_Nodes); FileStream Trunc_Red_File = new FileStream("Red_Info.txt", FileMode.Truncate); Trunc_Red_File.Close(); FileStream Red_File = new FileStream("Red_Info.txt", FileMode.Append); StreamWriter Red_Writer = new StreamWriter(Red_File); Red_Writer.WriteLine("--R--"); Red_Writer.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); Red_Writer.Close(); Red_File.Close(); Red_Huffman.PrintCodes(Red_Root, "", "Red_Info.txt"); FileStream Trunc_Green_File = new FileStream("Green_Info.txt", FileMode.Truncate); Trunc_Green_File.Close(); FileStream Green_File = new FileStream("Green_Info.txt", FileMode.Append); StreamWriter Green_Writer = new StreamWriter(Green_File); Green_Writer.WriteLine("--G--"); Green_Writer.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); Green_Writer.Close(); Green_File.Close(); Green_Huffman.PrintCodes(Green_Root, "", "Green_Info.txt"); FileStream Trunc_Blue_File = new FileStream("Blue_Info.txt", FileMode.Truncate); Trunc_Blue_File.Close(); FileStream Blue_File = new FileStream("Blue_Info.txt", FileMode.Append); StreamWriter Blue_Writer = new StreamWriter(Blue_File); Blue_Writer.WriteLine("--B--"); Blue_Writer.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); Blue_Writer.Close(); Blue_File.Close(); Blue_Huffman.PrintCodes(Blue_Root, "", "Blue_Info.txt"); FileStream truncate_total_info = new FileStream("Total_Info.dat", FileMode.Truncate); truncate_total_info.Close(); sp.Stop(); lbl_construct_tree.Text = sp.Elapsed.ToString(); MessageBox.Show("Constructed !"); }