-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeap.java
171 lines (109 loc) · 3.04 KB
/
Heap.java
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Author : Akash Yadav
SpaceTime : 20th JAN 2018
*/
import java.util.*;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Heaps {
public static class Heap{
private ArrayList<Integer> arr;
//contains maximum size
private int heapsize;
public Heap(){
this.arr = new ArrayList<>();
this.arr.add(0);
this.heapsize = 0;
}
private void corrrectHeapsize(){
this.heapsize = this.arr.size() - 1;
}
public void addKey(int key){
this.arr.add(key);
corrrectHeapsize();
}
public int returnMax() {
return this.arr.get(1);
}
public int removeMax() {
int ans = this.arr.get(1);
//swap arr[first] and arr[last]
Collections.swap(this.arr, this.heapsize, 1);
this.arr.remove(this.heapsize);
corrrectHeapsize();
heapify(1);
return ans;
}
public void increaseKey(int idx, int key) {
if(key <= arr.get(idx)) {
System.out.println("key not big enough to increase");
return;
}
this.arr.set(idx, key);
while( (idx > 1) && (this.arr.get(idx) > this.arr.get(idx/2)) ) {
//swap arr[idx] and arr[parent(i)]
Collections.swap(this.arr, idx, idx/2);
idx = idx / 2;
}
}
public void decreaseKey(int idx, int key) {
if(key >= arr.get(idx)) {
System.out.println("key not small enough to decrease");
return;
}
this.arr.set(idx, key);
heapify(idx);
}
public void display(){
corrrectHeapsize();
System.out.println("HEAP : " );
for(int i = 1; i <= this.heapsize; i++)
System.out.print("" + arr.get(i) + " ");
System.out.println("\n");
}
private void heapify(int i){
int leftChild = 2*i;
int rightChild = 2*i + 1;
int largest = i;
if( (leftChild <= this.heapsize) && (this.arr.get(leftChild) > this.arr.get(largest)) )
largest = leftChild;
if( (rightChild <= this.heapsize) && (this.arr.get(rightChild) > this.arr.get(largest)) )
largest = rightChild;
if(largest != i){
//swap arr[largest] and arr[i]
Collections.swap(this.arr, largest , i);
heapify(largest);
}
}
public void buildMaxHeap(){
corrrectHeapsize();
//start from the parent of the last element and go up
for(int i = (int)Math.floor(this.heapsize/2) ; i > 0; i--)
heapify(i);
corrrectHeapsize();
}
public void heapSort(){
buildMaxHeap();
int temp = this.heapsize;
for(int i = temp; i > 0; i--){
//swap arr[1] and arr[heapsize]
Collections.swap(this.arr, 1, this.heapsize);
this.heapsize--;
heapify(1);
}
}
}
public static void main(String[] args) {
Heap H = new Heap();
for(int i = 1; i <= 10; i++)
H.addKey(i);
H.display();
H.buildMaxHeap();
H.display();
//H.heapSort();
//H.display();
System.out.println("" + H.removeMax());
H.display();
}
}