Let's assume I'm given a map represented as {[A, 0], [B, 1] [C, 2], [D, 3], [E, 4], [F, 5], [G, 6]} and were to delete one of its elements, i.e [E, 4], then my new map would become {[A, 0], [B, 1] [C, 2], [D, 3], [F, 5], [G, 6]}. How would I resize or shrink this map such that the elements whose index is greater than the index of the deleted element, would decrement (shrink) their index (Value) by one, like this {[A, 0], [B, 1] [C, 2], [D, 3], [F, 4], [G, 5]}? In addition to this, how would I adjust the size of ArrayList<ArrayList<GraphEdge<L>>>
matrix?
Note: The assignment of the index to the nodes must follow the order of insertion, therefore the first node inserted in the graph must have assigned the index 0, the second the index 1 and so on.
The Java code:
@Override
public void removeNode(GraphNode<L> node) {
if (node == null) {
throw new NullPointerException();
}
if (!this.nodesIndex.containsKey(node)) {
throw new IllegalArgumentException();
}
// Implement here
}
@Override
public boolean addNode(GraphNode<L> node) {
if (node == null) {
throw new NullPointerException();
}
if (this.nodesIndex.containsKey(node)) {
return false;
}
// Implement here
}
The map which contains its elements (Keys) associated to their indexes (Values) is represented as following:
/*
* A set of nodes in which every node is associated to its index in the adjacency
* matrix
*/
protected Map<GraphNode<L>, Integer> nodesIndex = new HashMap<GraphNode<L>, Integer>();
Whereas the matrix is represented like this:
/*
* The adjacency matrix in which its elements are null or objects of the class
* GraphEdge<L>. The use of ArrayList allows the matrix to resize
* gradually whenever a new object (element) is added or removed
* (deleted).
*/
protected ArrayList<ArrayList<GraphEdge<L>>> matrix = new ArrayList<ArrayList<GraphEdge<L>>>();