diff --git a/src/Traversal.java b/src/Traversal.java index 8da0f79..6b45ddb 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,138 @@ +import java.util.*; public class Traversal { public static void main(String[] args) { + TreeNode root = new TreeNode(10, new TreeNode(9, new TreeNode(5, null, null), new TreeNode(2, null, null)), new TreeNode(15, new TreeNode(-3, null, null), new TreeNode(5, null, new TreeNode(22, null, null)))); + + // preorder(root); + // System.out.println(); + // postorder(root); + // System.out.println(); + // inorder(root); + // System.out.println(); + TreeNode stringRoot = new TreeNode("Hello", new TreeNode("Hello", new TreeNode("am", null, null), new TreeNode("am", null, null)), new TreeNode("I", new TreeNode("am", null, null), new TreeNode("so", null, 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(megaRoot); + + levelOrder(root); + + // System.out.println("Preorder Recursive: "); + // preorder(root); + // System.out.println(); + // System.out.println("Preorder Iterative"); + // preOrderIter(megaRoot); + // System.out.println(); + + // System.out.println(); + + // preorder(stringRoot); + // System.out.println(); + // postorder(stringRoot); + // System.out.println(); + // inorder(stringRoot); + // System.out.println(); + + // printGreater(root, 10); + // System.out.println(); + + // System.out.println(countNodes(root)); + // System.out.println(toMap(stringRoot)); + + } + + 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.print(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.isEmpty()) { + TreeNode current = stack.pop(); + if (current == null) continue; + System.out.print(current.value + " "); + stack.push(current.right); + stack.push(current.left); + } + } + + 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; + } + counts.put(node.value, counts.getOrDefault(node.value, 0) + 1); + toMap(node.left, counts); + toMap(node.right, counts); + } + + public static int countNodes(TreeNode node) { + int count = 0; + if (node == null) { + return 0; + } else { + count++; + count += (countNodes(node.right) + countNodes(node.left)); + } + return count; + } + + public static void printGreater(TreeNode node, int threshold) { + if (node == null) return; + if (node.value > threshold) { + System.out.print(node.value + " "); + } + printGreater(node.left, threshold); + printGreater(node.right, threshold); + } + + public static void preorder(TreeNode node) { + if (node == null) return; + System.out.print(node.value + " "); + preorder(node.left); + preorder(node.right); + } + + public static void postorder(TreeNode node) { + if (node == null) return; + postorder(node.left); + postorder(node.right); + System.out.print(node.value + " "); + } + + public static void inorder(TreeNode node) { + if (node == null) return; + inorder(node.left); + System.out.print(node.value + " "); + inorder(node.right); } } diff --git a/src/TreeNode.java b/src/TreeNode.java index acd9639..7b335f0 100644 --- a/src/TreeNode.java +++ b/src/TreeNode.java @@ -1,4 +1,17 @@ -public class TreeNode { +public class TreeNode { + // -- Instance fields -- - + // The value of the node + public T value; + + // Left and right pointers + public TreeNode left; + public TreeNode right; + + // -- Contructors -- + public TreeNode(T value, TreeNode left, TreeNode right) { + this.value = value; + this.left = left; + this.right = right; + } } \ No newline at end of file