Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package trials;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import java.util.Stack;
  6.  
  7. public class PQWithSearch {
  8.  
  9. private Phrase[] pq;
  10. private HashMap<Phrase, Integer> positionsMap = new HashMap<Phrase, Integer>();
  11. public int size = 0;
  12.  
  13. public PQWithSearch(int maxLength){
  14. pq = new Phrase[maxLength+1];
  15. }
  16.  
  17. public void stringInsert(String text){
  18. System.out.println("Trying to insert text: " + text);
  19. Phrase ph = new Phrase(text);
  20. if (positionsMap.containsKey(ph)){
  21. System.out.println("found it: " + ph);
  22. int phraseIndex = positionsMap.get(ph);
  23. pq[phraseIndex].incrementCount();
  24. } else {
  25. System.out.println("Didn't find it, adding it");
  26. insert(ph);
  27. }
  28.  
  29. }
  30. public void insert(Phrase k){
  31. size++;
  32. assign(size, k);
  33. swim(size);
  34. }
  35.  
  36. public Phrase delMin(){
  37. Phrase min = pq[1];
  38. size--;
  39. exch(1, size);
  40. assign(size+1, null);
  41. sink(1);
  42. removeFromPositionsMap(min);
  43. return min;
  44. }
  45.  
  46. public boolean isEmpty(){
  47. return size == 0;
  48. }
  49.  
  50. private void assign(int index, Phrase value){
  51.  
  52. pq[index] = value;
  53. if(value != null){
  54. positionsMap.put(value, index);
  55. }
  56. }
  57. private void removeFromPositionsMap(Phrase phrase){
  58. positionsMap.remove(phrase);
  59. }
  60.  
  61. private void swim(int k){
  62. while(k>1 && greater(k/2, k)){
  63. exch(k,k/2);
  64. k = k/2;
  65. }
  66. }
  67. private void sink(int k){
  68. while(2*k <= size){
  69. int j = 2 *k;
  70. if (j < size && greater(j,j+1)){
  71. j++;
  72. }
  73. if (greater(k,j)){
  74. exch(k,j);
  75. k = j;
  76. } else {
  77. break;
  78. }
  79. }
  80. }
  81.  
  82. private boolean less(int i, int j){
  83. if (pq[i].compareTo(pq[j]) < 0){
  84. return true;
  85. }
  86. return false;
  87. }
  88.  
  89.  
  90. private boolean greater(int i, int j){
  91. if (pq[i].compareTo(pq[j]) > 0){
  92. return true;
  93. }
  94. return false;
  95. }
  96.  
  97. private void exch(int i, int j) {
  98. Phrase t = pq[i];
  99. assign(i, pq[j]);
  100. assign(j, t);
  101. }
  102.  
  103. public static void main(String[] args) {
  104.  
  105. int maxSize = 5;
  106. PQWithSearch pq = new PQWithSearch(maxSize+1);
  107.  
  108. String [] fileContents = new String[]{"a","a","c","d","e","f","g","a","b","b","c"};
  109.  
  110. for (String text : fileContents){
  111. pq.stringInsert(text);
  112. if (pq.size > maxSize){
  113. pq.delMin();
  114. }
  115. }
  116.  
  117. Stack<Phrase> stack = new Stack<Phrase>();
  118. while (!pq.isEmpty()) stack.push(pq.delMin());
  119. for (Phrase t : stack)
  120. System.out.println(t);
  121. }
  122.  
  123. }
  124.  
  125. class Phrase implements Comparable<Phrase>{
  126.  
  127. private String text;
  128. private int count = 1;
  129. public Phrase(String text){
  130. this.text = text;
  131. }
  132. @Override
  133. public int compareTo(Phrase o) {
  134. if (this.count < o.count){
  135. return -1;
  136. }
  137. else if (this.count > o.count){
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. public int hashCode(){
  143. return this.text.hashCode();
  144. }
  145.  
  146. public boolean equals(Object other){
  147. Phrase otherPhrase = (Phrase) other;
  148. boolean isEqual = this.text.equals(otherPhrase.text);
  149. return isEqual;
  150. }
  151. public void incrementCount(){
  152. this.count += 1;
  153. }
  154.  
  155. public String toString(){
  156. return this.text + ": " + this.count;
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement