diff --git a/src/Traversal.java b/src/Traversal.java index 8da0f79..1ff5448 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,164 @@ +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Stack; + public class Traversal { public static void main(String[] args) { + TreeNode root = new TreeNode(10, null, null); + root.left = new TreeNode(9, null, null); + root.left.left = new TreeNode(5, null, null); + root.left.right = new TreeNode(2, null, null); + + root.right = new TreeNode(15, null, null); + root.right.left = new TreeNode(-3, null, null); + root.right.right = new TreeNode(5, null, null); + root.right.right.right = new TreeNode(22, null, null); + + TreeNode stringRoot = new TreeNode("hello", null, null); + stringRoot.left = new TreeNode("how", null, null); + stringRoot.left.left = new TreeNode("hello", null, null); + stringRoot.left.right = new TreeNode("you", null, null); + + stringRoot.right = new TreeNode("I", null, null); + stringRoot.right.left = new TreeNode("I", null, null); + stringRoot.right.right = new TreeNode("so", null, null); + stringRoot.right.right.right = new TreeNode("good", null, null); + + TreeNode megaRoot = new TreeNode(1, null, null); + TreeNode current = megaRoot; + + for (int i = 2; i <= 5000; i++) { + current.right = new TreeNode(i, null, null); + current = current.right; + } + + // preorder(root); + // postorder(root); + // inorder(root); + // printGreater(root, 4); + // System.out.println(countNodes(root)); + // Map counts = new HashMap<>(); + // System.out.println(toMap(stringRoot)); + // preOrderIter(megaRoot); + + System.out.println("Preorder recuresive"); + preorder(root); + System.out.println("Preorder iterative"); + // preOrderIter(root); + leverOrder(root); + + } + + public static void leverOrder(TreeNode node) { + Queue> queue = new LinkedList<>(); + + queue.add(node); + while (!queue.isEmpty()) { + TreeNode current = queue.poll(); + + if (current == null) { + continue; // move to the top of the loop + } + + System.out.println(current.value); + queue.add(current.left); + queue.add(current.right); + + } + + } + + public static void preOrderIter(TreeNode node) { + Stack> stack = new Stack<>(); + + stack.push(node); + while (!stack.empty()) { + TreeNode current = stack.pop(); + + if (current == null) { + continue; // move to the top of the loop + } + + System.out.println(current.value); + stack.push(current.right); + stack.push(current.left); + + } + + } + + // print tree rooted at the given node in pre-order + public static void preorder(TreeNode node) { + + if (node == null) + return; + System.out.println(node.value); + preorder(node.left); + preorder(node.right); + + } + + public static Map toMap(TreeNode node) { + Map counts = new HashMap<>(); + toMap(node, counts); + return counts; + + } + + private static void toMap(TreeNode node, Map counts) { + + if (node == null) + return; + // Fill up the counts + counts.put(node.value, counts.getOrDefault(node.value, 0) + 1); + toMap(node.left, counts); + toMap(node.right, counts); + + } + + public static int countNodes(TreeNode node) { + // if (node == null) return 0; + // if (node == null) + // return 0; + return node == null ? 0 : countNodes(node.left) + countNodes(node.right) + 1; + // int leftCount = countNodes(node.left); + // int rightCount = countNodes(node.right); + // int overAllCount = leftCount + rightCount + 1; + // return overAllCount; } + + public static void printGreater(TreeNode node, int threshold) { + + if (node == null) + return; + if (node.value > threshold) { + System.out.println(node.value); + } + printGreater(node.left, threshold); + printGreater(node.right, threshold); + + } + + public static void postorder(TreeNode node) { + + if (node == null) + return; + postorder(node.left); + postorder(node.right); + System.out.println(node.value); + + } + + public static void inorder(TreeNode node) { + if (node == null) + return; + inorder(node.left); + System.out.println(node.value); + inorder(node.right); + + } + } diff --git a/src/TreeNode.java b/src/TreeNode.java index acd9639..0b1fc35 100644 --- a/src/TreeNode.java +++ b/src/TreeNode.java @@ -1,4 +1,13 @@ -public class TreeNode { +public class TreeNode { + + public T value; + public TreeNode left; + public TreeNode right; + + public TreeNode(T value, TreeNode left, TreeNode right) { + this.value = value; + this.left = left; + this.right = right; + } - } \ No newline at end of file