File tree 2 files changed +5
-8
lines changed
src/main/java/com/rampatra/common
2 files changed +5
-8
lines changed Original file line number Diff line number Diff line change 5
5
import java .util .Arrays ;
6
6
7
7
/**
8
- * Created by IntelliJ IDEA.
9
- * <p/>
10
8
* A HEAP is a specialized tree-based ABSTRACT DATA TYPE that satisfies the heap property:
11
9
* min-heap: All non-leaf elements are either smaller than or equal to their left and right child.
12
10
* max-heap: All non-leaf elements are either greater than or equal to their left and right child.
19
17
* Therefore, buildMaxHeap() would take O(n log n) time BUT IF OBSERVED CAREFULLY IT TAKES 0(N) TIME.
20
18
* <p/>
21
19
* 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>
22
21
*
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
27
24
*/
28
25
public class MaxHeap {
29
26
Original file line number Diff line number Diff line change 14
14
* Each successor can be found in O(log n). The algorithm in minHeapify() takes O(log n) time
15
15
* Therefore, buildMinHeap() would take O(n log n) time BUT IF OBSERVED CAREFULLY IT TAKES 0(N) TIME.
16
16
* <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.
18
18
* <a href="@see http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap07.htm">Learn more</a>
19
19
*
20
20
* @author rampatra
@@ -68,7 +68,7 @@ public void buildMinHeap() {
68
68
}
69
69
70
70
public void insert (int elem ) {
71
- heap = Arrays .copyOf (heap , 2 * size );
71
+ heap = Arrays .copyOf (heap , size + 1 );
72
72
int i = size ;
73
73
int parentIndex = (int ) Math .floor ((i - 1 ) / 2 );
74
74
while (i > 0 && elem < heap [parentIndex ]) {
You can’t perform that action at this time.
0 commit comments