-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.java
144 lines (123 loc) · 3.73 KB
/
Tree.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Queue;
import java.util.Stack;
import java.util.LinkedList;
import java.util.Iterator;
public abstract class Tree<T extends Comparable<T>> {
TreeNode<T> root;
public void findAllLevels() {
throw new UnsupportedOperationException();
}
// how to add to a tree? it normally depends on the context...
abstract public boolean add(T element);
abstract public boolean remove(T element);
//How to search?? Use BFS or DFS
public boolean contains(T element) {
return iterativeBreadthFirstContains(element);
}
public boolean iterativeBreadthFirstContains(T element) {
if (root == null) {
return false;
}
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
q.add(root);
while (q.isEmpty() == false) {
TreeNode<T> temp = q.remove();
if (temp.obj.compareTo(element) == 0) {
return true;
}
for (TreeNode<T> child : temp.getAllChildren()) {
q.add(child);
}
}
return false;
}
public boolean iterativeDepthFirstContains(T element) {
if (root == null) {
return false;
}
Stack<TreeNode<T>> stack = new Stack<>();
HashSet<TreeNode<T>> seen = new HashSet<>();
stack.push(root);
while (stack.isEmpty() == false) {
TreeNode<T> temp = stack.peek();
if (temp.obj.compareTo(element) == 0) {
return true;
}
seen.add(temp);
boolean found = false;
Iterator<TreeNode<T>> iterator = temp.getAllChildren().iterator();
TreeNode<T> next = null;
while (!found && iterator.hasNext()) {
next = iterator.next();
if (!seen.contains(next)) {
found = true;
}
}
if (!found) {
stack.pop();
} else {
stack.push(next);
}
}
return false;
}
public boolean recursiveDepthFirstContains(T element) {
return depthFirstRecurse(element, root);
}
private boolean depthFirstRecurse(T e, TreeNode<T> n) {
if (n == null) {
return false;
}
if (n.obj.compareTo(e) == 0) {
return true;
}
for (TreeNode<T> child : n.getAllChildren()) {
if (depthFirstRecurse(e, child)) {
return true;
}
}
return false;
}
public static class TreeNode<T> {
T obj;
ArrayList<TreeNode<T>> children;
TreeNode(T o) {
obj = o;
children = new ArrayList<>();
}
boolean add(TreeNode<T> tr) {
return children.add(tr);
}
ArrayList<TreeNode<T>> getAllChildren() {
return children;
}
public int height() {
throw new UnsupportedOperationException();
}
}
public class BreadthFirstIterator implements Iterator<T>, Iterable<T> {
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
BreadthFirstIterator() {
q.add(root);
}
@Override
public boolean hasNext() {
if (root == null) {
return false;
}
return !q.isEmpty();
}
public T next() {
TreeNode<T> temp = q.remove();
for (TreeNode<T> child : temp.getAllChildren()) {
q.add(child);
}
return temp.obj;
}
public Iterator<T> iterator() {
return new BreadthFirstIterator();
}
}
}