Skip to content

Instantly share code, notes, and snippets.

@juliofalbo
Created December 14, 2018 11:23
Show Gist options
  • Save juliofalbo/6e5805ce36d972a8b05ebaf7213f0bab to your computer and use it in GitHub Desktop.
Save juliofalbo/6e5805ce36d972a8b05ebaf7213f0bab to your computer and use it in GitHub Desktop.
Bidirectional Binary Tree - Algorithm Test
package com.tradeshift.juliofalbo.challenge.tradeshift.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BTreePrinter {
public static <T extends Comparable<?>> void printNode(Node root) {
int maxLevel = BTreePrinter.maxLevel(root);
printNodeInternal(Collections.singletonList(root), 1, maxLevel);
}
private static <T extends Comparable<?>> void printNodeInternal(List<Node> nodes, int level, int maxLevel) {
if (nodes.isEmpty() || BTreePrinter.isAllElementsNull(nodes)) {
return;
}
int floor = maxLevel - level;
int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
int firstSpaces = (int) Math.pow(2, (floor)) - 1;
int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
BTreePrinter.printWhitespaces(firstSpaces);
List<Node> newNodes = new ArrayList<Node>();
for (Node node : nodes) {
if (node != null) {
System.out.print(node.getId());
newNodes.add(node.getLeft());
newNodes.add(node.getRight());
} else {
newNodes.add(null);
newNodes.add(null);
System.out.print(" ");
}
BTreePrinter.printWhitespaces(betweenSpaces);
}
System.out.println();
for (int i = 1; i <= endgeLines; i++) {
for (int j = 0; j < nodes.size(); j++) {
BTreePrinter.printWhitespaces(firstSpaces - i);
if (nodes.get(j) == null) {
BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1);
continue;
}
if (nodes.get(j).getLeft() != null) {
System.out.print("/");
} else {
BTreePrinter.printWhitespaces(1);
}
BTreePrinter.printWhitespaces(i + i - 1);
if (nodes.get(j).getRight() != null) {
System.out.print("\\");
} else {
BTreePrinter.printWhitespaces(1);
}
BTreePrinter.printWhitespaces(endgeLines + endgeLines - i);
}
System.out.println("");
}
printNodeInternal(newNodes, level + 1, maxLevel);
}
private static void printWhitespaces(int count) {
for (int i = 0; i < count; i++) {
System.out.print(" ");
}
}
private static <T extends Comparable<?>> int maxLevel(Node node) {
if (node == null) {
return 0;
}
return Math.max(BTreePrinter.maxLevel(node.getLeft()), BTreePrinter.maxLevel(node.getRight())) + 1;
}
private static boolean isAllElementsNull(List list) {
for (Object object : list) {
if (object != null) {
return false;
}
}
return true;
}
}
package com.tradeshift.juliofalbo.challenge.tradeshift.test;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<Node> database = new ArrayList<>();
public static void main(String[] args) {
createTree(null, false, true);
createTree(1, false, true);
createTree(3, false, true);
createTree(2, true, false);
BTreePrinter.printNode(database.get(0));
changeParent(2, 6);
BTreePrinter.printNode(database.get(0));
}
private static void createTree(Integer parentId, Boolean hasRight, Boolean hasLeft) {
Node root = new Node(database.size() + 1, parentId, null, null);
database.add(root);
if (hasRight) {
Node right = new Node(database.size() + 1, root.getId(), null, null);
database.add(right);
database.get(root.getId() - 1).setIdRight(right.getId());
}
if (hasLeft) {
Node left = new Node(database.size() + 1, root.getId(), null, null);
database.add(left);
database.get(root.getId() - 1).setIdLeft(left.getId());
}
if (parentId != null) {
Node node = database.get(parentId - 1);
insertNodeInEmptySpace(node, root.getId());
}
}
private static void insertNodeInEmptySpace(final Node node, final Integer id) {
if (node.getIdLeft() != null && node.getIdRight() != null) {
throw new RuntimeException("Parent node is full");
}
if (node.getIdLeft() == null) {
node.setIdLeft(id);
} else if (node.getIdRight() == null) {
node.setIdRight(id);
}
}
private static void changeParent(Integer currentTreeId, Integer newParentId) {
final Node currentNode = database.get(currentTreeId - 1);
final Node oldParent = database.get(currentNode.getIdParent() - 1);
if (oldParent.getIdLeft() != null && oldParent.getIdLeft().equals(currentTreeId)) {
oldParent.setIdLeft(null);
}
if (oldParent.getIdRight() != null && oldParent.getIdRight().equals(currentTreeId)) {
oldParent.setIdRight(null);
}
final Node newParent = database.get(newParentId - 1);
insertNodeInEmptySpace(newParent, currentTreeId);
currentNode.setIdParent(newParentId);
}
}
package com.tradeshift.juliofalbo.challenge.tradeshift.test;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Node {
Integer id;
Integer idParent;
Integer idRight;
Integer idLeft;
public Node getRight() {
return idRight == null ? null : Main.database.get(idRight - 1);
}
public Node getLeft() {
return idLeft == null ? null : Main.database.get(idLeft - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment