-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryMinHeap.java
More file actions
168 lines (150 loc) · 4.91 KB
/
BinaryMinHeap.java
File metadata and controls
168 lines (150 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BinaryMinHeap<T> {
private List<Node> allNodes = new ArrayList<>();
private Map<T,Integer> nodePosition = new HashMap<>();
public class Node {
int weight;
T key;
}
/**
* Checks where the key exists in heap or not
*/
public boolean containsData(T key){
return nodePosition.containsKey(key);
}
/**
* Add key and its weight to they heap
*/
public void add(int weight,T key) {
Node node = new Node();
node.weight = weight;
node.key = key;
allNodes.add(node);
int size = allNodes.size();
int current = size - 1;
int parentIndex = (current - 1) / 2;
nodePosition.put(node.key, current);
while (parentIndex >= 0) {
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);
current = parentIndex;
parentIndex = (parentIndex - 1) / 2;
} else {
break;
}
}
}
/**
* Get the heap min without extracting the key
*/
public T min(){
return allNodes.get(0).key;
}
/**
* Checks with heap is empty or not
*/
public boolean empty(){
return allNodes.size() == 0;
}
/**
* Decreases the weight of given key to 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){
swap(allNodes.get(parent), allNodes.get(position));
updatePositionMap(allNodes.get(parent).key,allNodes.get(position).key,parent,position);
position = parent;
parent = (parent-1)/2;
}else{
break;
}
}
}
/**
* Get the weight of given key
*/
public Integer getWeight(T key) {
Integer position = nodePosition.get(key);
if( position == null ) {
return null;
} else {
return allNodes.get(position).weight;
}
}
/**
* Returns the min node of the heap
*/
public Node extractMinNode() {
int size = allNodes.size() -1;
Node minNode = new Node();
minNode.key = allNodes.get(0).key;
minNode.weight = allNodes.get(0).weight;
int lastNodeWeight = allNodes.get(size).weight;
allNodes.get(0).weight = lastNodeWeight;
allNodes.get(0).key = allNodes.get(size).key;
nodePosition.remove(minNode.key);
nodePosition.remove(allNodes.get(0));
nodePosition.put(allNodes.get(0).key, 0);
allNodes.remove(size);
int currentIndex = 0;
size--;
while(true){
int left = 2*currentIndex + 1;
int right = 2*currentIndex + 2;
if(left > size){
break;
}
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){
swap(allNodes.get(currentIndex), allNodes.get(smallerIndex));
updatePositionMap(allNodes.get(currentIndex).key,allNodes.get(smallerIndex).key,currentIndex,smallerIndex);
currentIndex = smallerIndex;
}else{
break;
}
}
return minNode;
}
/**
* Extract min value key from the heap
*/
public T extractMin(){
Node node = extractMinNode();
return node.key;
}
/*private void printPositionMap(){
System.out.println(nodePosition);
}*/
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){
nodePosition.remove(data1);
nodePosition.remove(data2);
nodePosition.put(data1, pos1);
nodePosition.put(data2, pos2);
}
public void printHeap(){
for(Node n : allNodes){
System.out.println(n.weight + " " + n.key);
}
}
}