Skip to content
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

checkin #309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 58 additions & 52 deletions src/com/interview/graph/BinaryMinHeap.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@

/**
* Date 04/06/2013
*
* @author Tushar Roy
*
* Data structure to support following operations
* extracMin - O(logn)
* addToHeap - O(logn)
* containsKey - O(1)
* decreaseKey - O(logn)
* getKeyWeight - O(1)
* Data structure to support following operations extracMin - O(logn)
* addToHeap - O(logn) containsKey - O(1) decreaseKey - O(logn)
* getKeyWeight - O(1)
*
* It is a combination of binary heap and hash map
* It is a combination of binary heap and hash map
*
*/
public class BinaryMinHeap<T> {

private List<Node> allNodes = new ArrayList<>();
private Map<T,Integer> nodePosition = new HashMap<>();
private Map<T, Integer> nodePosition = new HashMap<>();

public class Node {
int weight;
T key;
Expand All @@ -32,14 +30,14 @@ public class Node {
/**
* Checks where the key exists in heap or not
*/
public boolean containsData(T key){
public boolean containsData(T key) {
return nodePosition.containsKey(key);
}

/**
* Add key and its weight to they heap
*/
public void add(int weight,T key) {
public void add(int weight, T key) {
Node node = new Node();
node.weight = weight;
node.key = key;
Expand All @@ -53,8 +51,8 @@ public void add(int weight,T key) {
Node parentNode = allNodes.get(parentIndex);
Node currentNode = allNodes.get(current);
if (parentNode.weight > currentNode.weight) {
swap(parentNode,currentNode);
updatePositionMap(parentNode.key,currentNode.key,parentIndex,current);
swap(parentNode, currentNode);
updatePositionMap(parentNode.key, currentNode.key, parentIndex, current);
current = parentIndex;
parentIndex = (parentIndex - 1) / 2;
} else {
Expand All @@ -66,31 +64,31 @@ public void add(int weight,T key) {
/**
* Get the heap min without extracting the key
*/
public T min(){
public T min() {
return allNodes.get(0).key;
}

/**
* Checks with heap is empty or not
*/
public boolean empty(){
public boolean empty() {
return allNodes.size() == 0;
}

/**
* Decreases the weight of given key to newWeight
*/
public void decrease(T data, int newWeight){
public void decrease(T data, int newWeight) {
Integer position = nodePosition.get(data);
allNodes.get(position).weight = newWeight;
int parent = (position -1 )/2;
while(parent >= 0){
if(allNodes.get(parent).weight > allNodes.get(position).weight){
int parent = (position - 1) / 2;
while (parent >= 0) {
if (allNodes.get(parent).weight > allNodes.get(position).weight) {
swap(allNodes.get(parent), allNodes.get(position));
updatePositionMap(allNodes.get(parent).key,allNodes.get(position).key,parent,position);
updatePositionMap(allNodes.get(parent).key, allNodes.get(position).key, parent, position);
position = parent;
parent = (parent-1)/2;
}else{
parent = (parent - 1) / 2;
} else {
break;
}
}
Expand All @@ -101,7 +99,7 @@ public void decrease(T data, int newWeight){
*/
public Integer getWeight(T key) {
Integer position = nodePosition.get(key);
if( position == null ) {
if (position == null) {
return null;
} else {
return allNodes.get(position).weight;
Expand All @@ -112,7 +110,7 @@ public Integer getWeight(T key) {
* Returns the min node of the heap
*/
public Node extractMinNode() {
int size = allNodes.size() -1;
int size = allNodes.size() - 1;
Node minNode = new Node();
minNode.key = allNodes.get(0).key;
minNode.weight = allNodes.get(0).weight;
Expand All @@ -127,73 +125,81 @@ public Node extractMinNode() {

int currentIndex = 0;
size--;
while(true){
int left = 2*currentIndex + 1;
int right = 2*currentIndex + 2;
if(left > size){
while (true) {
int left = 2 * currentIndex + 1;
int right = 2 * currentIndex + 2;
if (left > size) {
break;
}
if(right > size){
if (right > size) {
right = left;
}
int smallerIndex = allNodes.get(left).weight <= allNodes.get(right).weight ? left : right;
if(allNodes.get(currentIndex).weight > allNodes.get(smallerIndex).weight){
if (allNodes.get(currentIndex).weight > allNodes.get(smallerIndex).weight) {
swap(allNodes.get(currentIndex), allNodes.get(smallerIndex));
updatePositionMap(allNodes.get(currentIndex).key,allNodes.get(smallerIndex).key,currentIndex,smallerIndex);
updatePositionMap(allNodes.get(currentIndex).key, allNodes.get(smallerIndex).key, currentIndex,
smallerIndex);
currentIndex = smallerIndex;
}else{
} else {
break;
}
}
return minNode;
}

/**
* Extract min value key from the heap
*/
public T extractMin(){
public T extractMin() {
Node node = extractMinNode();
return node.key;
}

private void printPositionMap(){
private void printPositionMap() {
System.out.println(nodePosition);
}

private void swap(Node node1,Node node2){
private void swap(Node node1, Node node2) {
int weight = node1.weight;
T data = node1.key;

node1.key = node2.key;
node1.weight = node2.weight;

node2.key = data;
node2.weight = weight;
}

private void updatePositionMap(T data1, T data2, int pos1, int pos2){
private void updatePositionMap(T data1, T data2, int pos1, int pos2) {
nodePosition.remove(data1);
nodePosition.remove(data2);
nodePosition.put(data1, pos1);
nodePosition.put(data2, pos2);
}
public void printHeap(){
for(Node n : allNodes){

public void printHeap() {
for (Node n : allNodes) {
System.out.println(n.weight + " " + n.key);
}
}
public static void main(String args[]){

public static void main(String args[]) {
BinaryMinHeap<String> heap = new BinaryMinHeap<String>();
heap.add(3, "Tushar");
heap.add(4, "Ani");
heap.add(8, "Vijay");
heap.add(10, "Pramila");
heap.add(5, "Roy");
heap.add(6, "NTF");
heap.add(2,"AFR");
heap.decrease("Pramila", 1);
// heap.add(3, "Tushar");
// heap.add(4, "Ani");
// heap.add(8, "Vijay");
// heap.add(10, "Pramila");
// heap.add(5, "Roy");
// heap.add(6, "NTF");
// heap.add(2,"AFR");
// heap.decrease("Pramila", 1);
// heap.printHeap();
// heap.printPositionMap();
heap.add(5, "Amira");
heap.add(3, "Afsar");
heap.add(2, "Ayaha");
heap.add(1, "Abdul");
heap.add(4, "Afraz");
heap.printHeap();
heap.printPositionMap();
}
}