Created
September 15, 2016 00:21
-
-
Save lucis/2692bb17ca42bfcc94e391f6cd5e051f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adt.splaytree; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import adt.bt.BTNode; | |
public class TreePrinter { | |
public static void main(String[] args) { | |
SplayTree<Integer> spl = new SplayTreeImpl<Integer>(); | |
spl.insert(12); | |
spl.insert(1); | |
spl.insert(13); | |
spl.insert(5); | |
spl.insert(7); | |
spl.insert(24); | |
BTreePrinter.printNode(spl.getRoot()); | |
} | |
} | |
class BTreePrinter { | |
public static <T extends Comparable<?>> void printNode(BTNode<T> root) { | |
int maxLevel = BTreePrinter.maxLevel(root); | |
printNodeInternal(Collections.singletonList(root), 1, maxLevel); | |
} | |
private static <T extends Comparable<?>> void printNodeInternal(List<BTNode<T>> 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<BTNode<T>> newNodes = new ArrayList<BTNode<T>>(); | |
for (BTNode<T> node : nodes) { | |
if (node != null && !node.isEmpty()) { | |
System.out.print(node.getData()); | |
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).getLeft() != 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(BTNode<T> node) { | |
if (node == null) | |
return 0; | |
return Math.max(BTreePrinter.maxLevel(node.getLeft()), BTreePrinter.maxLevel(node.getRight())) + 1; | |
} | |
private static <T> boolean isAllElementsNull(List<T> list) { | |
for (Object object : list) { | |
if (object != null) | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment