From effb467dc03c97b22460909d190e6dd247946a6b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 23 Oct 2022 19:05:25 +0200 Subject: [PATCH] Solved Task 5 --- src/ch/zhaw/ads/TreeTraversal.java | 81 +++++++++++++++++++----------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/src/ch/zhaw/ads/TreeTraversal.java b/src/ch/zhaw/ads/TreeTraversal.java index 1fdecef..99ca0a8 100644 --- a/src/ch/zhaw/ads/TreeTraversal.java +++ b/src/ch/zhaw/ads/TreeTraversal.java @@ -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> implements Traversal { + @SuppressWarnings("unchecked") private final TreeNode root; public TreeTraversal(TreeNode root) { this.root = root; } - @Override - public void interval(T min, T max, Visitor vistor) { + @Override + public void interval(T min, T max, Visitor vis) { + Queue> queue = new LinkedList>(); + queue.add(root); + while(!queue.isEmpty()){ + TreeNode 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 vis) { @@ -31,7 +44,7 @@ public class TreeTraversal> implements Traversal { } public void preorder(Visitor vis) { - Stack> stack = new Stack(); + Stack> stack = new Stack>(); TreeNode node = root; stack.push(node); while(!stack.isEmpty()) { @@ -47,34 +60,42 @@ public class TreeTraversal> implements Traversal { } public void postorder(Visitor vis) { - postorderRec(vis, root); - } - - public void postorderRec(Visitor vis, TreeNode root){ - inorderRec(vis, root.left); - inorderRec(vis, root.right); - vis.visit(root.getValue()); + Stack> stack = new Stack>(); + TreeNode 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 vis){ - List> nodes = new ArrayList<>(); - nodes.add(root); - } - - public void levelorderRec(Visitor vis, List> nodes){ - List> newNodes = new ArrayList<>(); - for (TreeNode node : nodes){ - if(node.right != null){ - newNodes.add(node.right); - } - if(node.left != null) { - newNodes.add(node.left); - } + Queue> queue = new LinkedList>(); + queue.add(root); + while(!queue.isEmpty()){ + TreeNode 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); + } + } } + }