-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.java
More file actions
71 lines (64 loc) · 1.74 KB
/
MaxHeap.java
File metadata and controls
71 lines (64 loc) · 1.74 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
import java.util.Scanner;
import java.io.*;
import java.lang.String;
class MaxHeap {
int[] array;
int next = 1;
int last = -1;
int curr = 0;
int root = 1;
String command = null;
int commandNum = 0;
int n;
//"next" is the next empty space to insert, last location
//inserted into. "size" is number of elements in array
public static void main(String[] args) {
//Get the number of commands, n
MaxHeap heap = new MaxHeap();
Scanner in = new Scanner(new InputStreamReader(System.in));
heap.n = in.nextInt();
heap.array = new int[heap.n+1];
//Execute commands
for(int i = 0; i < heap.n; i++){
heap.command = in.next();
if(heap.command.equals("Insert") && in.hasNextInt()){
heap.commandNum = in.nextInt();
heap.insert(heap.commandNum, heap.array);
}
else if(heap.command.equals("Extract"))
heap.extract(heap.array);
}
}
public void insert(int num, int[] arr) {
this.array[next] = num;
this.curr = this.last = this.next;
this.next++;
//while num larger than parent, swap positions
while(this.curr > 1 && arr[this.curr/2] < num){
swap(this.curr, this.curr/2, array);
}
}
public void swap(int ind1, int ind2, int[] arr){
int cNum = arr[ind1];
arr[ind1] = arr[ind2];
arr[ind2] = cNum;
this.curr = ind2;
}
public void extract(int[] arr){
//remove and print max
System.out.println(arr[root]);
this.swap(this.root, this.last, arr);
arr[this.last] = 0;
this.next = this.last;
this.last--;
//place new root in correct postion
this.curr = this.root;
while(arr[this.curr] < arr[this.curr*2]
|| arr[this.curr] < arr[this.curr*2 + 1]){
if(arr[this.curr*2] > arr[this.curr*2 + 1]){
this.swap(this.curr, this.curr*2, arr);
}
else this.swap(this.curr,this.curr*2 + 1, arr);
}
}
}