|
| 1 | +package heap; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by dheeraj on 7/8/2016. |
| 7 | + */ |
| 8 | +public class KMostFrequent { |
| 9 | + |
| 10 | + static Data[] datas = new Data[5]; |
| 11 | + static int size; |
| 12 | + |
| 13 | + private static void addToHeap(Data data) { |
| 14 | + if (size < datas.length) { |
| 15 | + datas[size] = data; |
| 16 | + data.heapIndex = size; |
| 17 | + size++; |
| 18 | + if (size == datas.length) { |
| 19 | + buildHeap(); |
| 20 | + } |
| 21 | + } else if (datas[0].count < data.count) { |
| 22 | + datas[0].heapIndex = -1; |
| 23 | + datas[0] = data; |
| 24 | + datas[0].heapIndex = 0; |
| 25 | + heapify(0); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private static void buildHeap() { |
| 30 | + int p = getParent(datas.length - 1); |
| 31 | + for (int k = p; p >= 0; p--) { |
| 32 | + heapify(k); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static void heapify(int k) { |
| 37 | + int min = k; |
| 38 | + int l = getLeft(k); |
| 39 | + int r = getRight(k); |
| 40 | + |
| 41 | + if (l <= datas.length - 1 && datas[l].count < datas[k].count) { |
| 42 | + min = l; |
| 43 | + } |
| 44 | + |
| 45 | + if (r <= datas.length - 1 && datas[r].count < datas[min].count) { |
| 46 | + min = r; |
| 47 | + } |
| 48 | + |
| 49 | + if (min != k) { |
| 50 | + Data data = datas[min]; |
| 51 | + datas[k] = data; |
| 52 | + datas[k].heapIndex = k; |
| 53 | + datas[min] = datas[k]; |
| 54 | + datas[min].heapIndex = min; |
| 55 | + heapify(min); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + private static int getParent(int child) { |
| 61 | + return (child - 1) / 2; |
| 62 | + } |
| 63 | + |
| 64 | + private static int getRight(int p) { |
| 65 | + return 2 * p + 2; |
| 66 | + } |
| 67 | + |
| 68 | + private static int getLeft(int p) { |
| 69 | + return 2 * p + 1; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + static class Data { |
| 74 | + int count; |
| 75 | + String word; |
| 76 | + int heapIndex; |
| 77 | + |
| 78 | + public Data(String word, int count, int heapIndex) { |
| 79 | + this.count = count; |
| 80 | + this.word = word; |
| 81 | + this.heapIndex = -1; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static HashMap<String, Data> wordCountMap = new HashMap<String, Data>(); |
| 86 | + |
| 87 | + private static void addWord(String word) { |
| 88 | + Data data = wordCountMap.get(word); |
| 89 | + if (data == null) { |
| 90 | + data = new Data(word, 1, -1); |
| 91 | + wordCountMap.put(word, data); |
| 92 | + addToHeap(data); |
| 93 | + } else { |
| 94 | + data.count++; |
| 95 | + if (data.heapIndex == -1) { |
| 96 | + addToHeap(data); |
| 97 | + } else { |
| 98 | + heapify(data.heapIndex); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static void main(String[] strings) { |
| 104 | + String s = "Welcome to the world of Geeks " + |
| 105 | + "This portal has been created to provide well written well thought and well explained " + |
| 106 | + "solutions for selected questions If you like Geeks for Geeks and would like to contribute " + |
| 107 | + "here is your chance You can write article and mail your article to contribute at " + |
| 108 | + "geeksforgeeks org See your article appearing on the Geeks for Geeks main page and help" + |
| 109 | + "thousands of other Geeks"; |
| 110 | + String[] strings1 = s.split(" "); |
| 111 | + for (String s1 : strings1) { |
| 112 | + addWord(s1); |
| 113 | + } |
| 114 | + System.out.println(); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments