Allow traversing intervals

This commit is contained in:
Manuel Thalmann 2022-10-18 14:29:29 +02:00
parent 630a5c8acb
commit e4c4da8fb9

View file

@ -52,7 +52,7 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
queue.add(root); queue.add(root);
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
TreeNode<T> node = queue.poll(); TreeNode<T> node = queue.pop();
if (node.left != null) { if (node.left != null) {
queue.add(node.left); queue.add(node.left);
@ -67,8 +67,27 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
} }
@Override @Override
public void interval(T min, T max, Visitor<T> vistor) { public void interval(T min, T max, Visitor<T> visitor) {
// TODO Auto-generated method stub Deque<TreeNode<T>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode<T> node = queue.pop();
if (node != null) {
T value = node.getValue();
if (value.compareTo(min) >= 0 && value.compareTo(max) <= 0) {
visitor.visit(value);
queue.add(node.left);
queue.add(node.right);
} else if (value.compareTo(max) > 0) {
queue.add(node.left);
}
else if (value.compareTo(min) < 0) {
queue.add(node.right);
}
}
}
} }
} }