Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add auto testting #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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(root, ancestor.findFCA(root, root.getLeft().getLeft().getLeft(), root.getRight().getRight()));
}

}
71 changes: 49 additions & 22 deletions src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/*
Expand All @@ -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);
}
}