Solved Task 5
This commit is contained in:
parent
1462cdcaba
commit
effb467dc0
|
@ -1,19 +1,32 @@
|
|||
package ch.zhaw.ads;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.*;
|
||||
|
||||
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
||||
@SuppressWarnings("unchecked")
|
||||
private final TreeNode<T> root;
|
||||
|
||||
public TreeTraversal(TreeNode<T> 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) {
|
||||
|
@ -31,7 +44,7 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
|||
}
|
||||
|
||||
public void preorder(Visitor<T> vis) {
|
||||
Stack<TreeNode<T>> stack = new Stack();
|
||||
Stack<TreeNode<T>> stack = new Stack<TreeNode<T>>();
|
||||
TreeNode<T> node = root;
|
||||
stack.push(node);
|
||||
while(!stack.isEmpty()) {
|
||||
|
@ -47,34 +60,42 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
|||
}
|
||||
|
||||
public void postorder(Visitor<T> vis) {
|
||||
postorderRec(vis, root);
|
||||
}
|
||||
|
||||
public void postorderRec(Visitor<T> vis, TreeNode<T> root){
|
||||
inorderRec(vis, root.left);
|
||||
inorderRec(vis, root.right);
|
||||
vis.visit(root.getValue());
|
||||
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);
|
||||
root = root.left;
|
||||
}
|
||||
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());
|
||||
root = null;
|
||||
}
|
||||
} while(!stack.isEmpty());
|
||||
}
|
||||
|
||||
public void levelorder(Visitor<T> vis){
|
||||
List<TreeNode<T>> nodes = new ArrayList<>();
|
||||
nodes.add(root);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
Queue<TreeNode<T>> queue = new LinkedList<TreeNode<T>>();
|
||||
queue.add(root);
|
||||
while(!queue.isEmpty()){
|
||||
TreeNode<T> node = queue.poll();
|
||||
vis.visit(node.getValue());
|
||||
}
|
||||
if(newNodes.size() > 0){
|
||||
levelorderRec(vis, newNodes);
|
||||
}
|
||||
if(node.left != null){
|
||||
queue.add(node.left);
|
||||
}
|
||||
if(node.right != null){
|
||||
queue.add(node.right);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue