Skip to content

Commit 924e6d9

Browse files
anwarshaikh078abranhe
authored andcommitted
MaxHeap.java
1 parent da262eb commit 924e6d9

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

MaxHeap.java

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
5+
abstract class Heap{
6+
protected int capacity;
7+
protected int size;
8+
protected int []items;
9+
10+
public Heap()
11+
{
12+
this.capacity = 10;
13+
this.size = 0;
14+
this.items = new int[capacity];
15+
}
16+
17+
public int getLeftChildIndex(int parentindex){
18+
return 2*parentindex+1;
19+
}
20+
21+
public int getRightChildIndex(int parentindex){
22+
return 2*parentindex+2;
23+
}
24+
25+
public int getParentIndex(int childIndex){
26+
return (childIndex-1)/2;
27+
}
28+
29+
public boolean hasLeftChild(int parentindex)
30+
{
31+
return getLeftChildIndex(parentindex) < size;
32+
}
33+
34+
35+
public boolean hasRightChild(int parentindex)
36+
{
37+
return getRightChildIndex(parentindex) < size;
38+
}
39+
40+
public boolean hasParent(int index){
41+
return getParentIndex(index) >= 0;
42+
}
43+
44+
public int leftChild(int parentindex){
45+
return items[getLeftChildIndex(parentindex)];
46+
}
47+
48+
public int rightChild(int parentindex){
49+
return items[getRightChildIndex(parentindex)];
50+
}
51+
52+
public int parent(int index)
53+
{
54+
return items[getParentIndex(index)];
55+
}
56+
57+
public void swap(int indexone,int indextwo){
58+
int temp = items[indexone];
59+
items[indexone] = items[indextwo];
60+
items[indextwo] = temp;
61+
}
62+
63+
public void add(int item)
64+
{
65+
items[size] = item;
66+
size++;
67+
heapifyUp();
68+
}
69+
public void isEmpty(String name)
70+
{
71+
if(size == 0)
72+
{
73+
System.out.println(name+"cant poll");
74+
}
75+
}
76+
77+
public int poll()
78+
{
79+
isEmpty("its empty");
80+
81+
int item = items[0];
82+
items[0] = items[size-1];
83+
size--;
84+
85+
heapifyDown();
86+
return item;
87+
}
88+
89+
public void print()
90+
{
91+
for(int i=0;i<size;i++)
92+
{
93+
System.out.print(items[i]);
94+
}
95+
}
96+
97+
public abstract void heapifyDown();
98+
99+
public abstract void heapifyUp();
100+
101+
}
102+
class MaxHeap extends Heap
103+
{
104+
public void heapifyDown()
105+
{
106+
int index = 0;
107+
108+
while(hasLeftChild(index)){
109+
int smallerChildIndex = getLeftChildIndex(index);
110+
111+
if(hasRightChild(index) && rightChild(index) > leftChild(index))
112+
{
113+
smallerChildIndex = getRightChildIndex(index);
114+
}
115+
116+
if(items[index] > items[smallerChildIndex])
117+
{
118+
break;
119+
}
120+
else{
121+
swap(index,smallerChildIndex);
122+
}
123+
index = smallerChildIndex;
124+
}
125+
}
126+
127+
128+
public void heapifyUp()
129+
{
130+
int index = size - 1;
131+
while(hasParent(index) && parent(index) < items[index]){
132+
swap(getParentIndex(index),index);
133+
index = getParentIndex(index);
134+
}
135+
}
136+
137+
public static void main(String []args)
138+
{
139+
Scanner ob = new Scanner(System.in);
140+
int n = ob.nextInt();
141+
Heap myHeap = new MaxHeap();
142+
for(int i=0;i<n;i++)
143+
{
144+
myHeap.add(ob.nextInt());
145+
}
146+
147+
myHeap.print();
148+
System.out.println("Element removed:"+myHeap.poll());
149+
myHeap.print();
150+
}
151+
}
152+

0 commit comments

Comments
 (0)