Skip to content

Commit de1976b

Browse files
Custom MinHeap and MaxHeap Implementation added
1 parent 084a81a commit de1976b

2 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.ArrayList;
2+
3+
//Implemented a custom MaxHeap using generics which supports operation to peek ,insert and remove
4+
5+
public class CustomMaxHeap <T extends Comparable<T>> {
6+
ArrayList<T> list;
7+
public CustomMaxHeap(){
8+
list = new ArrayList<>();
9+
}
10+
public void insert(T val){
11+
list.add(val);
12+
upHeapify(list.size()-1);
13+
}
14+
public T peek() throws Exception {
15+
if(list.size()==0){
16+
throw new Exception("Heap is empty.Cannot peek");
17+
18+
}
19+
return list.get(0);
20+
}
21+
@Override
22+
public String toString() {
23+
return list.toString();
24+
}
25+
public int size(){
26+
return list.size();
27+
}
28+
public T remove(){
29+
if(list.size()==0){return null;}
30+
if(list.size()==1){return list.remove(0);}
31+
T val = list.get(0);
32+
T temp = val;
33+
list.set(0,list.get(list.size()-1));
34+
list.set(list.size()-1,temp);
35+
list.remove(list.size()-1);
36+
downHeapify(0);
37+
return val;
38+
}
39+
public void downHeapify(int index){
40+
int maximum = index;
41+
int left =getLeftChildIndex(index) ;
42+
int right =getRightChildIndex(index);
43+
44+
if(left < list.size() && list.get(left).compareTo(list.get(maximum) ) > 0){
45+
maximum = left ;
46+
}
47+
if(right< list.size() && list.get(right).compareTo(list.get(maximum) ) > 0){
48+
maximum = right;
49+
}
50+
if(maximum !=index){
51+
T temp = list.get(maximum);
52+
list.set(maximum,list.get(index));
53+
list.set(index,temp);
54+
downHeapify(maximum);
55+
}
56+
}
57+
public void upHeapify(int index){
58+
if(index==0){
59+
return;
60+
}
61+
int parentindex = getParentIndex(index);
62+
if(list.get(parentindex).compareTo(list.get(index)) < 0){
63+
T temp = list.get(parentindex);
64+
list.set(parentindex,list.get(index));
65+
list.set(index,temp);
66+
upHeapify(parentindex);
67+
}
68+
69+
}
70+
public int getParentIndex(int index){
71+
return (index -1)/2;
72+
}
73+
public int getLeftChildIndex(int index){
74+
return 2*index + 1;
75+
}
76+
77+
public int getRightChildIndex(int index){
78+
return 2*index + 2;
79+
}
80+
public static void main(String[] args) throws Exception {
81+
CustomMaxHeap<Integer> maxHeap = new CustomMaxHeap<>();
82+
maxHeap.insert(5);
83+
maxHeap.insert(500);
84+
maxHeap.insert(10);
85+
maxHeap.insert(100);
86+
maxHeap.insert(15);
87+
maxHeap.insert(20);
88+
System.out.println(maxHeap);
89+
System.out.println(maxHeap.peek());
90+
System.out.println(maxHeap.remove());
91+
System.out.println(maxHeap);
92+
System.out.println(maxHeap.size());
93+
}
94+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import java.util.ArrayList;
2+
//Implemented a custom MinHeap using generics which supports operation to peek ,insert and remove
3+
public class CustomMinHeap <T extends Comparable<T>> {
4+
5+
ArrayList<T> list;
6+
public CustomMinHeap(){
7+
list = new ArrayList<>();
8+
}
9+
public void insert(T val){
10+
list.add(val);
11+
upHeapify(list.size()-1);
12+
}
13+
public int getParentIndex(int index){
14+
return (index -1)/2;
15+
}
16+
public int getLeftChildIndex(int index){
17+
return 2*index + 1;
18+
}
19+
public int size(){
20+
return list.size();
21+
}
22+
public void upHeapify(int index){
23+
if(index==0){
24+
return;
25+
}
26+
int parentindex = getParentIndex(index);
27+
if(list.get(parentindex).compareTo(list.get(index)) > 0){
28+
T temp = list.get(parentindex);
29+
list.set(parentindex,list.get(index));
30+
list.set(index,temp);
31+
upHeapify(parentindex);
32+
}
33+
34+
}
35+
36+
public T remove(){
37+
if(list.size()==0){return null;}
38+
if(list.size()==1){return list.remove(0);}
39+
T val = list.get(0);
40+
T temp = val;
41+
list.set(0,list.get(list.size()-1));
42+
list.set(list.size()-1,temp);
43+
list.remove(list.size()-1);
44+
downHeapify(0);
45+
return val;
46+
}
47+
public void downHeapify(int index){
48+
int minimum = index;
49+
int left =getLeftChildIndex(index) ;
50+
int right =getRightChildIndex(index);
51+
52+
if(left < list.size() && list.get(left).compareTo(list.get(minimum) ) < 0){
53+
minimum = left ;
54+
}
55+
if(right< list.size() && list.get(right).compareTo(list.get(minimum) ) < 0){
56+
minimum = right;
57+
}
58+
if(minimum !=index){
59+
T temp = list.get(minimum);
60+
list.set(minimum,list.get(index));
61+
list.set(index,temp);
62+
downHeapify(minimum);
63+
}
64+
}
65+
public int getRightChildIndex(int index){
66+
return 2*index + 2;
67+
}
68+
public T peek() throws Exception {
69+
if(list.size()==0){
70+
throw new Exception("Heap is empty.Cannot peek");
71+
}
72+
return list.get(0);
73+
}
74+
@Override
75+
public String toString() {
76+
return list.toString();
77+
}
78+
public static void main(String[] args) throws Exception {
79+
CustomMinHeap<Integer> minHeap = new CustomMinHeap<>();
80+
minHeap.insert(5);
81+
minHeap.insert(500);
82+
minHeap.insert(10);
83+
minHeap.insert(100);
84+
minHeap.insert(15);
85+
minHeap.insert(20);
86+
System.out.println(minHeap);
87+
System.out.println(minHeap.peek());
88+
System.out.println(minHeap.remove());
89+
System.out.println(minHeap);
90+
91+
}
92+
}

0 commit comments

Comments
 (0)