A java utility for generating formatted strings from binary trees.
To install the printer, simply add the toString method, and the TreeString class inside your BinaryTree class. These methods can be found in BinaryTree.java, where a full setup is provided.
public class BinaryTree {
private Node root;
private class Node {
Node left, right;
int value;
}
// add the toString method
@Override
public String toString() {
// get the content from BinaryTree.java
}
// add the TreeString class
private class TreeString {
// get the content from BinaryTree.java
}
}The code will only work if you have the same names as above, unless you change the code.
BinaryTreecan be called something else if you want toNodeclass has to be called Noderoothas to be called root and be of typeNodelefthas to be called left and be of typeNoderighthas to be called left and be of typeNodevaluehas to be called value, but does not be of typeint. Since the printer usesString.valueOf(node.value), the printer will probably work on any datatype. If it is an object, it will fall back to the objectstoStringmethod.
The file contains an override of the toString() method, and a private class that contains all the logic. Simple call the tree's toString() function, and a representation of the tree is returned.
+--[ 31 ]
|
+--[ 50 ]--+
|
+--[ 61 ]--+
| |
| | +--[ 89 ]--+
| | | |
| | | +--[ 99 ]
| | |
| | +--[ 207 ]--+
| | |
| +--[ 225 ]--+
| |
| +--[ 291 ]--+
| |
| +--[ 373 ]--+
| |
| +--[3513]
|
[5767]--+
|
+--[6460]--+
|
| +--[78788]--+
| | |
| | +--[91482]
| |
+--[811201]--+