From 17ec212d170f671f67eed41de3b15b02ae19a83d Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Thu, 12 Feb 2026 14:47:50 -0800 Subject: [PATCH 1/4] done w the file --- src/Traversal.java | 49 ++++++++++++++++++++++++++++++++++++++++++++-- src/TreeNode.java | 10 ++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/Traversal.java b/src/Traversal.java index 8da0f79..d719f41 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,50 @@ 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); // root in the beginning + // postOrder(root); // root in the end + inOrder(root); // root in the middle + } + + // print a tree routed at the given node in pre order + public static void preOrder(TreeNode node) { + // if full return + if(node == null) { + return; + } + // print value + System.out.println(node.value); + // traverse left + preOrder(node.left); + // traverse right + preOrder(node.right); + + } + // print after recursion + 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..80829bf 100644 --- a/src/TreeNode.java +++ b/src/TreeNode.java @@ -1,4 +1,14 @@ 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 6ca05198ca6ded1daf1a67eef349152dfb49b097 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Tue, 17 Feb 2026 14:41:10 -0800 Subject: [PATCH 2/4] updated treenode and traversal --- src/Traversal.java | 66 +++++++++++++++++++++++++++++++++++++--------- src/TreeNode.java | 10 +++---- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/Traversal.java b/src/Traversal.java index d719f41..e06f807 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,22 +1,64 @@ 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 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); + 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("hello", null, null); + + stringRoot.right = new TreeNode("you", null, null); + stringRoot.right.left = new TreeNode("i", null, null); + stringRoot.right.right = new TreeNode("miss", null, null); + stringRoot.right.right.right = new TreeNode("you", null, null); // preOrder(root); // root in the beginning // postOrder(root); // root in the end - inOrder(root); // root in the middle + // inOrder(root); // root in the middle + printGreater(root, 100); + System.out.println(countNodes(root)); } + public static int countNodes(TreeNode node) { + // if node is null : return 0; + if (node == null) return 0; + return 1 + countNodes(node.left) + countNodes(node.right); + + // or + // return node == null? 0 : 1 + countNodes(node.left) + countNodes(node.right); + + + // // leftCount = count nodes in left subtree + // int leftCount = countNodes(node.left); + // // rightCount = count nodes in right subtree + // int rightCount = countNodes(node.right); + // // overallCount = leftcount + rightcount + 1 --> 1 is th eparent + // int overallCount = leftCount + rightCount + 1; + // // return overallCount + // 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 routed at the given node in pre order - public static void preOrder(TreeNode node) { + public static void preOrder(TreeNode node) { // if full return if(node == null) { return; @@ -30,14 +72,14 @@ public static void preOrder(TreeNode node) { } // print after recursion - 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); System.out.println(node.value); diff --git a/src/TreeNode.java b/src/TreeNode.java index 80829bf..dae07f1 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 f7a54928d24fb959b2fa954705f4d9e9ac72375e Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Thu, 19 Feb 2026 14:33:10 -0800 Subject: [PATCH 3/4] done w file --- src/Traversal.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Traversal.java b/src/Traversal.java index e06f807..4f6440a 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); @@ -24,9 +27,31 @@ public static void main(String[] args) { // preOrder(root); // root in the beginning // postOrder(root); // root in the end // inOrder(root); // root in the middle - printGreater(root, 100); - System.out.println(countNodes(root)); + // printGreater(root, 100); + // 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 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 is null : return 0; if (node == null) return 0; From 7d4249905434970a2b55bb09c5a58d759be07873 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Tue, 24 Feb 2026 14:51:10 -0800 Subject: [PATCH 4/4] done w file --- src/Traversal.java | 87 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 15 deletions(-) diff --git a/src/Traversal.java b/src/Traversal.java index 4f6440a..1ba6617 100644 --- a/src/Traversal.java +++ b/src/Traversal.java @@ -1,5 +1,11 @@ import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.Map; +import java.util.Stack; +import java.util.Queue; +import java.util.LinkedList; + + public class Traversal { public static void main(String[] args) { @@ -24,15 +30,79 @@ public static void main(String[] args) { stringRoot.right.right = new TreeNode("miss", null, null); stringRoot.right.right.right = new TreeNode("you", null, null); - // preOrder(root); // root in the beginning + 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; + } + + levelOrder(root); + // preOrderIter(megaRoot); + // System.out.println("Preorder recursive"); + // preorder(root); + // System.out.println("preorder iterative"); + // preOrderIter(root); + + // // preOrder(root); // root in the beginning // postOrder(root); // root in the end // inOrder(root); // root in the middle // printGreater(root, 100); // System.out.println(countNodes(root)); // Map counts = new HashMap<>(); - System.out.println(toMap(stringRoot)); + // System.out.println(toMap(stringRoot)); } + // print a tree routed at the given node in pre order + public static void preOrder(TreeNode node) { + if(node == null) {// if full return + return; + } + System.out.println(node.value); // print value + preOrder(node.left); // traverse left + preOrder(node.right); // traverse 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; + } + + // if (current != null) { + 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; + } + + // if (current != null) { + 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); @@ -82,20 +152,7 @@ public static void printGreater(TreeNode node, int threshold) { } - // print a tree routed at the given node in pre order - public static void preOrder(TreeNode node) { - // if full return - if(node == null) { - return; - } - // print value - System.out.println(node.value); - // traverse left - preOrder(node.left); - // traverse right - preOrder(node.right); - } // print after recursion public static void postOrder(TreeNode node) { if (node == null) return;