Solved Task 5

This commit is contained in:
schrom01 2022-10-23 19:05:25 +02:00
parent 1462cdcaba
commit effb467dc0
1 changed files with 51 additions and 30 deletions

View File

@ -1,19 +1,32 @@
package ch.zhaw.ads; package ch.zhaw.ads;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Stack;
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> { public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
@SuppressWarnings("unchecked")
private final TreeNode<T> root; private final TreeNode<T> root;
public TreeTraversal(TreeNode<T> root) { public TreeTraversal(TreeNode<T> root) {
this.root = root; this.root = root;
} }
@Override
public void interval(T min, T max, Visitor<T> vistor) {
@Override
public void interval(T min, T max, Visitor<T> vis) {
Queue<TreeNode<T>> queue = new LinkedList<TreeNode<T>>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode<T> node = queue.poll();
if(node.getValue().compareTo(min) >= 0 && node.getValue().compareTo(max) <= 0)
vis.visit(node.getValue());
if(node.left != null && (node.right == null || node.right.getValue().compareTo(min) >= 0)){
queue.add(node.left);
}
if(node.right != null && (node.left == null || node.left.getValue().compareTo(max) <= 0)){
queue.add(node.right);
}
}
} }
public void inorder(Visitor<T> vis) { public void inorder(Visitor<T> vis) {
@ -31,7 +44,7 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
} }
public void preorder(Visitor<T> vis) { public void preorder(Visitor<T> vis) {
Stack<TreeNode<T>> stack = new Stack(); Stack<TreeNode<T>> stack = new Stack<TreeNode<T>>();
TreeNode<T> node = root; TreeNode<T> node = root;
stack.push(node); stack.push(node);
while(!stack.isEmpty()) { while(!stack.isEmpty()) {
@ -47,34 +60,42 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
} }
public void postorder(Visitor<T> vis) { public void postorder(Visitor<T> vis) {
postorderRec(vis, root); Stack<TreeNode<T>> stack = new Stack<TreeNode<T>>();
TreeNode<T> root = this.root;
do {
while(root != null){
if(root.right != null) {
stack.push(root.right);
} }
stack.push(root);
public void postorderRec(Visitor<T> vis, TreeNode<T> root){ root = root.left;
inorderRec(vis, root.left); }
inorderRec(vis, root.right); root = stack.pop();
if(root.right != null && !stack.isEmpty() && root.right == stack.peek()){
stack.pop();
stack.push(root);
root = root.right;
} else {
vis.visit(root.getValue()); vis.visit(root.getValue());
root = null;
}
} while(!stack.isEmpty());
} }
public void levelorder(Visitor<T> vis){ public void levelorder(Visitor<T> vis){
List<TreeNode<T>> nodes = new ArrayList<>(); Queue<TreeNode<T>> queue = new LinkedList<TreeNode<T>>();
nodes.add(root); queue.add(root);
} while(!queue.isEmpty()){
TreeNode<T> node = queue.poll();
public void levelorderRec(Visitor<T> vis, List<TreeNode<T>> nodes){
List<TreeNode<T>> newNodes = new ArrayList<>();
for (TreeNode<T> node : nodes){
if(node.right != null){
newNodes.add(node.right);
}
if(node.left != null) {
newNodes.add(node.left);
}
vis.visit(node.getValue()); vis.visit(node.getValue());
if(node.left != null){
queue.add(node.left);
} }
if(newNodes.size() > 0){ if(node.right != null){
levelorderRec(vis, newNodes); queue.add(node.right);
} }
} }
}
} }