From 23d5b90298cf433d6065ad430b9b1b98e3c17eab Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Thu, 12 Feb 2026 22:51:11 -0800 Subject: [PATCH 1/4] finished livecode --- src/Traversal.java | 41 +++++++++++++++++++++++++++++++++++++++++ src/TreeNode.java | 9 ++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Traversal.java b/src/Traversal.java index 8da0f79..836e255 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,46 @@ 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); + + // preorder(root); + // postorder(root); + inorder(root); + } + + // Print a tree rooted at the given node in pre-order + public static void preorder(TreeNode node) { + // if null, return + if(node == null) { + return; + } + // print value + System.out.println(node.value); + // traverse left + preorder(node.left); + // traverse right + preorder(node.right); + } + + 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..35daf2e 100644 --- a/src/TreeNode.java +++ b/src/TreeNode.java @@ -1,4 +1,11 @@ public class TreeNode { - + public int value; + public TreeNode left; + public TreeNode right; + public TreeNode(int value, TreeNode left, TreeNode right) { + this.value = value; + this.left = left; + this.right = right; + } } \ No newline at end of file From c59782876e6695b62b5f16189f53ce68c60c171b Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Tue, 17 Feb 2026 19:29:24 -0800 Subject: [PATCH 2/4] push --- src/Traversal.java | 50 +++++++++++++++++++++++++++++++++++----------- src/TreeNode.java | 10 +++++----- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/Traversal.java b/src/Traversal.java index 836e255..6ec2772 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,22 +1,48 @@ 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); + TreeNode root = new TreeNode(10, 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); - 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); // preorder(root); // postorder(root); - inorder(root); + inorder(stringRoot); + printGreater(root, 1); + System.out.println(countNodes(root)); + } + + public static int countNodes(TreeNode node){ + + if (node == null) return 0; + return 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); } // Print a tree rooted at the given node in pre-order - public static void preorder(TreeNode node) { + public static void preorder(TreeNode node) { // if null, return if(node == null) { return; @@ -29,14 +55,14 @@ public static void preorder(TreeNode node) { preorder(node.right); } - public static void postorder(TreeNode node){ + 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){ + public static void inorder(TreeNode node){ if (node == null) return; inorder(node.left); diff --git a/src/TreeNode.java b/src/TreeNode.java index 35daf2e..8fd9faf 100644 --- a/src/TreeNode.java +++ b/src/TreeNode.java @@ -1,9 +1,9 @@ -public class TreeNode { - public int value; - public TreeNode left; - public TreeNode right; +public class TreeNode { + public T value; + public TreeNode left; + public TreeNode right; - public TreeNode(int value, TreeNode left, TreeNode right) { + public TreeNode(T value, TreeNode left, TreeNode right) { this.value = value; this.left = left; this.right = right; From 9df28a7bb0ab0058f81f3ccb43de9d73fc053f01 Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Thu, 19 Feb 2026 14:34:31 -0800 Subject: [PATCH 3/4] dfd --- src/Traversal.java | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Traversal.java b/src/Traversal.java index 6ec2772..4f4c940 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,3 +1,6 @@ +import java.util.HashMap; +import java.util.Map; + public class Traversal { public static void main(String[] args) { TreeNode root = new TreeNode(10, null, null); @@ -15,9 +18,27 @@ public static void main(String[] args) { // preorder(root); // postorder(root); - inorder(stringRoot); - printGreater(root, 1); - System.out.println(countNodes(root)); + // inorder(stringRoot); + // printGreater(root, 1); + // System.out.println(countNodes(root)); + Map counts = new HashMap<>(); + System.out.println(toMap(stringRoot)); + } + + public static Map toMap(TreeNode node){ + Map counts = new HashMap<>(); + toMap(node, counts); + return counts; + } + + private static Map toMap(TreeNode node, Map counts) { + if (node == null) { + return counts; + } + counts.put(node.value, counts.getOrDefault(node.value, 0) + 1 ); + toMap(node.left, counts); + toMap(node.right, counts); + return counts; } public static int countNodes(TreeNode node){ From 13affc91232a1a6c76bd2765b73d89a0564666ab Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Tue, 24 Feb 2026 19:57:30 -0800 Subject: [PATCH 4/4] finished --- src/Traversal.java | 85 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 15 deletions(-) diff --git a/src/Traversal.java b/src/Traversal.java index 4f4c940..3ada74b 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,8 @@ 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) { @@ -15,14 +18,79 @@ public static void main(String[] args) { 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<=50000; i++) { + current.right = new TreeNode(i, null, null); + current = current.right; + } + + // preOrderIter(megaRoot); + levelOrder(stringRoot); + + // System.out.println("Preorder recursive"); + // preorder(root); + // System.out.println("Preorder iterative"); + // preOrderIter(root); + + // preorder(megaRoot); // preorder(root); // postorder(root); // inorder(stringRoot); // printGreater(root, 1); // System.out.println(countNodes(root)); - Map counts = new HashMap<>(); - System.out.println(toMap(stringRoot)); + // Map counts = new HashMap<>(); + // System.out.println(toMap(stringRoot)); + } + + // Print a tree rooted at the given node in pre-order + public static void preorder(TreeNode node) { + // if null, return + if(node == null) { + return; + } + // print value + System.out.println(node.value); + // traverse left + preorder(node.left); + // traverse right + preorder(node.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.println(current.value); + stack.push(current.right); + stack.push(current.left); + + } + } + + public static void levelOrder(TreeNode node) { + Queue> stack = new LinkedList<>(); + + stack.add(node); + + while (!stack.isEmpty()) { + TreeNode current = stack.poll(); + + if (current == null) continue; + + System.out.println(current.value); + stack.add(current.left); + stack.add(current.right); + + } + } public static Map toMap(TreeNode node){ @@ -62,19 +130,6 @@ public static void printGreater(TreeNode node, int threshold) { printGreater(node.right, threshold); } - // Print a tree rooted at the given node in pre-order - public static void preorder(TreeNode node) { - // if null, return - if(node == null) { - return; - } - // print value - System.out.println(node.value); - // traverse left - preorder(node.left); - // traverse right - preorder(node.right); - } public static void postorder(TreeNode node){ if (node == null) return;