-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
48 lines (37 loc) · 1.52 KB
/
App.java
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
package datastructures;
import java.util.Iterator;
public class App {
public static void main(String[] args) {
Tree tree = new Tree();
/*
* The second parameter for the addNode method is the identifier
* for the node's parent. In the case of the root node, either
* null is provided or no second parameter is provided.
*/
tree.addNode("Harry");
tree.addNode("Jane", "Harry");
tree.addNode("Bill", "Harry");
tree.addNode("Joe", "Jane");
tree.addNode("Diane", "Jane");
tree.addNode("George", "Diane");
tree.addNode("Mary", "Diane");
tree.addNode("Jill", "George");
tree.addNode("Carol", "Jill");
tree.addNode("Grace", "Bill");
tree.addNode("Mark", "Jane");
tree.display("Harry");
System.out.println("\n***** DEPTH-FIRST ITERATION *****");
// Default traversal strategy is 'depth-first'
Iterator<Node> depthIterator = tree.iterator("Harry");
while (depthIterator.hasNext()) {
Node node = depthIterator.next();
System.out.println(node.getIdentifier());
}
System.out.println("\n***** BREADTH-FIRST ITERATION *****");
Iterator<Node> breadthIterator = tree.iterator("Harry", TraversalStrategy.BREADTH_FIRST);
while (breadthIterator.hasNext()) {
Node node = breadthIterator.next();
System.out.println(node.getIdentifier());
}
}
}