Guest User

Untitled

a guest
Mar 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. package alg;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5.  
  6. public class BinaryHeap<T extends Comparable<? super T>> {
  7. private Object[] m_itemArray;
  8. private int m_length;
  9. private int m_capacity;
  10.  
  11. public BinaryHeap(Collection<? extends T> items) {
  12. m_itemArray = items.toArray();
  13. m_length = m_itemArray.length;
  14. m_capacity = m_itemArray.length;
  15. heapify();
  16. }
  17.  
  18. private void heapify() {
  19. // Heapify from the bottom up by sifting down the nodes at each level.
  20. int i = (m_length - 1) / 2;
  21. for (; i >= 0; --i) {
  22. siftDown(i, getAt(i));
  23. }
  24. }
  25.  
  26. public boolean empty() {
  27. return m_length == 0;
  28. }
  29.  
  30. public int size() {
  31. return m_length;
  32. }
  33.  
  34. public boolean add(T element) {
  35. ensureCapacity(m_length + 1);
  36.  
  37. // To insert into a heap, push it to the end of the array
  38. // and sift up.
  39. siftUp(m_length++, element);
  40.  
  41. return true;
  42. }
  43.  
  44. public T peek() {
  45. return getAt(0);
  46. }
  47.  
  48. public T pop() {
  49. T item = peek();
  50. siftDown(0, getAt(--m_length));
  51. return item;
  52. }
  53.  
  54. private void siftUp(int i, T ele) {
  55. // Compare node i to its parent. If it's less than parent, then swap and
  56. // recurse up.
  57. int parent = (i - 1) / 2;
  58. if (0 < i && ele.compareTo(getAt(parent)) < 0) {
  59. m_itemArray[i] = m_itemArray[parent];
  60. siftUp(parent, ele);
  61. } else {
  62. m_itemArray[i] = ele;
  63. }
  64. }
  65.  
  66. private T getAt(int i) {
  67. @SuppressWarnings("unchecked")
  68. T t = (T) m_itemArray[i];
  69. return t;
  70. }
  71.  
  72. @SuppressWarnings("unchecked")
  73. private void siftDown(int i, T ele) {
  74. // Compare node i to its children. If the minimum is less than the node,
  75. // then swap and recurse down.
  76. int j = i;
  77. T cmp = ele;
  78.  
  79. int child = 2 * i + 1;
  80. if (child < m_length && getAt(child).compareTo(cmp) < 0) {
  81. j = child;
  82. cmp = (T) m_itemArray[child];
  83. }
  84. child++;
  85. if (child < m_length && getAt(child).compareTo(cmp) < 0) {
  86. j = child;
  87. cmp = (T) m_itemArray[child];
  88. }
  89.  
  90. m_itemArray[i] = cmp;
  91. if (j != i) {
  92. siftDown(j, ele);
  93. }
  94. }
  95.  
  96. private void ensureCapacity(int cap) {
  97. if (cap > m_capacity) {
  98. int nextCap = Math.max((m_capacity / 2) * 3, cap);
  99. Object[] nextItems = new Object[nextCap];
  100. System.arraycopy(m_itemArray, 0, nextItems, 0, m_length);
  101.  
  102. m_itemArray = nextItems;
  103. m_capacity = nextCap;
  104. }
  105. }
  106.  
  107. public static void main(String[] args) {
  108. BinaryHeap<Integer> heap = new BinaryHeap<>(Arrays.asList((Integer) 9, 2, 7, 6, 4, 1, 8, 5, 3));
  109.  
  110. heap.add(12);
  111. heap.add(-1);
  112. heap.add(10);
  113.  
  114. while (!heap.empty()) {
  115. System.out.println(heap.pop());
  116. }
  117. }
  118. }
Add Comment
Please, Sign In to add comment