Skip to content

Created GenericHeap #1320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions DataStructures/Heaps/GenericHeap
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.*;

public class GenericHeap <T extends Comparable <T> >{
ArrayList <T> data=new ArrayList<>();
HashMap<T,Integer> map=new HashMap<>();
public void add(T item) {
this.data.add(item);
map.put(item,this.data.size()-1);//
upHeapify(this.data.size()-1);
}
private void upHeapify(int ci) {
int pi=(ci-1)/2;
if(isLarger(this.data.get(ci),this.data.get(pi))>0) {
swap(pi,ci);
upHeapify(pi);
}
}
public void display() {
System.out.println(this.data);
}
public int size() {
return this.data.size();
}
public boolean isEmpty() {
return this.size()==0;
}
public T remove() {
this.swap(0,this.size()-1);
T rv=this.data.remove(this.size()-1);
downHeapify(0);
map.remove(rv);
return rv;
}
private void downHeapify(int pi) {
int lci=2*pi+1;
int rci=2*pi+2;
int mini=pi;
if(lci<this.size() && isLarger(this.data.get(lci),this.data.get(mini))>0) {
mini=lci;
}
if(rci<this.size() && isLarger(this.data.get(rci),this.data.get(mini))>0) {
mini=rci;
}
if(mini!=pi) {
this.swap(pi,mini);
downHeapify(mini);
}
}
public T get() {
return this.data.get(0);
}
//t has higher property then return +ve
private int isLarger(T t,T o) {
return t.compareTo(o);
}
private void swap(int i,int j) {
T ith=this.data.get(i);
T jth=this.data.get(j);
this.data.set(i,jth);
this.data.set(j,ith);
map.put(ith,j);
map.put(jth,i);
}
public void updatePriority(T item) {
int index=map.get(item);
//because we enter lesser value then old vale
upHeapify(index);
}
}