Add source code for BAEL-3408

This commit is contained in:
Yavuz Tas 2019-11-20 00:33:12 +01:00
parent 0bf12a690c
commit 45126f1d62
3 changed files with 223 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.binarytree;
public class BinaryTreeModel {
private Object value;
private BinaryTreeModel left;
private BinaryTreeModel right;
public BinaryTreeModel(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public BinaryTreeModel getLeft() {
return left;
}
public void setLeft(BinaryTreeModel left) {
this.left = left;
}
public BinaryTreeModel getRight() {
return right;
}
public void setRight(BinaryTreeModel right) {
this.right = right;
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.binarytree;
public class BinaryTreePrinter {
private BinaryTreeModel tree;
public BinaryTreePrinter(BinaryTreeModel tree) {
this.tree = tree;
}
private String traversePreOrder(BinaryTreeModel root) {
if (root == null) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append(root.getValue());
sb.append("\n");
String pointerRight = "└──";
String pointerLeft = (root.getRight() != null) ? "├──" : "└──";
traverseNodes(sb, "", pointerLeft, root.getLeft(), root.getRight() != null);
traverseNodes(sb, "", pointerRight, root.getRight(), false);
return sb.toString();
}
private void traverseNodes(StringBuilder sb, String padding, String pointer, BinaryTreeModel node,
boolean hasRightSibling) {
if (node != null) {
sb.append(padding);
sb.append(pointer);
sb.append(node.getValue());
sb.append("\n");
StringBuilder paddingBuilder = new StringBuilder(padding);
if (hasRightSibling) {
paddingBuilder.append("");
} else {
paddingBuilder.append(" ");
}
String paddingForBoth = paddingBuilder.toString();
String pointerRight = "└──";
String pointerLeft = (node.getRight() != null) ? "├──" : "└──";
traverseNodes(sb, paddingForBoth, pointerLeft, node.getLeft(), node.getRight() != null);
traverseNodes(sb, paddingForBoth, pointerRight, node.getRight(), false);
}
}
public void print() {
System.out.println(traversePreOrder(tree));
}
}

View File

@ -0,0 +1,124 @@
package com.baeldung.binarytree;
import org.junit.Before;
import org.junit.Test;
public class PrintingBinaryTreeUnitTest {
private BinaryTreeModel balanced;
private BinaryTreeModel leftUnbalanced;
private BinaryTreeModel rightUnbalanced;
@Before
public void setup() {
balanced = createBalancedTree();
leftUnbalanced = createLeftUnbalancedTree();
rightUnbalanced = createRightUnbalancedTree();
}
private BinaryTreeModel createBalancedTree() {
BinaryTreeModel root = new BinaryTreeModel("root");
BinaryTreeModel node1 = new BinaryTreeModel("node1");
BinaryTreeModel node2 = new BinaryTreeModel("node2");
root.setLeft(node1);
root.setRight(node2);
BinaryTreeModel node3 = new BinaryTreeModel("node3");
BinaryTreeModel node4 = new BinaryTreeModel("node4");
node1.setLeft(node3);
node1.setRight(node4);
BinaryTreeModel node5 = new BinaryTreeModel("node5");
BinaryTreeModel node6 = new BinaryTreeModel("node6");
node2.setLeft(node5);
node2.setRight(node6);
BinaryTreeModel node7 = new BinaryTreeModel("node7");
node3.setLeft(node7);
BinaryTreeModel node8 = new BinaryTreeModel("node8");
BinaryTreeModel node9 = new BinaryTreeModel("node9");
node7.setLeft(node8);
node7.setRight(node9);
return root;
}
private BinaryTreeModel createLeftUnbalancedTree() {
BinaryTreeModel root = new BinaryTreeModel("left");
BinaryTreeModel node1 = new BinaryTreeModel("node1");
BinaryTreeModel node2 = new BinaryTreeModel("node2");
root.setLeft(node1);
root.setRight(node2);
BinaryTreeModel node3 = new BinaryTreeModel("node3");
node1.setLeft(node3);
BinaryTreeModel node4 = new BinaryTreeModel("node4");
node3.setLeft(node4);
BinaryTreeModel node5 = new BinaryTreeModel("node5");
node4.setLeft(node5);
BinaryTreeModel node6 = new BinaryTreeModel("node6");
node5.setLeft(node6);
BinaryTreeModel node7 = new BinaryTreeModel("node7");
node6.setLeft(node7);
BinaryTreeModel node8 = new BinaryTreeModel("node8");
node7.setLeft(node8);
return root;
}
private BinaryTreeModel createRightUnbalancedTree() {
BinaryTreeModel root = new BinaryTreeModel("right");
BinaryTreeModel node1 = new BinaryTreeModel("node1");
BinaryTreeModel node2 = new BinaryTreeModel("node2");
root.setLeft(node1);
root.setRight(node2);
BinaryTreeModel node3 = new BinaryTreeModel("node3");
node2.setRight(node3);
BinaryTreeModel node4 = new BinaryTreeModel("node4");
node3.setRight(node4);
BinaryTreeModel node5 = new BinaryTreeModel("node5");
node4.setRight(node5);
BinaryTreeModel node6 = new BinaryTreeModel("node6");
node5.setRight(node6);
BinaryTreeModel node7 = new BinaryTreeModel("node7");
node6.setRight(node7);
BinaryTreeModel node8 = new BinaryTreeModel("node8");
node7.setRight(node8);
return root;
}
@Test
public void givenBinaryTreeModelBalanced_whenPrintWithBinaryTreePrinter() {
new BinaryTreePrinter(balanced).print();
}
@Test
public void givenBinaryTreeModelLeftUnbalanced_whenPrintWithBinaryTreePrinter() {
new BinaryTreePrinter(leftUnbalanced).print();
}
@Test
public void givenBinaryTreeModelRightUnbalanced_whenPrintWithBinaryTreePrinter() {
new BinaryTreePrinter(rightUnbalanced).print();
}
}