|
| 1 | +package hw05; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class HuffmanCode { |
| 11 | + private Node[] arr; |
| 12 | + private int heapSize; |
| 13 | + private String str; |
| 14 | + private HashMap<Character, Integer> map; |
| 15 | + private Node huffmanTree; |
| 16 | + |
| 17 | + public HuffmanCode(String filePath){ |
| 18 | + this.str = this.readFile(filePath); |
| 19 | + createFrequencyMap(); |
| 20 | + this.arr = new Node[map.size() + 1]; |
| 21 | + mapToArr(); |
| 22 | + this.heapSize = arr.length - 1; |
| 23 | + doBuildMinHeap(); // 초기 MinHeap 구성 |
| 24 | + } |
| 25 | + |
| 26 | + public void huffmanCode(){ |
| 27 | + for(int i = this.heapSize ; i > 1 ; i--) { |
| 28 | + Node left = extractMin(this.arr); |
| 29 | + Node right = extractMin(this.arr); |
| 30 | + Node newNode = new Node(left.getFrequency() + right.getFrequency(), left, right); |
| 31 | + insert(newNode); |
| 32 | + } |
| 33 | + huffmanTree = this.arr[1]; |
| 34 | + } |
| 35 | + |
| 36 | + private void createFrequencyMap(){ |
| 37 | + this.map = new HashMap<Character, Integer>(); |
| 38 | + String str = this.str; |
| 39 | + for(int i = 0; i < str.length(); i++){ |
| 40 | + char character = str.charAt(i); |
| 41 | + if(this.map.containsKey(character)){ |
| 42 | + int frequency = this.map.get(character); |
| 43 | + this.map.put(character, ++frequency); |
| 44 | + }else{ |
| 45 | + this.map.put(character, 1); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void printHuffmanTree(){ |
| 51 | + recursionHuffmanTree(huffmanTree, ""); |
| 52 | + } |
| 53 | + |
| 54 | + private void recursionHuffmanTree(Node root, String code){ |
| 55 | + if(root != null){ |
| 56 | + if(root.getLeft() != null && root.getRight() != null){ |
| 57 | + recursionHuffmanTree(root.getLeft(), code + "0"); |
| 58 | + recursionHuffmanTree(root.getRight(), code + "1"); |
| 59 | + } |
| 60 | + else{ |
| 61 | + System.out.println(root.getAlphabet() + ", " + code); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void insert(Node x){ |
| 67 | + Node[] temp = new Node[this.heapSize + 2]; |
| 68 | + for(int i = 1 ; i <= this.heapSize; i++){ |
| 69 | + temp[i] = arr[i]; |
| 70 | + } |
| 71 | + temp[temp.length - 1] = x; |
| 72 | + this.arr = temp; |
| 73 | + this.heapSize = temp.length - 1; |
| 74 | + buildMinHeap(this.arr); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + private void mapToArr(){ |
| 80 | + int i = 1; |
| 81 | + for(HashMap.Entry<Character, Integer> elem : map.entrySet()){ |
| 82 | + arr[i] = new Node(elem.getKey(), elem.getValue()); |
| 83 | + i++; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private Node extractMin(Node[] arr){ |
| 88 | + Node minNode = arr[1]; |
| 89 | + swap(arr, heapSize, 1); |
| 90 | + this.heapSize--; |
| 91 | + doBuildMinHeap(); |
| 92 | + return minNode; |
| 93 | + } |
| 94 | + |
| 95 | + public void doBuildMinHeap(){ |
| 96 | + buildMinHeap(this.arr); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + private void buildMinHeap(Node[] arr){ |
| 101 | + for(int i = (this.heapSize) / 2 ; i > 0 ; i--){ |
| 102 | + minHeapify(arr, i); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void minHeapify(Node[] arr, int i){ |
| 107 | + int smallest; |
| 108 | + int leftChildIdx = getLeftChildIdx(i); |
| 109 | + int rightChildIdx = getRightChildIdx(i); |
| 110 | + if(this.heapSize >= leftChildIdx && arr[leftChildIdx].frequency < arr[i].frequency){ |
| 111 | + smallest = leftChildIdx; |
| 112 | + }else{ |
| 113 | + smallest = i; |
| 114 | + } |
| 115 | + if(this.heapSize >= rightChildIdx && arr[rightChildIdx].frequency < arr[smallest].frequency){ |
| 116 | + smallest = rightChildIdx; |
| 117 | + } |
| 118 | + if(smallest != i){ |
| 119 | + swap(arr, smallest, i); |
| 120 | + minHeapify(arr, smallest); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + private void swap(Node[] arr, int i, int j){ |
| 126 | + Node temp = arr[i]; |
| 127 | + arr[i] = arr[j]; |
| 128 | + arr[j] = temp; |
| 129 | + } |
| 130 | + |
| 131 | + private int getLeftChildIdx(int parentIdx){ |
| 132 | + return 2*parentIdx; |
| 133 | + } |
| 134 | + |
| 135 | + private int getRightChildIdx(int parentIdx){ |
| 136 | + return 2*parentIdx+1; |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + private String readFile(String filePath) { |
| 141 | + BufferedReader br = null; |
| 142 | + StringTokenizer str = null; |
| 143 | + StringBuilder sb = null; |
| 144 | + String line; |
| 145 | + |
| 146 | + try { |
| 147 | + br = new BufferedReader(new FileReader(filePath)); |
| 148 | + sb = new StringBuilder(); |
| 149 | + while ((line = br.readLine()) != null) { |
| 150 | + sb.append(line); |
| 151 | + } |
| 152 | + } catch (FileNotFoundException e) { |
| 153 | + e.printStackTrace(); |
| 154 | + } catch (IOException e) { |
| 155 | + e.printStackTrace(); |
| 156 | + } finally { |
| 157 | + if (br != null) { |
| 158 | + try { |
| 159 | + br.close(); |
| 160 | + } catch (IOException e) { |
| 161 | + e.printStackTrace(); |
| 162 | + } |
| 163 | + } |
| 164 | + return sb.toString(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + class Node{ |
| 169 | + private Node left, right; |
| 170 | + private char alphabet; |
| 171 | + private int frequency; |
| 172 | + |
| 173 | + public Node(char alphabet, int frequency){ |
| 174 | + this.alphabet = alphabet; |
| 175 | + this.frequency = frequency; |
| 176 | + this.left = null; |
| 177 | + this.right = null; |
| 178 | + } |
| 179 | + |
| 180 | + public Node(int frequency, Node left, Node right){ |
| 181 | + this.alphabet = ' '; |
| 182 | + this.frequency = frequency; |
| 183 | + this.left = left; |
| 184 | + this.right = right; |
| 185 | + } |
| 186 | + |
| 187 | + public Node(char alphabet, int frequency, Node left, Node right){ |
| 188 | + this.alphabet = alphabet; |
| 189 | + this.frequency = frequency; |
| 190 | + this.left = left; |
| 191 | + this.right = right; |
| 192 | + } |
| 193 | + |
| 194 | + public Node getLeft() { |
| 195 | + return left; |
| 196 | + } |
| 197 | + |
| 198 | + public Node getRight() { |
| 199 | + return right; |
| 200 | + } |
| 201 | + |
| 202 | + public char getAlphabet() { |
| 203 | + return alphabet; |
| 204 | + } |
| 205 | + |
| 206 | + public int getFrequency() { |
| 207 | + return frequency; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments