Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class MaxHeap {
  4. private ArrayList<Integer> data_;
  5.  
  6. public MaxHeap() {
  7. data_ = new ArrayList<Integer>();
  8. }
  9.  
  10. // Given an index, return the parent index
  11. private int parent(int index) {
  12. return (index - 1) / 2;
  13. }
  14. private int lchild(int index) {
  15. return (2* index + 1);
  16. }
  17. private int rchild(int index) {
  18. return (2* index + 2);
  19. }
  20.  
  21. public void insert(int value) {
  22. this.data_.add(value);
  23. int index = data_.size() - 1;
  24. while (this.data_.get(index) > this.data_.get(parent(index))) {
  25. Collections.swap(this.data_, index, parent(index));
  26. index = parent(index);
  27. }
  28. }
  29.  
  30. //////////////////////////////
  31. //ATTEMPT #1
  32.  
  33. /*
  34. public void remove(int index) {
  35. //swap node to remove with the last node in the heapremove the last nodebubble down (“heapify”) to correct position
  36. int last = data_.size() - 1;
  37. if(index == last){
  38. data_.remove(last);
  39. }
  40. else{
  41. //swaps index wanted to be deleted with last element and then deletes last element
  42. Collections.swap(this.data_, index, last);
  43. data_.remove(last);
  44. boolean finding = true;
  45.  
  46. while(!finding){
  47. int max = 0;
  48. if (data_.size()-1 >= data_.get(lchild(index)) && data_.size()-1 >= data_.get(rchild(index))){
  49.  
  50. if(data_.get(lchild(index)) >= data_.get(rchild(index))){
  51. max = data_.get(lchild(index));
  52. Collections.swap(this.data_, index, lchild(index));
  53. index = lchild(index);
  54. }
  55. else if(data_.get(rchild(index)) > data_.get(lchild(index))){
  56. max = data_.get(rchild(index));
  57. Collections.swap(this.data_, index, rchild(index));
  58. index = rchild(index);
  59. }
  60. }
  61. else if(data_.size()-1 >= data_.get(lchild(index)) && data_.size()-1 < data_.get(rchild(index))){
  62. max = data_.get(lchild(index));
  63. Collections.swap(this.data_, index, lchild(index));
  64. index = lchild(index);
  65. }
  66. else if(data_.size()-1 < data_.get(lchild(index)) && data_.size()-1 >= data_.get(rchild(index))){
  67. max = data_.get(rchild(index));
  68. Collections.swap(this.data_, index, rchild(index));
  69. index = rchild(index);
  70. }
  71. else{
  72. finding = false;
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. */
  79.  
  80. /////////////////////////////
  81. //ATTEMPT #2
  82.  
  83. /*
  84. public void remove(int index) {
  85. //swap node to remove with the last node in the heapremove the last nodebubble down (“heapify”) to correct position
  86. int last = data_.size() - 1;
  87. if(index == last){
  88. data_.remove(last);
  89. }
  90. else{
  91. //swaps index wanted to be deleted with last element and then deletes last element
  92. Collections.swap(this.data_, index, last);
  93. data_.remove(last);
  94. trickleDown(index);
  95. }
  96. }
  97. public void trickleDown(int index){
  98. // TASK: Check if leaf, if that's the case do nothing
  99. if (data_.size()-1 < data_.get(lchild(index)) && data_.size()-1 < data_.get(rchild(index)))
  100. return;
  101.  
  102. // TASK: Initialize children variables
  103. int leftChildIndex = index * 2;
  104. int rightChildIndex = (index * 2) + 1;
  105. int minChildIndex;
  106.  
  107. // TASK: Check if rightChildren doesn't exist, in that case
  108. // the minChildIndex is the left, otherwise, find the min children
  109. if (rightChildIndex > data_.size()-1) {
  110. minChildIndex = leftChildIndex;
  111. }
  112. else {
  113. minChildIndex = (this.data_.get(leftChildIndex) < this.data_.get(rightChildIndex)) ? leftChildIndex : rightChildIndex;
  114. }
  115.  
  116. // TASK: stop in case the parent is already smaller than the children
  117. if (this.data_.get(index) < this.data_.get(minChildIndex)) return;
  118.  
  119. Collections.swap(this.data_, index, minChildIndex);
  120.  
  121. // TASK: apply the same algorithm recursively
  122. trickleDown(minChildIndex);
  123. }
  124. */
  125. ///////////////////////
  126.  
  127.  
  128. public int extractMax() {
  129. throw new RuntimeException("Not implemented!");
  130. }
  131.  
  132. public int getMax() {
  133. throw new RuntimeException("Not implemented!");
  134. }
  135.  
  136. public void print() {
  137. for (Integer i : data_) {
  138. System.out.print(i + " ");
  139. }
  140. System.out.println();
  141. }
  142.  
  143. public boolean isEmpty() {
  144. return data_.isEmpty();
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement