Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. public class HeapQueue{
  2.  
  3. private final Integer[] items;
  4. private int index = 0;
  5. private int _capacity;
  6.  
  7. public HeapQueue(int capacity) {
  8. items = new Integer[capacity];
  9. _capacity = capacity;
  10. }
  11.  
  12. public void enqueue(Integer item) {
  13. items[index] = item;
  14. int currentPos = index;
  15.  
  16. while (items[currentPos].compareTo(items[(currentPos - 1) / 2]) > 0)
  17. {
  18. Integer tmpData = items[currentPos];
  19. items[currentPos] = items[(currentPos - 1) / 2];
  20. items[(currentPos - 1) / 2] = tmpData;
  21. currentPos = (currentPos - 1) / 2;
  22. }
  23. index++;
  24. }
  25.  
  26. public Integer dequeue() {
  27. if(index == 0)
  28. throw new IllegalArgumentException("Cannot dequeue from an empty queue");
  29.  
  30. Integer passangerToReturn = items[0];
  31.  
  32. items[0] = items[index-1];
  33. items[index-1] = null;
  34. int currentPos = 0;
  35. boolean foundRightPosition = false;
  36.  
  37. while (!foundRightPosition && items[currentPos] != null)
  38. {
  39. int leftPos = (currentPos * 2) + 1;
  40. int rightPos = (currentPos+1) * 2;
  41. //Get left value
  42. Integer left;
  43. if(leftPos < _capacity)
  44. left = items[leftPos];
  45. else
  46. left = null;
  47. //Get right value
  48. Integer right;
  49. if(rightPos < _capacity)
  50. right= items[rightPos];
  51. else
  52. right = null;
  53.  
  54. if(left != null && left.compareTo(right)>=0 && items[currentPos].compareTo(left) < 0)
  55. {
  56. Integer tmp = left;
  57. items[leftPos] = items[currentPos];
  58. items[currentPos] = tmp;
  59. currentPos = leftPos;
  60. }
  61. else if(right!=null && right.compareTo(left)>=0 && items[currentPos].compareTo(right)<0)
  62. {
  63. Integer tmp = right;
  64. items[rightPos] = items[currentPos];
  65. items[currentPos] = tmp;
  66. currentPos = rightPos;
  67. }
  68. else
  69. {
  70. foundRightPosition = true;
  71. }
  72. }
  73.  
  74. index--;
  75. return passangerToReturn;
  76. }
  77.  
  78.  
  79. public void printQueue()
  80. {
  81. for(Integer p : items)
  82. if(p!=null)
  83. System.out.println(p);
  84. System.out.println("\n");
  85. }
  86.  
  87. public static void main(String[] args){
  88. HeapQueue hq = new HeapQueue(10);
  89. hq.printQueue();
  90.  
  91. hq.enqueue(5);
  92. hq.enqueue(8);
  93. hq.enqueue(2);
  94. hq.enqueue(3);
  95. hq.enqueue(7);
  96. hq.enqueue(4);
  97. hq.enqueue(9);
  98. hq.enqueue(1);
  99. hq.printQueue();
  100.  
  101. System.out.println(hq.dequeue());
  102. System.out.println(hq.dequeue());
  103. System.out.println(hq.dequeue());
  104. System.out.println(hq.dequeue());
  105. System.out.println(hq.dequeue());
  106. System.out.println(hq.dequeue());
  107. System.out.println(hq.dequeue());
  108. System.out.println(hq.dequeue());
  109.  
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement