forked from Sandip-Jana/Algorithms-ACM-ICPC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeap_Operations.java
More file actions
129 lines (128 loc) · 2.86 KB
/
Heap_Operations.java
File metadata and controls
129 lines (128 loc) · 2.86 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
124
125
126
127
128
129
import java.io.*;
import java.util.*;
class Heap_Operations
{
BufferedReader in;
int Heap_size;
public static void main(String[] args) throws IOException{
new Heap_Operations().run();
}
void run()throws IOException {
in=new BufferedReader(new InputStreamReader(System.in));
solve();
}
void solve() throws IOException {
int n=Integer.parseInt(in.readLine());
StringTokenizer st=new StringTokenizer(in.readLine());
int a[]=new int[n+1];
// must always be 1-index bcz 0 ka parent 0*2=0 so 1- index only
for(int i=1;i<=n;i++)
a[i]=Integer.parseInt(st.nextToken());
Heap(a);
}
void Heap(int a[]) throws IOException {
Heap_size=a.length;
build_Max_Heap(a);
for(int i=1;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
//building heap is complete
//lets sort the array
System.out.println("Enter 1 to sort");
System.out.println("Enter 2 to find max");
System.out.println("Enter 3 to heap Extract Max");
int ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1 : heapsort(a);
Heap_size=a.length;
for(int i=1;i<a.length;i++)
System.out.print(a[i]+" ");
break;
case 2 : System.out.println("Max = " + find_max(a)); break;
case 3 : System.out.println("Extract_MAX : "+Extract_MAX(a)); break;
case 4 :System.out.println("Enter index and Key");
int index=Integer.parseInt(in.readLine());
int key=Integer.parseInt(in.readLine());
heap_increase_key(a,index,key);
System.out.println("heap Increased And Updated "); break;
default : break;
}
}
void heap_increase_key(int a[],int i,int key) {
if(key<a[i])
System.out.println("new key less than original");
else
{
a[i]=key;
while(i>1 && a[parent(i)]<a[i])
{
swap(a,i,parent(i));
i=parent(i);
}
}
}
int find_max(int a[]) {
if(a.length>=2)
return a[1];
else
System.out.println("Heap Empty ... ");
return 0;
}
int Extract_MAX(int a[]) {
int max=-1;
if(a.length<2)
System.out.println("Heap Underflow");
else
{
max=a[1];
a[1]=a[a.length-1];
Heap_size-=1;
heapify(a,1);
}
return max;
}
void heapsort(int a[]) {
for(int i=a.length-1;i>=2;i--) {
swap(a,i,1);
Heap_size-=1;
heapify(a,1);
}
for(int i=1;i<a.length;i++)
System.out.println(a[i]);
}
void build_Max_Heap(int a[]) {
for(int i=a.length/2;i>=1;i--) {
heapify(a,i);
}
}
void heapify(int a[],int i) {
int left=left_child(i);
int right=right_child(i);
int largest=-1;
if(left<Heap_size && a[left]>a[i])
largest=left;
else
largest=i;
if(right<Heap_size && a[right]>a[largest])
largest=right;
if(largest!=i) {
swap(a,i,largest);
heapify(a,largest);
}
}
void swap(int a[],int i,int j) {
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
int parent(int i) {
return i/2;
}
int left_child(int i) {
return 2*i;
}
int right_child(int i) {
return 2*i+1;
}
}