-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionMaxHeap.java
More file actions
40 lines (33 loc) · 974 Bytes
/
InsertionMaxHeap.java
File metadata and controls
40 lines (33 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package Heaps;
import java.util.ArrayList;
import java.util.List;
/*
* 1) Insert at the last.
* 2) Make sure that the new node is at correct position.
* */
public class InsertionMaxHeap {
public static void insert(List<Integer> heap, int element) {
heap.add(element);
int index = heap.size() - 1;
pushUp(heap, index);
}
public static void pushUp(List<Integer> heap, int i) {
int parent_index = (i - 1) / 2;
// Base Case
if (i == 0 || heap.get(parent_index) > heap.get(i)) return;
Basics.swap(heap, i, parent_index);
pushUp(heap, parent_index);
}
// Time Complexity - O(logN)
public static void main(String[] args) {
List<Integer> heap = new ArrayList<>();
heap.add(80);
heap.add(50);
heap.add(70);
heap.add(10);
heap.add(20);
System.out.println(heap);
insert(heap, 90);
System.out.println(heap);
}
}