-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.java
More file actions
177 lines (148 loc) · 3.87 KB
/
MaxHeap.java
File metadata and controls
177 lines (148 loc) · 3.87 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
public class MaxHeap {
private static int maxsize;
private static int size=0;
private static int[] heap;
private MaxHeap(int maxsize)
{
MaxHeap.maxsize = maxsize;
MaxHeap.heap = new int[maxsize];
MaxHeap.heap[0] = Integer.MAX_VALUE;
for (int i=1;i<MaxHeap.heap.length;i++)
{
MaxHeap.heap[i]=-1;
}
}
private static void insert(int x)
{
if(MaxHeap.heap[0]==Integer.MAX_VALUE)
{
MaxHeap.size++;
MaxHeap.heap[0] =x;
MaxHeap.heap[1] = -1;
MaxHeap.heap[2] = -1;
System.out.println(x+" is inserted at position 0");
maxsize =size;
}
else
{
int v = size/2;
int parent;
if(v==0 || v==1)
{
parent =0;
}
else
{
parent = v-1;
}
int left = parent*2 +1;
int right = parent * 2 +2;
if(MaxHeap.heap[left]==-1)
{
MaxHeap.heap[left]=x;
System.out.println(x+" is inserted at position "+ left);
size++;
}
else if(MaxHeap.heap[right] == -1)
{
MaxHeap.heap[right] = x;
System.out.println(x+" is inserted at position "+ right);
size++;
}
else
{
MaxHeap.heap[size] = x;
System.out.println(x+" is inserted at position "+ size);
size++;
}
}
System.out.println("size is " + MaxHeap.size);
heapify(MaxHeap.size,x);
}
private static void swap(int a, int b)
{
int temp = MaxHeap.heap[a];
MaxHeap.heap[a]=MaxHeap.heap[b];
MaxHeap.heap[b] =temp;
}
private static void heapify(int c, int x)
{
if(c==0 || c==1)
{
System.out.println("found at position 0");
return;
}
int parent = c /2 -1;
int left = parent *2 +1;
int right = parent *2 +2;
if(MaxHeap.heap[left]<= MaxHeap.heap[parent] && MaxHeap.heap[right]<= MaxHeap.heap[parent])
{
if(MaxHeap.heap[left]==x)
{
System.out.println("found at position "+left);
}
else
{
System.out.println("found at position "+ right);
}
return;
}
if(MaxHeap.heap[left]> MaxHeap.heap[parent])
{
swap(left, parent);
}
if(MaxHeap.heap[right]>MaxHeap.heap[parent])
{
swap(right,parent);
}
heapify(parent+1,x);
}
private static void fix(int p)
{
int l = 2*p+1;
int r = 2*p+2;
if(MaxHeap.heap[p]==-1)
{
return;
}
if(MaxHeap.heap[p]< MaxHeap.heap[l])
{
swap(p,l);
}
if(MaxHeap.heap[p]<MaxHeap.heap[r])
{
swap(p,r);
}
fix(l);
fix(r);
}
private static void popmax()
{
int max = MaxHeap.heap[0];
MaxHeap.heap[0]= MaxHeap.heap[MaxHeap.size-1];
MaxHeap.heap[MaxHeap.size-1]=-1;
MaxHeap.size--;
fix(0);
System.out.println("max element is " +max);
}
public static void main(String[] args) {
MaxHeap a = new MaxHeap(20);
a.insert(4);
a.insert(20);
a.insert(5);
a.insert(3);
a.insert(2);
a.insert(7);
a.insert(10);
a.insert(90);
for ( int i=0;i<MaxHeap.heap.length;i++)
{
System.out.println(MaxHeap.heap[i]);
}
popmax();
for ( int i=0;i<MaxHeap.heap.length;i++)
{
System.out.println(MaxHeap.heap[i]);
}
}
}