forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapmain.java
315 lines (279 loc) · 6.67 KB
/
heapmain.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Implementation of heap.
* A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
* A tree is said to be complete if all its levels, except possibly the deepest, are complete.
* A heap has many applications, including the most efficient implementation of priority queues, which are useful in many applications.
* In the worst case, the time complexity of inserting a node in a heap would be O(logN).
* The delete operation has a time complexity relative to the height of the tree, or O(log n).
* Heapsort is a comparison-based sorting algorithm. Heaps are often implemented in an array rather than a linked list.
Overall time complexity of Heap Sort is O(nLogn).
*/
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;
class heap //max-heap
{
private static final int d = 2;
private int[] arr1;
private int heapSize;
//This will initialize our heap with default size.
public heap(int capacity) {
heapSize = 0;
arr1 = new int[capacity + 1];
Arrays.fill(arr1, -1);
}
//This will check if the heap is empty or not
public boolean isEmpty() {
return heapSize == 0;
}
//to check if heap is full or not
public boolean isFull() {
return heapSize == arr1.length;
}
private int parent(int i) {
return (i - 1) / d;
}
private int kthChild(int i, int k) {
return d * i + k;
}
//This will insert new element in to heap Complexity: O(log N) As worst case scenario, we need to traverse till the root
public void insert(int x) {
if (isFull())
throw new NoSuchElementException("Heap is full, No space to insert new element");
arr1[heapSize++] = x;
heapifyUp(heapSize - 1);
}
//This will delete element at index x
public int delete(int x)
{
if (isEmpty())
throw new NoSuchElementException("Heap is empty, No element to delete");
int key = arr1[x];
arr1[x] = arr1[heapSize - 1];
heapSize--;
heapifyDown(x);
return key;
}
// This method used to maintain the heap property while inserting an element.
private void heapifyUp(int i)
{
int temp = arr1[i];
while (i > 0 && temp > arr1[parent(i)]) {
arr1[i] = arr1[parent(i)];
i = parent(i);
}
arr1[i] = temp;
}
// This method used to maintain the heap property while deleting an element.
private void heapifyDown(int i)
{
int child;
int temp = arr1[i];
while (kthChild(i, 1) < heapSize) {
child = maxChild(i);
if (temp < arr1[child]) {
arr1[i] = arr1[child];
} else
break;
i = child;
}
arr1[i] = temp;
}
private int maxChild(int i) {
int leftChild = kthChild(i, 1);
int rightChild = kthChild(i, 2);
return arr1[leftChild] > arr1[rightChild] ? leftChild : rightChild;
}
//This method used to print all element of the heap
public void printHeap()
{
for (int i = 0; i < heapSize; i++)
System.out.print(arr1[i] + " ");
System.out.println();
}
void heapify(int arr[], int n, int i)
{
int temp;
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
int temp;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void sort1()
{
heapSort(arr1, heapSize);
}
}
public class heapmain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s1 = new Scanner(System.in);
int choice = 0;
int a1 = 0;
System.out.println("ENTER THE SIZE OF HEAP");
a1=s1.nextInt();
System.out.println(" ");
heap a = new heap(a1);
int yn = 0;
do {
System.out.println("PRESS 1 TO CREATE ");
System.out.println("PRESS 2 TO ADD ");
System.out.println("PRESS 3 TO DELETE ");
System.out.println("PRESS 4 TO DISPLAY");
System.out.println("PRESS 5 TO SORT");
System.out.println("PRESS 0 TO EXIT");
System.out.println(" ");
choice = s1.nextInt();
switch (choice) {
case 1:
do {
System.out.println("ENTER THE NUMBER TO BE ADDED");
int k = 0;
k = s1.nextInt();
a.insert(k);
System.out.println("DO YOU WANT TO CONTINUE?(1=YES /2=N0)");
yn = s1.nextInt();
} while (yn != 2);
break;
case 2:
System.out.println("ENTER THE NEW NUMBER TO BE ADDED");
int k = 0;
k = s1.nextInt();
a.insert(k);
System.out.print("NEW HEAP = ");
a.printHeap();
System.out.println(" ");
break;
case 3:
System.out.println("ENTER THE INDEX OF THE NUMBER TO BE DELETED");
int q0 = 0;
q0 = s1.nextInt();
a.delete(q0);
System.out.print("NEW HEAP = ");
a.printHeap();
System.out.println(" ");
break;
case 4:
System.out.print("HEAP = ");
a.printHeap();
System.out.println(" ");
break;
case 5:
System.out.print("SORTED HEAP = ");
a.sort1();
a.printHeap();
System.out.println(" ");
break;
}
} while (choice != 0);
}
}
/*
OUTPUT :
ENTER THE SIZE OF HEAP
20
PRESS 1 TO CREATE
PRESS 2 TO ADD
PRESS 3 TO DELETE
PRESS 4 TO DISPLAY
PRESS 5 TO SORT
PRESS 0 TO EXIT
1
ENTER THE NUMBER TO BE ADDED
17
DO YOU WANT TO CONTINUE?(1=YES /2=N0)
1
ENTER THE NUMBER TO BE ADDED
25
DO YOU WANT TO CONTINUE?(1=YES /2=N0)
1
ENTER THE NUMBER TO BE ADDED
3
DO YOU WANT TO CONTINUE?(1=YES /2=N0)
1
ENTER THE NUMBER TO BE ADDED
1
DO YOU WANT TO CONTINUE?(1=YES /2=N0)
1
ENTER THE NUMBER TO BE ADDED
0
DO YOU WANT TO CONTINUE?(1=YES /2=N0)
1
ENTER THE NUMBER TO BE ADDED
1008
DO YOU WANT TO CONTINUE?(1=YES /2=N0)
2
PRESS 1 TO CREATE
PRESS 2 TO ADD
PRESS 3 TO DELETE
PRESS 4 TO DISPLAY
PRESS 5 TO SORT
PRESS 0 TO EXIT
4
HEAP = 1008 17 25 1 0 3
PRESS 1 TO CREATE
PRESS 2 TO ADD
PRESS 3 TO DELETE
PRESS 4 TO DISPLAY
PRESS 5 TO SORT
PRESS 0 TO EXIT
2
ENTER THE NEW NUMBER TO BE ADDED
45
NEW HEAP = 1008 17 45 1 0 3 25
PRESS 1 TO CREATE
PRESS 2 TO ADD
PRESS 3 TO DELETE
PRESS 4 TO DISPLAY
PRESS 5 TO SORT
PRESS 0 TO EXIT
2
ENTER THE NEW NUMBER TO BE ADDED
58
NEW HEAP = 1008 58 45 17 0 3 25 1
PRESS 1 TO CREATE
PRESS 2 TO ADD
PRESS 3 TO DELETE
PRESS 4 TO DISPLAY
PRESS 5 TO SORT
PRESS 0 TO EXIT
3
ENTER THE INDEX OF THE NUMBER TO BE DELETED
0
NEW HEAP = 58 17 45 1 0 3 25
PRESS 1 TO CREATE
PRESS 2 TO ADD
PRESS 3 TO DELETE
PRESS 4 TO DISPLAY
PRESS 5 TO SORT
PRESS 0 TO EXIT
5
SORTED HEAP = 0 1 3 17 25 45 58
PRESS 1 TO CREATE
PRESS 2 TO ADD
PRESS 3 TO DELETE
PRESS 4 TO DISPLAY
PRESS 5 TO SORT
PRESS 0 TO EXIT
0
*/