Skip to content

Instantly share code, notes, and snippets.

@devnacho
Created October 4, 2012 20:27
Show Gist options
  • Save devnacho/3836211 to your computer and use it in GitHub Desktop.
Save devnacho/3836211 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
public class Node {
private HashMap<String, Node> nodes;
private String name;
// Chraga los datos del objeto.
public Node(String name) {
this.nodes = new HashMap<String, Node>();
this.name = name;
}
// tenemos alogo tipo [/, com, google, www]
// el com existe, creamos el 'google' y lo añadimos al 'com',
// depues igual con el 'www'
public synchronized void add_Child(String[] fils) {
if (!this.nodes.containsKey(fils[0])) {
// The child does not exists
this.nodes.put(fils[0], new Node(fils[0]));
}
// Now the child exists
if (fils.length>1) {
String[] newChild = new String[fils.length-1];
System.arraycopy(fils, 1, newChild, 0, fils.length-1);
this.nodes.get(fils[0]).add_Child(newChild);
}
}
public String getNom() {
return name;
}
public void display(String start) {
System.out.println(start + name);
for (Node noeud : nodes.values()) {
noeud.display(start.replace('|', ' ').replace('-', ' ') + "|- ");
}
}
public String toString(String debut){
String text = '\n' + name;
for (Node current_node : nodes.values()) {
System.out.println(text);
text.concat('\n' + current_node.toString(debut.replace('|', ' ').replace('-', ' ') + "|- "));
System.out.println(text);
}
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment