diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c995aa5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/src/Traversal.java b/src/Traversal.java index 8da0f79..f2c7771 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,220 @@ +import static org.junit.jupiter.api.DynamicTest.stream; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Map; +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("are", null, null); + stringRoot.left.right = new TreeNode("you", null, null); + + stringRoot.right = new TreeNode("I", null, null); + stringRoot.right.left = new TreeNode("am", 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; + } + + System.out.println("Preorder recursive"); + preorder(root); + System.out.println("Preorder iterative"); + perOrderIter(root); + System.out.println("Preorder iterative"); + levelOrder(root); + // preorder(root); + // postorder(root); + // inorder(stringRoot); + // printGreater(root, 7); + // Map counts = new HashMap<>(); + // System.out.println(toMap(root)); + } + + //print a tree roted at the given node in pre-order + public static void preorder(TreeNode node) { + if (node == null) { + return; + } + //print value + System.out.println(node.value); + //traverse left + preorder(node.left); + //traverse right + preorder(node.right); + + } + + public static void perOrderIter(TreeNode node) { + Stack> stack = new Stack<>(); + // TreeNode current = node; + + stack.push(node); + + while (!stack.isEmpty()) { + TreeNode current = stack.pop(); + + if (current == null) { + continue; + } + + System.out.println(current.value); + stack.push(current.right); + stack.push(current.left); + } + } + + public static void levelOrder(TreeNode node) { + Queue> queue = new LinkedList<>(); + + queue.add(node); + + while (!queue.isEmpty()) { + TreeNode current = queue.poll(); + + if (current == null) { + continue; + } + + System.out.println(current.value); + queue.add(current.left); + queue.add(current.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 those 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; + } + int leftCount = countNodes(node.left); + int rightCount = countNodes(node.right); + int overallCount = leftCount + rightCount + 1; + + return overallCount; + //return node == null ? 0 : 1 + countNodes(node.left) + countNodes(node.right); + // return countNodes(node.left) + countNodes(node.right) + 1; + } + + public static void printGreater(TreeNode node, int threshold) { + if (node == null) { + return; + } + //print value + if (node.value > threshold) { + System.out.println(node.value); + } + + //traverse left + printGreater(node.left, threshold); + //traverse right + 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); } } + +/** + * A utility class for performing operations on a binary tree of Strings. + */ +public class QuickCheck { + + /** + * Prints all strings longer than 7 characters in a tree. + * The strings should be printed in pre-order. + * Each qualifying string should appear on its own line. + * + * If the node itself is null, the method should print nothing. + * + * You can assume that there are no null Strings stored in the tree nodes. + * + * @param node the root node of the binary tree + */ + public static void printLongerThan7(TreeNode node) { + if (node == null) { + return; + } + + if ((node.value).length() > 7) { + System.out.println(node.value); + } + + printLongerThan7(node.left); + printLongerThan7(node.right); + + } + + /** + * Returns the sum of all nodes holding odd numbers. + * + * If the node itself is null, return 0. + * + * You can assume there are no null Integers held in the values. + * + * @param node the root node of the binary tree + * @return the sum of all odd-valued nodes + */ + public static int oddSum(TreeNode node) { + if (node == null) { + return 0; + } + + int left = oddSum(node.left); + int right = oddSum(node.right); + + if (node.value % 2 != 0) { + return left + right + node.value; + } + return left + right; + + + } +} \ No newline at end of file diff --git a/src/TreeNode.java b/src/TreeNode.java index acd9639..f230dc8 100644 --- a/src/TreeNode.java +++ b/src/TreeNode.java @@ -1,4 +1,11 @@ -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