package model.graphs;

import java.util.*;
import java.util.Map.Entry;

/**
 * Created by stefanoandriolo on 19/04/17.
 */
public class Graph<T extends Comparable<T>> implements IGraph<T> {

    HashMap<Node<T>, LinkedList<Node<T>>> vertexes;


    public Graph() {
        this.vertexes = new HashMap<>();
    }

    @Override
    public boolean insertNode(Node<T> u) {

        if (!this.vertexes.containsKey(u)) {
            this.vertexes.put(u, new LinkedList<>());
            return true;
        }

        return false;
    }

    @Override
    public void deleteNode(Node<T> u) {
        // TODO: completare, cancellare il nodo e tutti gli archi adiacenti e incidenti
    }

    @Override
    // NOTA: Assumiamo che u e v esistano nel grafo
    public boolean insertEdge(Node<T> u, Node<T> v) {
        if (this.vertexes.containsKey(u) && this.vertexes.containsKey(v)) {
            this.vertexes.get(u).add(v);
            return true;
        }
        
        return false;
    }

    @Override
    public void deleteEdge(Node<T> u, Node<T> v) {
        if (this.vertexes.containsKey(u) && this.vertexes.containsKey(v))
            this.vertexes.get(u).remove(v);
    }

    @Override
    public Set<Node<T>> adj(Node<T> u) {

        HashSet<Node<T>> adjSet = new HashSet<Node<T>>();

        if (this.vertexes.containsKey(u))
            adjSet.addAll(this.vertexes.get(u));

        return adjSet;
    }

    @Override
    public Set<Node<T>> V() {
        return null;
    }

    @Override
    public void print() {
        for (Entry<Node<T>, LinkedList<Node<T>>> e :
                this.vertexes.entrySet()) {
            System.out.print(e.getKey() + " : ");

            for (Node<T> l :
                    e.getValue()) {
                System.out.print(l + " ");
            }

            System.out.println();
        }
    }
    
    
    // ritorna una copia del vettore dei vertici, accessibile solamente alle classi derivate
    // serve per riuscire a costruire il disegno del grafico
    protected HashMap<Node<T>, LinkedList<Node<T>>> getVertexes() {
    	return new HashMap<Node<T>, LinkedList<Node<T>>>(this.vertexes);
    }
    
    
    public Node<T> getMaxKey() {
    	
    	try {
    		// ritorna la chiave con il valore massimo
    		Node<T> maxKey = Collections.max(this.vertexes.keySet());
    		return maxKey;
    	} catch (NoSuchElementException nse) {
    		// se scatta un'eccezione (la collezione è vuota) si ritorna un valore di default
    		return null;
    	}
    }
}

@@ -535,36 +535,55 @@
 %0A%0A    @Override%0A
-    
+%09//TODO: cancellare u%0A%09
 public void dele
@@ -606,100 +606,318 @@
 ) %7B%0A
-        // TODO: completare, cancellare il nodo e tutti gli archi adiacenti e incidenti%0A    
+%09%09%0A%09%09//cancello gli archi uscenti da u%0A%09%09if (this.vertexes.containsKey(u)) %7B%0A%09%09%09this.vertexes.get(u).clear() ;%0A%09%09%09%0A%09%09%09//cancello gli archi incidenti in u%0A%09%09%09for (Node%3CT%3E n : this.vertexes.keySet()) %7B%0A%09%09%09%09%0A%09%09%09%09if (this.vertexes.get(n).contains(u))%0A%09%09%09%09%09this.vertexes.get(n).remove(u) ;%0A%09%09%09%09%09%09%09%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09
 %7D%0A%0A 
@@ -1658,36 +1658,33 @@
 %0A%0A    @Override%0A
-    
+%09
 public Set%3CNode%3C
@@ -1697,33 +1697,162 @@
 ) %7B%0A
-        return null;%0A    
+%09%09%0A%09%09Set%3CNode%3CT%3E%3E nodeSet = new TreeSet%3CNode%3CT%3E%3E() ;%0A%09%09%0A%09%09if (!this.vertexes.isEmpty()) %7B%0A%09%09%09nodeSet = this.vertexes.keySet() ;%0A%09%09%7D%0A%09%09%0A%09%09return nodeSet;%0A%09
 %7D%0A%0A 

Update Graph.java