|
| 1 | + |
| 2 | +import java.util.PriorityQueue; |
| 3 | +import java.util.Scanner; |
| 4 | +import java.util.Comparator; |
| 5 | + |
| 6 | +// node class is the basic structure |
| 7 | +// of each node present in the huffman - tree. |
| 8 | +class HuffmanNode { |
| 9 | + |
| 10 | + int data; |
| 11 | + char c; |
| 12 | + |
| 13 | + HuffmanNode left; |
| 14 | + HuffmanNode right; |
| 15 | +} |
| 16 | + |
| 17 | +// comparator class helps to compare the node |
| 18 | +// on the basis of one of its attribute. |
| 19 | +// Here we will be compared |
| 20 | +// on the basis of data values of the nodes. |
| 21 | +class MyComparator implements Comparator<HuffmanNode> { |
| 22 | + public int compare(HuffmanNode x, HuffmanNode y) |
| 23 | + { |
| 24 | + |
| 25 | + return x.data - y.data; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +public class Huffman { |
| 30 | + |
| 31 | + // recursive function to print the |
| 32 | + // huffman-code through the tree traversal. |
| 33 | + // Here s is the huffman - code generated. |
| 34 | + public static void printCode(HuffmanNode root, String s) |
| 35 | + { |
| 36 | + |
| 37 | + // base case; if the left and right are null |
| 38 | + // then its a leaf node and we print |
| 39 | + // the code s generated by traversing the tree. |
| 40 | + if (root.left |
| 41 | + == null |
| 42 | + && root.right |
| 43 | + == null |
| 44 | + && Character.isLetter(root.c)) { |
| 45 | + |
| 46 | + // c is the character in the node |
| 47 | + System.out.println(root.c + ":" + s); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // if we go to left then add "0" to the code. |
| 53 | + // if we go to the right add"1" to the code. |
| 54 | + |
| 55 | + // recursive calls for left and |
| 56 | + // right sub-tree of the generated tree. |
| 57 | + printCode(root.left, s + "0"); |
| 58 | + printCode(root.right, s + "1"); |
| 59 | + } |
| 60 | + |
| 61 | + // main function |
| 62 | + public static void main(String[] args) |
| 63 | + { |
| 64 | + |
| 65 | + Scanner s = new Scanner(System.in); |
| 66 | + |
| 67 | + // number of characters. |
| 68 | + int n = 6; |
| 69 | + char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 70 | + int[] charfreq = { 5, 9, 12, 13, 16, 45 }; |
| 71 | + |
| 72 | + // creating a priority queue q. |
| 73 | + // makes a min-priority queue(min-heap). |
| 74 | + PriorityQueue<HuffmanNode> q |
| 75 | + = new PriorityQueue<HuffmanNode>(n, new MyComparator()); |
| 76 | + |
| 77 | + for (int i = 0; i < n; i++) { |
| 78 | + |
| 79 | + // creating a huffman node object |
| 80 | + // and adding it to the priority-queue. |
| 81 | + HuffmanNode hn = new HuffmanNode(); |
| 82 | + |
| 83 | + hn.c = charArray[i]; |
| 84 | + hn.data = charfreq[i]; |
| 85 | + |
| 86 | + hn.left = null; |
| 87 | + hn.right = null; |
| 88 | + |
| 89 | + // add functions adds |
| 90 | + // the huffman node to the queue. |
| 91 | + q.add(hn); |
| 92 | + } |
| 93 | + |
| 94 | + // create a root node |
| 95 | + HuffmanNode root = null; |
| 96 | + |
| 97 | + // Here we will extract the two minimum value |
| 98 | + // from the heap each time until |
| 99 | + // its size reduces to 1, extract until |
| 100 | + // all the nodes are extracted. |
| 101 | + while (q.size() > 1) { |
| 102 | + |
| 103 | + // first min extract. |
| 104 | + HuffmanNode x = q.peek(); |
| 105 | + q.poll(); |
| 106 | + |
| 107 | + // second min extarct. |
| 108 | + HuffmanNode y = q.peek(); |
| 109 | + q.poll(); |
| 110 | + |
| 111 | + // new node f which is equal |
| 112 | + HuffmanNode f = new HuffmanNode(); |
| 113 | + |
| 114 | + // to the sum of the frequency of the two nodes |
| 115 | + // assigning values to the f node. |
| 116 | + f.data = x.data + y.data; |
| 117 | + f.c = '-'; |
| 118 | + |
| 119 | + // first extracted node as left child. |
| 120 | + f.left = x; |
| 121 | + |
| 122 | + // second extracted node as the right child. |
| 123 | + f.right = y; |
| 124 | + |
| 125 | + // marking the f node as the root node. |
| 126 | + root = f; |
| 127 | + |
| 128 | + // add this node to the priority-queue. |
| 129 | + q.add(f); |
| 130 | + } |
| 131 | + |
| 132 | + // print the codes by traversing the tree |
| 133 | + printCode(root, ""); |
| 134 | + } |
| 135 | +} |
0 commit comments