Bonjour,

Je reçois, une par une, un nombre indéterminé et dans le désordre de paires père-fils.
Je dois, avec ces paires, construire un arbre.

Par exemple,
je reçois la paire (1, 2), mon arbre est donc
Puis je reçois la paire (4, 6), mon arbre devient
Puis je reçois la paire (2, 4), mon arbre devient
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
      1
     /
    2
   /
  4
 /
6

Ma classe Node :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public class Node<T> {
	T data;
	Node<T> parent;
	private Map<T, Node<T>> children;
 
	public Node(T data, Node<T> parent) {
		this.data = data;
		this.parent = parent;
		this.parent.children = new HashMap<T, Node<T>>();
		this.parent.children.put(this.data, this);
	}
 
	public Node(T data) {
		this.data = data;
	}
 
	public boolean hasChildren() {
		if (this.children != null) {
			return this.children.size() > 0;
		}
		return false;
	}
 
	public void setParent(Node<T> parent) {
		parent.addChild(this);
		this.parent = parent;
	}
 
	public void addChild(T data) {
		Node<T> child = new Node<T>(data);
		child.setParent(this);
		this.children.put(child.data, child);
	}
 
	public void addChild(Node<T> child) {
		child.setParent(this);
		this.children.put(child.data, child);
	}
}
Dans ma classe utilisatrice, je fais :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
Set<Node<String>> tree = new HashSet<Node<String>>;
Node<String> parent1 = new Node<String>("1");
Node<String> node1 = new Node<String>("2", parent1);
tree.add(node1);
tree.add(parent1);
 
Node<String> parent2 = new Node<String>("4");
Node<String> node2 = new Node<String>("6", parent2);
tree.add(node2);
tree.add(parent2);
 
Node<String> parent3 = new Node<String>("2");
Node<String> node3 = new Node<String>("4", parent3);
tree.add(node3);
tree.add(parent3);
Par contre je n'arrive pas à 'relier' les 2 sous-arbres (1, 2) et (4, 6) quand je dois gérer la paire (2, 4).

Déjà, est-ce que ma structure est la bonne pour faire ce dont j'ai besoin ?
Comment je peux faire ?