File tree Expand file tree Collapse file tree 2 files changed +5
-8
lines changed
src/main/java/com/rampatra/common Expand file tree Collapse file tree 2 files changed +5
-8
lines changed Original file line number Diff line number Diff line change 55import java .util .Arrays ;
66
77/**
8- * Created by IntelliJ IDEA.
9- * <p/>
108 * A HEAP is a specialized tree-based ABSTRACT DATA TYPE that satisfies the heap property:
119 * min-heap: All non-leaf elements are either smaller than or equal to their left and right child.
1210 * max-heap: All non-leaf elements are either greater than or equal to their left and right child.
1917 * Therefore, buildMaxHeap() would take O(n log n) time BUT IF OBSERVED CAREFULLY IT TAKES 0(N) TIME.
2018 * <p/>
2119 * Used in the HeapSort algorithm. Also can be used to implement a PriorityQueue.
20+ * <a href="@see http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap07.htm">Learn more</a>
2221 *
23- * @author: ramswaroop
24- * @date: 8/2/15
25- * @time: 11:57 AM
26- * @see: http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap07.htm
22+ * @author rampatra
23+ * @since 8/2/15
2724 */
2825public class MaxHeap {
2926
Original file line number Diff line number Diff line change 1414 * Each successor can be found in O(log n). The algorithm in minHeapify() takes O(log n) time
1515 * Therefore, buildMinHeap() would take O(n log n) time BUT IF OBSERVED CAREFULLY IT TAKES 0(N) TIME.
1616 * <p/>
17- * Used in the HeapSort algorithm. Also can be used to implement a PriorityQueue.
17+ * Used in the HeapSort algorithm. Also can be used to implement a PriorityQueue.
1818 * <a href="@see http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap07.htm">Learn more</a>
1919 *
2020 * @author rampatra
@@ -68,7 +68,7 @@ public void buildMinHeap() {
6868 }
6969
7070 public void insert (int elem ) {
71- heap = Arrays .copyOf (heap , 2 * size );
71+ heap = Arrays .copyOf (heap , size + 1 );
7272 int i = size ;
7373 int parentIndex = (int ) Math .floor ((i - 1 ) / 2 );
7474 while (i > 0 && elem < heap [parentIndex ]) {
You can’t perform that action at this time.
0 commit comments