-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.java
108 lines (96 loc) · 1.81 KB
/
Heap.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
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
package heap;
import java.util.NoSuchElementException;
public class Heap
{
private int[] a;
private int n;
public Heap()
{
a=new int[10];
n=0;
a[0]=99999;
}
public Heap(int maxSize)
{
a=new int[maxSize];
n=0;
a[0]=99999;
}
public void insert(int value)
{
n++;
a[n]=value;
restoreUp(n);
}
private void restoreUp(int i)
{
int k=a[i];
int iparent=i/2;
while(a[iparent]<k) /* No sentinel : while(iparent>=1 && a[iparent]<k) */
{
a[i]=a[iparent];
i=iparent;
iparent=i/2;
}
a[i]=k;
}
public int deleteRoot()
{
if(n==0)
throw new NoSuchElementException("Heap is Empty");
int maxValue=a[1];
a[1]=a[n];
n--;
restoreDown(1);
return maxValue;
}
private void restoreDown(int i)
{
int k=a[i];
int lchild=2*i, rchild=lchild+1;
while(rchild<=n)
{
if( k>=a[lchild] && k>=a[rchild] )
{
a[i]=k;
return;
}
else if(a[lchild] > a[rchild])
{
a[i]=a[lchild];
i=lchild;
}
else
{
a[i]=a[rchild];
i=rchild;
}
lchild=2*i;
rchild=lchild+1;
}
/*If number of nodes is even*/
if(lchild==n && k<a[lchild])
{
a[i]=a[lchild];
i=lchild;
}
a[i]=k;
}
public void display()
{
int i;
if(n==0)
{
System.out.println("Heap is empty");
return;
}
System.out.println("Heap size = " + n);
for(i=1; i<=n; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}