-
Notifications
You must be signed in to change notification settings - Fork 1
/
IteratorCodingExercise.java
64 lines (55 loc) · 1.49 KB
/
IteratorCodingExercise.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Given a Node<T>, implement a preorder traversal that returns a sequence of Ts
*/
package behavioral.iterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class TreeNode<T>
{
public T value;
public TreeNode<T> left;
public TreeNode<T> right;
public TreeNode<T> parent;
public TreeNode(T value)
{
this.value = value;
}
public TreeNode(T value, TreeNode<T> left, TreeNode<T> right)
{
this.value = value;
this.left = left;
this.right = right;
left.parent = right.parent = this;
}
private void traverse (TreeNode<T> current,
ArrayList<TreeNode<T>> list)
{
list.add(current);
if(current.left != null)
traverse(current.left, list);
if (current.right != null)
traverse(current.right, list);
}
public Iterator<TreeNode<T>> preOrder()
{
ArrayList<TreeNode<T>> items = new ArrayList<>();
traverse(this, items);
items.forEach((el) -> System.out.println(el.value));
return items.iterator();
}
}
class IteratorExerciseDemo
{
public static void main(String[] args) {
TreeNode<Character> node = new TreeNode<>('a',
new TreeNode<>('b', new TreeNode<>('c'), new TreeNode<>('d')),
new TreeNode<>('e'));
// a
// / \
// b e
// / \
// c d
System.out.println(node.preOrder());
}
}