-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathHeap.java
More file actions
123 lines (111 loc) · 3.51 KB
/
Heap.java
File metadata and controls
123 lines (111 loc) · 3.51 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.*;
// created a heap data structure
public class Heap {
private ArrayList<Integer> heap = new ArrayList<>();
// this is a list of elements which will be inserted in the heap
public ArrayList<Integer> list = new ArrayList<>();
Heap(){
heap.add(-1);
list.add(-1);
}
// this is to insert an element in the heap
// this is different from heapify because here we choose a child to parent swapping approach
private void insertion(int data){
if(heap.isEmpty()){
heap.add(data);
return;
}
heap.add(data);
int j = heap.size()-1;
int i = heap.size()-1;
while(i>=1){
i/=2;
if(heap.get(i)>heap.get(j)){
Collections.swap(heap, i, j);
}
j=i;
}
}
// this method is used when the root element does not have the correct value
// and we want to place it in the correct position by swapping its value from the
// children values till it reaches the correct place
// here it is different from insertion as it follows a parent to child approach swapping approach
// its TC: O(logn)
private void heapify(int data,int i){
int j = 2*i;
int k = 2*i+1;
if(j>=heap.size()&&k>=heap.size()){
return;
}
if(data<heap.get(j)&&data<heap.get(k)){
return;
}
int min = (heap.get(j)>heap.get(k))?k:j;
Collections.swap(heap, min, i);
heapify(data, min);
}
// we obtain the value of the top most priority element of the heap
private int top(){
return heap.get(1);
}
// we take out the top most priority element of the heap
private void pop(){
Collections.swap(heap,1,heap.size()-1);
heap.remove(heap.size()-1);
heapify(heap.get(1),1);
}
// this method is to build the heap from the array of elements
// it has complexity O(nlogn)
// this is different from heapify because here we choose a child to parent swapping approach
private void buildFromArray(){
int n = list.size();
for(int i=1;i<n;i++){
int indx = i;
int parent = indx/2;
// here indx>1 and not ind>0 because for 2nd case parent will become 0
// and swapping will happen for the 0th charater
while(indx>1 && list.get(indx)>list.get(parent)){
Collections.swap(list, indx, parent);
indx = parent;
parent/=2;
}
}
}
// building heap from array in TC: O(n)
private void buildHeap(){
int n = list.size();
for(int i=n/2;i>=1;i--){
heapify(list.get(i),i);
}
}
// printing the heap elements
private void print(){
for(Integer i: heap){
if(i==-1) continue;
System.out.print(i+" ");
}
System.out.println();
}
public static void main(String[] args) {
Heap h = new Heap();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n!=-1){
// h.insertion(n);
h.list.add(n);
n = sc.nextInt();
}
sc.close();
// h.print();
// h.insertion(0);
// h.print();
// System.out.println("min:"+h.top());
// h.pop();
// h.print();
h.buildHeap();
for(Integer i:h.list){
System.out.print(i+" ");
}
System.out.println();
}
}