I am working on a custom implementation of Binary heaps. I need a Min and a Max Heap so I can create a "Sale - Buy" bid system.
My problem is that when a new offer(Sale or Buy) comes in and then there is a purchase then the "persons" should be popped out of the heap and the list that keeps track of them. I was wondering how can I keep track of the Keys(the names) in the binary heap.
For example in the outer class I have something like this.
public class OuterClass{
public static HashMap<String, Integer> buyers = new HashMap<>();
public static HashMap<String, Integer> sellers = new HashMap<>();
public static BHeap<String, Integer> buyHeap = new BHeap<>();
public static BHeap<String, Integer> sellHeap = new BHeap<>();
//etc...
}
In my binary heap implementation I have something like this.
public class BHeap <K extends Comparable<? super K>, V extends Comparable<? super V>> {
protected ArrayList<K> keys = new ArrayList<>();
protected ArrayList<V> values = new ArrayList<>();
//do heap stuff
}
When I am comparing the two heaps I go on popping until I find a match(that is buy price >= sell price). But how do I keep track of who (KEY) sold and bought, when only information I can return is the VAL
My solution is when doing
pop()
orpeek()(returns top element)
then the function should return the KEY not the VALUE(but in pop() you remove both). Then on the outer class you keep track of what's happening on the HashMap(which may be redundant, but good to have for another thing)Another solution would be the ArrayList placeholder to be of the type "Person" or "Node". I actually had opted for a tree based solution with
but it was requested specifically to be an array implementation