SantoshSingh
BAN USER
- 0of 0 votes
AnswersGiven Two classes A & B. How will B know if an instance of A is already created?
- SantoshSingh in India| Report Duplicate | Flag | PURGE
Oracle SDE-2 Java
Priority Queue Interface
package priorityQueue;
public interface PriorityQueue<K,V>{
public int size();
public boolean isEmpty();
public Entry<K,V> min() throws EmptyPriorityQueueException;
public Entry<K, V> insert(K key, V value) throws UnComparableKeyException;
public Entry<K, V> removeMin() throws EmptyPriorityQueueException;
}
Heap Based Priority Queue
--------------------------------------
public class HeapPriorityQueue<K, V> implements PriorityQueue<K, V> {
Heap<K, V> heap;
public HeapPriorityQueue() {
super();
heap = new Heap<>();
}
public HeapPriorityQueue(Comparator<? super K> comparator) {
super();
heap = new Heap<>(comparator);
}
@Override
public int size() {
return heap.size();
}
@Override
public boolean isEmpty() {
return heap.isEmpty();
}
@Override
public Entry<K, V> min() throws EmptyPriorityQueueException {
return heap.getMin();
}
@Override
public Entry<K, V> insert(K key, V value) throws UnComparableKeyException {
return heap.add(key, value);
}
@Override
public Entry<K, V> removeMin() throws EmptyPriorityQueueException {
return heap.removeMin();
}
}
Heap Implementation using Complete Binary Tree
-----------------------------------
public class Heap<K, V> {
ArrayListCompleteBinaryTree<Entry<K, V>> completeBinaryTree = new ArrayListCompleteBinaryTree<Entry<K, V>>();
Comparator<? super K> comparator;
public Heap(Comparator<? super K> comparator){
this.comparator = comparator;
}
public Heap(){
comparator = null;
}
public Entry<K, V> add(K key, V value) throws UnComparableKeyException{
return add(new Entry<K, V>(key, value));
}
public Entry<K, V> add(Entry<K, V> entry) throws UnComparableKeyException{
// handle case of comparable interface as well.
if (isEmpty()){
return completeBinaryTree.add(entry).getElement();
}
ArrayListCompleteBinaryTreeNode<Entry<K, V>> addedEntry = completeBinaryTree.add(entry); // adds element to the end.
K addedKey = addedEntry.getElement().getKey();
ArrayListCompleteBinaryTreeNode<Entry<K, V>> parentNode = completeBinaryTree.getParentNode(addedEntry);
K parentKey = parentNode.getElement().getKey();
while(addedKeyLessThanParentKey(addedKey, parentKey)){
addedEntry = completeBinaryTree.swap(addedEntry,parentNode); // swap and get new position of added entry.
parentNode = completeBinaryTree.getParentNode(addedEntry);
if (parentNode == null){
break;
}
parentKey = parentNode.getElement().getKey();
}
return addedEntry.getElement();
}
public Entry<K, V> removeMin() throws EmptyPriorityQueueException{
throw new EmptyPriorityQueueException("Not yet implemented");
}
public Entry<K, V> getMin(){
if (isEmpty()){
return null;
}
return completeBinaryTree.getRootElement();
}
public boolean isEmpty(){
return completeBinaryTree.isEmpty();
}
private boolean addedKeyLessThanParentKey(K addedKey, K parentKey) throws UnComparableKeyException {
if (comparator == null){
if (addedKey instanceof Comparable<?> && parentKey instanceof Comparable<?>){
@SuppressWarnings("unchecked")
Comparable<K> compatableAddedKey = (Comparable<K>)addedKey;
if (compatableAddedKey.compareTo(parentKey) < 0){
return true;
} else {
return false;
}
}else {
throw new UnComparableKeyException();
}
} else {// use comparator
if (comparator.compare(addedKey, parentKey) < 0){
return true;
} else {
return false;
}
}
}
public int size() {
return completeBinaryTree.size();
}
}
Array based Complete Binary Tree
---------------------------------------------
public class ArrayListCompleteBinaryTree<E> implements CompleteBinaryTree<E>{
ArrayList<ArrayListCompleteBinaryTreeNode<E>> arrayList = new ArrayList<ArrayListCompleteBinaryTreeNode<E>>();
public ArrayListCompleteBinaryTree() {
super();
arrayList.add(0, null);
}
@Override
public ArrayListCompleteBinaryTreeNode<E> add(E element) {
int addIndex = arrayList.size();
ArrayListCompleteBinaryTreeNode<E> treeNode = new ArrayListCompleteBinaryTreeNode<E>(addIndex, element);
arrayList.add(addIndex, treeNode);
return treeNode;
}
@Override
public E remove() {
ArrayListCompleteBinaryTreeNode<E> lastItem = arrayList.remove(arrayList.size()-1);
return lastItem.getElement();
}
public E getRootElement(){
return arrayList.get(1).getElement();
}
public ArrayListCompleteBinaryTreeNode<E> getRootNode(){
return arrayList.get(1);
}
public ArrayListCompleteBinaryTreeNode<E> getParentNode(ArrayListCompleteBinaryTreeNode<E> node){
if (isRoot(node)){
return null;
}
int pos = node.getIndex();
int parentPos = 0;
if ((pos%2) == 0){ //it is even ie, left child
parentPos = pos/2;
} else {
parentPos = (pos - 1)/2;
}
return arrayList.get(parentPos);
}
public boolean isEmpty() {
return (arrayList.size() == 1 ? true : false);
}
public ArrayListCompleteBinaryTreeNode<E> swap(ArrayListCompleteBinaryTreeNode<E> node1, ArrayListCompleteBinaryTreeNode<E> node2){
int node1Index = node1.getIndex();
int node2Index = node2.getIndex();
node1.setIndex(node2Index);
node2.setIndex(node1Index);
arrayList.set(node1Index, node2);
arrayList.set(node2Index, node1);
return node1;
}
public boolean isRoot(ArrayListCompleteBinaryTreeNode<E> node){
if (node.getIndex() == 1){
return true;
}else {
return false;
}
}
public int size() {
return arrayList.size() - 1;
}
}
Complete Binary Tree Node
-------------------------------------
public class ArrayListCompleteBinaryTreeNode<E>{
int index;
E element;
public ArrayListCompleteBinaryTreeNode() {
index = -1;
element = null;
}
public ArrayListCompleteBinaryTreeNode(int index, E element){
this.index = index;
this.element = element;
}
public void setIndex(int index){
this.index = index;
}
public int getIndex(){
return index;
}
public E getElement(){
return element;
}
}
I used default hashCode method along with MAD (Multiply Add and Divide) to get hash Value. Collision resolved by Separate chaining. Remove is not implemented but could be implemented as add.
- SantoshSingh June 02, 2015
A valid comparator needs to be implemented and passed to the e constructor of Priority queue.Your comparator should define the ordering you want, otherwise natural ordering will be used.
- SantoshSingh June 04, 2015