-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathHeapdemo.java
96 lines (75 loc) · 1.5 KB
/
Heapdemo.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
import java.util.*;
class Heap{
int heapSize;
void build_max_heap(int[] a)
{
heapSize=a.length;
for(int i=(heapSize/2);i>=0;i--)
max_heapify(a,i);
}
void max_heapify(int[] a,int i)
{
int l=2*i+1;
int r=2*i+2;
int largest=i;
if(l<heapSize &&a[l]>a[largest])
largest=l;
if(r<heapSize &&a[r]>a[largest])
largest=r;
if(largest!=i)
{
int t=a[i];
a[i]=a[largest];
a[largest]=t;
max_heapify(a,largest);
}
}
//to delete the max element
int extract_max(int[] a)
{
if(heapSize<0)
System.out.println("underflow");
int max=a[0];
a[0]=a[heapSize-1];
heapSize--;
max_heapify(a,0);
return max;
}
void increase_key(int[] a,int i,int key)
{
if(key<a[i])
System.out.println("error");
a[i]=key;
while(i>=0 && a[(i-1)/2]<a[i])
{
int t=a[(i-1)/2];
a[(i-1)/2]=a[i];
a[i]=t;
i=(i-1)/2;
}
}
void print_heap(int a[])
{
for(int i=0;i<heapSize;i++)
System.out.println(a[i]+" ");
}
}
public class Heapdemo{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int a[]=new int[n];
System.out.println("enter the elements of array");
for(int i=0;i<n;i++)
a[i]=in.nextInt();
Heap ob=new Heap();
ob.build_max_heap(a);
ob.print_heap(a);
System.out.println("maximum element is : "+ob.extract_max(a));
ob.print_heap(a);
System.out.println("maximum element is : "+ob.extract_max(a));
ob.increase_key(a,6,800);
ob.print_heap(a);
}
}