Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add auto testting#26

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ngoc2503 wants to merge3 commits intorampatra:master
base:master
Choose a base branch
Loading
fromngoc2503:master
Open
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
}
}

[8]ページ先頭

©2009-2025 Movatter.jp