Skip to content

Commit ebfb455

Browse files
Merge pull request #175 from Monica-Chhabria/MinMaxHeap
Custom MinHeap and MaxHeap Implementation added
2 parents 2a49e54 + 6eebe10 commit ebfb455

2 files changed

Lines changed: 237 additions & 0 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
//constructor
8+
public CustomMaxHeap(){
9+
list = new ArrayList<>();
10+
}
11+
//function to insert into max heap
12+
public void insert(T val){
13+
list.add(val);
14+
upHeapify(list.size()-1);
15+
}
16+
//function to peek into max heap and get the root
17+
public T peek() throws Exception {
18+
if(list.size()==0){
19+
throw new Exception("Heap is empty.Cannot peek");
20+
21+
}
22+
return list.get(0);
23+
}
24+
@Override
25+
public String toString() {
26+
return list.toString();
27+
}
28+
//function to return no of elements in the heap
29+
public int size(){
30+
return list.size();
31+
}
32+
//function to remove the element from the heap
33+
public T remove(){
34+
if(list.size()==0){return null;}
35+
if(list.size()==1){return list.remove(0);}
36+
T val = list.get(0);
37+
T temp = val;
38+
//swap root elment with the last element
39+
list.set(0,list.get(list.size()-1));
40+
list.set(list.size()-1,temp);
41+
//remove the last element
42+
list.remove(list.size()-1);
43+
downHeapify(0);
44+
return val;
45+
}
46+
//function to downheapify
47+
public void downHeapify(int index){
48+
49+
int maximum = index;
50+
//getting left child index
51+
int left =getLeftChildIndex(index);
52+
//getting right child index
53+
int right =getRightChildIndex(index);
54+
//if left child value is greater than current maximum index value set maximum to left child index
55+
if(left < list.size() && list.get(left).compareTo(list.get(maximum) ) > 0){
56+
maximum = left ;
57+
}
58+
//if right child value is greater than current maximum index value set maximum to right child index
59+
if(right< list.size() && list.get(right).compareTo(list.get(maximum) ) > 0){
60+
maximum = right;
61+
}
62+
//swap maximum index value to current index value if maximum !=index
63+
if(maximum !=index){
64+
T temp = list.get(maximum);
65+
list.set(maximum,list.get(index));
66+
list.set(index,temp);
67+
downHeapify(maximum);
68+
}
69+
}
70+
//function to upheapify
71+
public void upHeapify(int index){
72+
if(index==0){
73+
return;
74+
}
75+
//getting parent index
76+
int parentindex = getParentIndex(index);
77+
//if the parent value is smaller than current node value swap them since parent has to be maximum
78+
if(list.get(parentindex).compareTo(list.get(index)) < 0){
79+
T temp = list.get(parentindex);
80+
list.set(parentindex,list.get(index));
81+
list.set(index,temp);
82+
upHeapify(parentindex);
83+
}
84+
85+
}
86+
//function to get the index of the parent
87+
public int getParentIndex(int index){
88+
return (index -1)/2;
89+
}
90+
//function to get the index of the left child
91+
public int getLeftChildIndex(int index){
92+
return 2*index + 1;
93+
}
94+
//function to get the index of the right child
95+
public int getRightChildIndex(int index){
96+
return 2*index + 2;
97+
}
98+
public static void main(String[] args) throws Exception {
99+
//Initialize the heap
100+
CustomMaxHeap<Integer> maxHeap = new CustomMaxHeap<>();
101+
//inserting into the heap
102+
maxHeap.insert(5);
103+
maxHeap.insert(500);
104+
maxHeap.insert(10);
105+
maxHeap.insert(100);
106+
maxHeap.insert(15);
107+
maxHeap.insert(20);
108+
//display the heap
109+
System.out.println(maxHeap);
110+
//get the element at the top
111+
System.out.println(maxHeap.peek());
112+
//remove the max element and print it
113+
System.out.println(maxHeap.remove());
114+
//display the heap after removing max element
115+
System.out.println(maxHeap);
116+
//display the size of the heap
117+
System.out.println(maxHeap.size());
118+
}
119+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
//constructor
7+
public CustomMinHeap(){
8+
list = new ArrayList<>();
9+
}
10+
//function to insert into min heap
11+
public void insert(T val){
12+
list.add(val);
13+
upHeapify(list.size()-1);
14+
}
15+
//function to get the index of the parent
16+
public int getParentIndex(int index){
17+
return (index -1)/2;
18+
}
19+
//function to get the index of the left child
20+
public int getLeftChildIndex(int index){
21+
return 2*index + 1;
22+
}
23+
//function to return no of elements in the heap
24+
public int size(){
25+
return list.size();
26+
}
27+
//function to upheapify
28+
public void upHeapify(int index){
29+
if(index==0){
30+
return;
31+
}
32+
//getting parent index
33+
int parentindex = getParentIndex(index);
34+
//if the parent value is greater than current node value swap them since parent has to be minimum
35+
if(list.get(parentindex).compareTo(list.get(index)) > 0){
36+
T temp = list.get(parentindex);
37+
list.set(parentindex,list.get(index));
38+
list.set(index,temp);
39+
upHeapify(parentindex);
40+
}
41+
42+
}
43+
44+
//function to remove the element from the heap
45+
public T remove(){
46+
if(list.size()==0){return null;}
47+
if(list.size()==1){return list.remove(0);}
48+
T val = list.get(0);
49+
T temp = val;
50+
//swap root elment with the last element
51+
list.set(0,list.get(list.size()-1));
52+
list.set(list.size()-1,temp);
53+
//remove the last element
54+
list.remove(list.size()-1);
55+
downHeapify(0);
56+
return val;
57+
}
58+
//function to downheapify
59+
public void downHeapify(int index){
60+
int minimum = index;
61+
//getting left child index
62+
int left =getLeftChildIndex(index) ;
63+
//getting right child index
64+
int right =getRightChildIndex(index);
65+
//if left child value is smaller than current minimum index value set minimum to left child index
66+
if(left < list.size() && list.get(left).compareTo(list.get(minimum) ) < 0){
67+
minimum = left ;
68+
}
69+
//if right child value is smaller than current minimum index value set minimum to right child index
70+
if(right< list.size() && list.get(right).compareTo(list.get(minimum) ) < 0){
71+
minimum = right;
72+
}
73+
//swap minimum index value to current index value if minimum !=index
74+
if(minimum !=index){
75+
T temp = list.get(minimum);
76+
list.set(minimum,list.get(index));
77+
list.set(index,temp);
78+
downHeapify(minimum);
79+
}
80+
}
81+
//function to get the index of the right child
82+
public int getRightChildIndex(int index){
83+
return 2*index + 2;
84+
}
85+
//function to peek into min heap and get the root
86+
public T peek() throws Exception {
87+
if(list.size()==0){
88+
throw new Exception("Heap is empty.Cannot peek");
89+
}
90+
return list.get(0);
91+
}
92+
@Override
93+
public String toString() {
94+
return list.toString();
95+
}
96+
public static void main(String[] args) throws Exception {
97+
//Initialize the heap
98+
CustomMinHeap<Integer> minHeap = new CustomMinHeap<>();
99+
//inserting into the heap
100+
minHeap.insert(5);
101+
minHeap.insert(500);
102+
minHeap.insert(10);
103+
minHeap.insert(100);
104+
minHeap.insert(15);
105+
minHeap.insert(20);
106+
//display the heap
107+
System.out.println(minHeap);
108+
//get the element at the top
109+
System.out.println(minHeap.peek());
110+
//remove the min element and print it
111+
System.out.println(minHeap.remove());
112+
//display the heap after removing min element
113+
System.out.println(minHeap);
114+
//display the size of the heap
115+
System.out.println(minHeap.size());
116+
117+
}
118+
}

0 commit comments

Comments
 (0)