update unit tests
This commit is contained in:
parent
af1f61923d
commit
a63f93f69a
|
@ -16,7 +16,6 @@ public class BinaryTreePrinter {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(root.getValue());
|
sb.append(root.getValue());
|
||||||
sb.append("\n");
|
|
||||||
|
|
||||||
String pointerRight = "└──";
|
String pointerRight = "└──";
|
||||||
String pointerLeft = (root.getRight() != null) ? "├──" : "└──";
|
String pointerLeft = (root.getRight() != null) ? "├──" : "└──";
|
||||||
|
@ -32,10 +31,10 @@ public class BinaryTreePrinter {
|
||||||
|
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
|
|
||||||
|
sb.append("\n");
|
||||||
sb.append(padding);
|
sb.append(padding);
|
||||||
sb.append(pointer);
|
sb.append(pointer);
|
||||||
sb.append(node.getValue());
|
sb.append(node.getValue());
|
||||||
sb.append("\n");
|
|
||||||
|
|
||||||
StringBuilder paddingBuilder = new StringBuilder(padding);
|
StringBuilder paddingBuilder = new StringBuilder(padding);
|
||||||
if (hasRightSibling) {
|
if (hasRightSibling) {
|
||||||
|
@ -56,7 +55,7 @@ public class BinaryTreePrinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void print() {
|
public void print() {
|
||||||
System.out.println(traversePreOrder(tree));
|
System.out.print(traversePreOrder(tree));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,19 +1,36 @@
|
||||||
package com.baeldung.binarytree;
|
package com.baeldung.binarytree;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class PrintingBinaryTreeUnitTest {
|
public class PrintingBinaryTreeModelUnitTest {
|
||||||
|
|
||||||
private BinaryTreeModel balanced;
|
private BinaryTreeModel balanced;
|
||||||
private BinaryTreeModel leftUnbalanced;
|
private BinaryTreeModel leftSkewed;
|
||||||
private BinaryTreeModel rightUnbalanced;
|
private BinaryTreeModel rightSkewed;
|
||||||
|
|
||||||
|
private OutputStream output;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
balanced = createBalancedTree();
|
balanced = createBalancedTree();
|
||||||
leftUnbalanced = createLeftUnbalancedTree();
|
leftSkewed = createLeftUnbalancedTree();
|
||||||
rightUnbalanced = createRightUnbalancedTree();
|
rightSkewed = createRightUnbalancedTree();
|
||||||
|
|
||||||
|
output = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(output));
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
System.setOut(System.out);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BinaryTreeModel createBalancedTree() {
|
private BinaryTreeModel createBalancedTree() {
|
||||||
|
@ -48,7 +65,7 @@ public class PrintingBinaryTreeUnitTest {
|
||||||
|
|
||||||
private BinaryTreeModel createLeftUnbalancedTree() {
|
private BinaryTreeModel createLeftUnbalancedTree() {
|
||||||
|
|
||||||
BinaryTreeModel root = new BinaryTreeModel("left");
|
BinaryTreeModel root = new BinaryTreeModel("root");
|
||||||
|
|
||||||
BinaryTreeModel node1 = new BinaryTreeModel("node1");
|
BinaryTreeModel node1 = new BinaryTreeModel("node1");
|
||||||
BinaryTreeModel node2 = new BinaryTreeModel("node2");
|
BinaryTreeModel node2 = new BinaryTreeModel("node2");
|
||||||
|
@ -78,7 +95,7 @@ public class PrintingBinaryTreeUnitTest {
|
||||||
|
|
||||||
private BinaryTreeModel createRightUnbalancedTree() {
|
private BinaryTreeModel createRightUnbalancedTree() {
|
||||||
|
|
||||||
BinaryTreeModel root = new BinaryTreeModel("right");
|
BinaryTreeModel root = new BinaryTreeModel("root");
|
||||||
|
|
||||||
BinaryTreeModel node1 = new BinaryTreeModel("node1");
|
BinaryTreeModel node1 = new BinaryTreeModel("node1");
|
||||||
BinaryTreeModel node2 = new BinaryTreeModel("node2");
|
BinaryTreeModel node2 = new BinaryTreeModel("node2");
|
||||||
|
@ -108,17 +125,60 @@ public class PrintingBinaryTreeUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBinaryTreeModelBalanced_whenPrintWithBinaryTreePrinter() {
|
public void givenBinaryTreeModelBalanced_whenPrintWithBinaryTreePrinter() {
|
||||||
|
|
||||||
|
StringBuilder expected = new StringBuilder();
|
||||||
|
expected.append("root").append("\n");
|
||||||
|
expected.append("├──node1").append("\n");
|
||||||
|
expected.append("│ ├──node3").append("\n");
|
||||||
|
expected.append("│ │ └──node7").append("\n");
|
||||||
|
expected.append("│ │ ├──node8").append("\n");
|
||||||
|
expected.append("│ │ └──node9").append("\n");
|
||||||
|
expected.append("│ └──node4").append("\n");
|
||||||
|
expected.append("└──node2").append("\n");
|
||||||
|
expected.append(" ├──node5").append("\n");
|
||||||
|
expected.append(" └──node6");
|
||||||
|
|
||||||
new BinaryTreePrinter(balanced).print();
|
new BinaryTreePrinter(balanced).print();
|
||||||
|
|
||||||
|
assertEquals(expected.toString(), output.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBinaryTreeModelLeftUnbalanced_whenPrintWithBinaryTreePrinter() {
|
public void givenBinaryTreeModelLeftUnbalanced_whenPrintWithBinaryTreePrinter() {
|
||||||
new BinaryTreePrinter(leftUnbalanced).print();
|
|
||||||
|
StringBuilder expected = new StringBuilder();
|
||||||
|
expected.append("root").append("\n");
|
||||||
|
expected.append("├──node1").append("\n");
|
||||||
|
expected.append("│ └──node3").append("\n");
|
||||||
|
expected.append("│ └──node4").append("\n");
|
||||||
|
expected.append("│ └──node5").append("\n");
|
||||||
|
expected.append("│ └──node6").append("\n");
|
||||||
|
expected.append("│ └──node7").append("\n");
|
||||||
|
expected.append("│ └──node8").append("\n");
|
||||||
|
expected.append("└──node2");
|
||||||
|
|
||||||
|
new BinaryTreePrinter(leftSkewed).print();
|
||||||
|
|
||||||
|
assertEquals(expected.toString(), output.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBinaryTreeModelRightUnbalanced_whenPrintWithBinaryTreePrinter() {
|
public void givenBinaryTreeModelRightUnbalanced_whenPrintWithBinaryTreePrinter() {
|
||||||
new BinaryTreePrinter(rightUnbalanced).print();
|
|
||||||
|
StringBuilder expected = new StringBuilder();
|
||||||
|
expected.append("root").append("\n");
|
||||||
|
expected.append("├──node1").append("\n");
|
||||||
|
expected.append("└──node2").append("\n");
|
||||||
|
expected.append(" └──node3").append("\n");
|
||||||
|
expected.append(" └──node4").append("\n");
|
||||||
|
expected.append(" └──node5").append("\n");
|
||||||
|
expected.append(" └──node6").append("\n");
|
||||||
|
expected.append(" └──node7").append("\n");
|
||||||
|
expected.append(" └──node8");
|
||||||
|
|
||||||
|
new BinaryTreePrinter(rightSkewed).print();
|
||||||
|
|
||||||
|
assertEquals(expected.toString(), output.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue