public BinaryTreeNode <CharFreq> Build(List <CharFreq> charFreq, int n) { PriorityQueue Q = new PriorityQueue(); for (int i = 0; i < n; i++) { BinaryTreeNode <CharFreq> z = new BinaryTreeNode <CharFreq>(charFreq[i]); Q.insert(z); } Q.buildHeap(); for (int i = 0; i < n - 1; i++) { BinaryTreeNode <CharFreq> x = Q.extractMin(); BinaryTreeNode <CharFreq> y = Q.extractMin(); CharFreq chFreq = new CharFreq(); chFreq.ch = (char)((int)x.Value.ch + (int)y.Value.ch); chFreq.freq = x.Value.freq + y.Value.freq; BinaryTreeNode <CharFreq> z = new BinaryTreeNode <CharFreq>(chFreq); z.Left = x; z.Right = y; Q.insert(z); } return(Q.extractMin()); }
private void preobr_Click(object sender, EventArgs e) { string s = tb1.Text; int n = s.Length; List <CharFreq> list = new List <CharFreq>(); tb2.Text = string.Empty; for (int i = 0; i < n; i++) { bool found = false; char c = s[i]; CharFreq cf = new CharFreq(); for (int j = 0; !found && j < list.Count; j++) { if (c == list[j].ch) { found = true; cf.ch = c; cf.freq = 1 + list[j].freq; list.RemoveAt(j); list.Add(cf); } } if (!found) { cf.ch = c; cf.freq = 1; list.Add(cf); } } HuffmanTree ht = new HuffmanTree(); BinaryTreeNode <CharFreq> root = ht.Build(list, list.Count); InorderTraversal(root); tb2.Text += "\r\n characters = " + n.ToString() + "\r\n"; tb2.Text += " leaf nodes = " + leafNodes.ToString() + "\r\n"; tb2.Text += "\r\n % compressed = " + (100.0 - 100.0 * ((double)leafNodes) / n).ToString("F2") + "\r\n"; }
private void InorderTraversal(BinaryTreeNode <CharFreq> node) { if (node != null) { InorderTraversal(node.Left); CharFreq cf = node.Value; int ord = (int)cf.ch; if (node.Left == null && node.Right == null) { leafNodes++; tb2.Text += "'" + new string(cf.ch, 1) + "' "; tb2.Text += node.Value.freq.ToString() + "\r\n"; } InorderTraversal(node.Right); } }