From 2005a9b159caa596641070d7491170d593a72eb9 Mon Sep 17 00:00:00 2001 From: ngocnguyen <nguyenvanngoc7857@gmail.com> Date: Mon, 8 Aug 2022 13:18:07 +0700 Subject: [PATCH 1/3] edit FirstCommonAncestor.java --- .../treesandgraphs/FirstCommonAncestor.java | 71 +++++++++++++------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java b/src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java index 38977be3..bb362696 100644 --- a/src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java +++ b/src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java @@ -57,15 +57,57 @@ private static TreeNode findFCA(TreeNode root, TreeNode a, TreeNode b) { } } - private static class TreeNode { + public class TreeNode { int val; TreeNode left; TreeNode right; - - TreeNode(int val) { + public TreeNode(int val) { this.val = val; } + + public void setLeft(TreeNode left) { + this.left = left; + } + public TreeNode getLeft() { + return left; + } + + public void setRight(TreeNode right) { + this.right = right; + } + + public TreeNode getRight() { + return right; + } + public int getVal() { + return val; + } } + + // addNode + public void addNode(TreeNode treeRoot) { + treeRoot.left = new TreeNode(5); + treeRoot.right = new TreeNode(8); + treeRoot.left.left = new TreeNode(1); + treeRoot.left.right = new TreeNode(3); + treeRoot.left.left.left = new TreeNode(0); + treeRoot.right.left = new TreeNode(2); + treeRoot.right.right = new TreeNode(9); + treeRoot.right.left.right = new TreeNode(7); + } + + // test + public void testCase(TreeNode treeRoot) { + System.out.println("FCA of 0 and 7 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.left.right).val); + System.out.println("FCA of 0 and 9 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.right).val); + System.out.println("FCA of 0 and 1 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.left.left).val); + System.out.println("FCA of 1 and 2 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left).val); + System.out.println("FCA of 1 and 7 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left.right).val); + System.out.println("FCA of 4 and 7 is: " + findFCA(treeRoot, treeRoot, treeRoot.right.left.right).val); + System.out.println("FCA of 5 and 2 is: " + findFCA(treeRoot, treeRoot.left, treeRoot.right.left).val); + System.out.println("FCA of 7 and 9 is: " + findFCA(treeRoot, treeRoot.right.left.right, treeRoot.right.right).val); + System.out.println("FCA of 7 and 10 is: " + findFCA(treeRoot, treeRoot.right.left.right, new TreeNode(10)).val); // this use case does not work with the above algorithm + } public static void main(String[] args) { /* @@ -80,24 +122,9 @@ public static void main(String[] args) { 0 7 */ - TreeNode treeRoot = new TreeNode(4); - treeRoot.left = new TreeNode(5); - treeRoot.right = new TreeNode(8); - treeRoot.left.left = new TreeNode(1); - treeRoot.left.right = new TreeNode(3); - treeRoot.left.left.left = new TreeNode(0); - treeRoot.right.left = new TreeNode(2); - treeRoot.right.right = new TreeNode(9); - treeRoot.right.left.right = new TreeNode(7); - - System.out.println("FCA of 0 and 7 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.left.right).val); - System.out.println("FCA of 0 and 9 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.right).val); - System.out.println("FCA of 0 and 1 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.left.left).val); - System.out.println("FCA of 1 and 2 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left).val); - System.out.println("FCA of 1 and 7 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left.right).val); - System.out.println("FCA of 4 and 7 is: " + findFCA(treeRoot, treeRoot, treeRoot.right.left.right).val); - System.out.println("FCA of 5 and 2 is: " + findFCA(treeRoot, treeRoot.left, treeRoot.right.left).val); - System.out.println("FCA of 7 and 9 is: " + findFCA(treeRoot, treeRoot.right.left.right, treeRoot.right.right).val); - System.out.println("FCA of 7 and 10 is: " + findFCA(treeRoot, treeRoot.right.left.right, new TreeNode(10)).val); // this use case does not work with the above algorithm + FirstCommonAncestor fcancestor = new FirstCommonAncestor(); + FirstCommonAncestor.TreeNode treeRoot = fcancestor.new TreeNode(4); + fcancestor.addNode(treeRoot); + fcancestor.testCase(treeRoot); } } \ No newline at end of file From 93359417b964b147c3f73682a14d3316cb24406a Mon Sep 17 00:00:00 2001 From: ngocnguyen <nguyenvanngoc7857@gmail.com> Date: Mon, 8 Aug 2022 13:28:43 +0700 Subject: [PATCH 2/3] add new file test --- .../FirstCommonAncestorTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java diff --git a/src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java b/src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java new file mode 100644 index 00000000..6ab1ef8d --- /dev/null +++ b/src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java @@ -0,0 +1,44 @@ +package com.swe102x.mytest; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.swe102x.myclass.FirstCommonAncestor; +import com.swe102x.myclass.FirstCommonAncestor.TreeNode; + +class FirstCommonAncestorTest { + + @BeforeAll + static void setUpBeforeClass() throws Exception { + } + + @AfterAll + static void tearDownAfterClass() throws Exception { + } + FirstCommonAncestor ancestor; + FirstCommonAncestor.TreeNode root; + @BeforeEach + void setUp() throws Exception { + + ancestor = new FirstCommonAncestor(); + root = ancestor.new TreeNode(4); + ancestor.addNode(root); + + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void testFindFCA() { + //test case + assertEquals(null, ancestor.findFCA(null, root.getLeft().getLeft().getLeft(), root.getRight().getRight())); + } + +} From 3b616af85726e032e5da7c08509d354eba189bc4 Mon Sep 17 00:00:00 2001 From: ngocnguyen <nguyenvanngoc7857@gmail.com> Date: Wed, 10 Aug 2022 19:25:01 +0700 Subject: [PATCH 3/3] edit file test --- .../ctci/testfirstcommonancestor/FirstCommonAncestorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java b/src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java index 6ab1ef8d..44b000f8 100644 --- a/src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java +++ b/src/main/java/com/ctci/testfirstcommonancestor/FirstCommonAncestorTest.java @@ -38,7 +38,7 @@ void tearDown() throws Exception { @Test void testFindFCA() { //test case - assertEquals(null, ancestor.findFCA(null, root.getLeft().getLeft().getLeft(), root.getRight().getRight())); + assertEquals(root, ancestor.findFCA(root, root.getLeft().getLeft().getLeft(), root.getRight().getRight())); } }