diff --git a/src/Traversal.java b/src/Traversal.java index 8da0f79..2152708 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,115 @@ +import java.util.LinkedList; +import java.util.Stack; + public class Traversal { public static void main(String[] args) { + TreeNode root = new TreeNode<>(10); + root.left = new TreeNode<>(9); + root.left.left = new TreeNode<>(5); + root.left.right = new TreeNode<>(2); + + root.right = new TreeNode<>(15); + root.right.left = new TreeNode<>(-3); + root.right.right = new TreeNode<>(5); + root.right.right.right = new TreeNode<>(22); + + TreeNode stringRoot = new TreeNode<>("Hello"); + stringRoot.left = new TreeNode<>("hi"); + stringRoot.left.left = new TreeNode<>("what"); + stringRoot.left.right = new TreeNode<>("jessica"); + + stringRoot.right = new TreeNode<>("yes"); + stringRoot.right.left = new TreeNode<>("blue"); + stringRoot.right.right = new TreeNode<>("yellow"); + stringRoot.right.right.right = new TreeNode<>("pink"); + + TreeNode megaRoot = new TreeNode (1); + TreeNode current = megaRoot; + + for(int i=2; i<100_001; i++){ + current.right = new TreeNode(i); + current= current.right; + } + preOrder(megaRoot); + public static void preOrderIterative(TreeNode root){ + + Stack> stack = new Stack<>(); + stack.push(root); + + while (!stack.isEmpty()){ + TreeNode current = stack.pop(); + if(current == null) continue; + //do something + System.out.println(current.value); + stack.push(current.right); + stack.push(current.left); + } + } + + public static void levelOrder(TreeNode root){ + Queue> queue = new LinkedList<>(); + queue.offer(root); + + while(!queue.isEmpty()){ + TreeNode current = queue.remove(); + if(current == null) continue; + System.out.println(current.value); + } + } + //printGreaterThan(root, 4); + preOrder(stringRoot); + + int results = sum(root); + System.out.println(results); + System.err.println(oddSum(root)); + } + + public static void printGreaterThan(TreeNode current, int threshold) { + if (current == null) return; + + if (current.value > threshold) { + System.out.println(current.value); + } + + printGreaterThan(current.left, threshold); + printGreaterThan(current.right, threshold); + } + public static int countNodes(TreeNode current){ + if (current == null) return 0; + int leftCount = countNodes(current.left); + int rightCount = countNodes(current.right); + int totalCount = 1+ leftCount + rightCount; + + return totalCount; + + + + + } + + public static void preOrder(TreeNode current) { + if (current == null) return; + + System.out.println(current.data); + preOrder(current.left); + preOrder(current.right); + } + + public static void postOrder(TreeNode current) { + if (current == null) return; + + postOrder(current.left); + postOrder(current.right); + E myValue = current.value; + System.out.println(myValue); + } + + public static void inOrder(TreeNode current) { + if (current == null) return; + + inOrder(current.left); + System.out.println(current.data); + inOrder(current.right); } -} +} \ No newline at end of file diff --git a/src/TreeNode.java b/src/TreeNode.java index acd9639..9f41e0f 100644 --- a/src/TreeNode.java +++ b/src/TreeNode.java @@ -1,4 +1,21 @@ -public class TreeNode { - + +public class TreeNode { + public T data; + public TreeNode left; + public TreeNode right; + + public TreeNode (T data){ + this.data = data; + } + public TreeNode (T data, TreeNode left){ + this.data = data; + this.left = left; + + } + public TreeNode (T data, TreeNode left, TreeNode right){ + this.data = data; + this.left = left; + this.right = right; + } } \ No newline at end of file