-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxHeap.java
More file actions
89 lines (73 loc) · 1.97 KB
/
Copy pathMaxHeap.java
File metadata and controls
89 lines (73 loc) · 1.97 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
public class MaxHeap {
private int []heap;
private int size;
private int capacity;
public MaxHeap(int capacity){
this.capacity = capacity;
this.size =0;
this.heap = new int[capacity];
}
public int parent(int index){
return (index-1)/2;
}
public int leftChild(int index){
return 2*index+1;
}
public int rightChild(int index){
return 2*index+2;
}
public void swap(int index1,int index2){
int temp = heap[index1];
heap[index1] = heap[index2];
heap[index2] = temp;
}
public void insert(int value){
//OUT OF RANGE --> OVERFLOW
if(size==capacity){
System.out.println("HEAP IS FULL....");
return;
}
int current = size;
heap[size++] = value;
while(current>0 && heap[current] > heap[parent(current)]){
swap(current,parent(current));
current = parent(current);
}
}
public int delete(){
//UNDERFLOW
if(size ==0){
System.out.println("HEAP IS EMPTY.....");
}
int x = heap[0];
heap[0] = heap[--size];
maxHeapify(0);
return x;
}
public void maxHeapify(int index){
int par = index;
int lci = leftChild(index);
int rci = rightChild(index);
if(lci < size && heap[lci] > heap[par]){
par = lci;
}
if(rci < size && heap[rci] > heap[par]){
par=rci;
}
if(par != index){
swap(index, par);
maxHeapify(par);
}
}
public static void main(String[] args) {
MaxHeap maxHeap = new MaxHeap(10);
maxHeap.insert(10);
maxHeap.insert(20);
maxHeap.insert(30);
maxHeap.insert(40);
maxHeap.insert(50);
System.out.println(maxHeap.delete());
System.out.println(maxHeap.delete());
System.out.println(maxHeap.delete());
}
}