/**
* Prints a binary tree in outline form, with each left child above
* its corresponding right sibling. if tellType is true,
* the type (class) of each node is displayed in front of the String
* representation of its value.
*
* @param tellType Tells whether to display the type (class) of each node.
*/
public void print(boolean tellType) {
print(true, "", tellType);
}
/**
* Prints a binary tree in outline form, with each left child above
* its corresponding right sibling. The type (class) of each node
* is displayed, followed by the String representation of its value.
*/
public void print() {
print(true, "", true);
}
/**
* Prints a binary tree in outline form, with each left child above
* its corresponding right sibling. if tellType is true,
* the type (class) of each node is displayed in front of the String
* representation of its value.
*
* @param isLeft True to indicate that this node is a left child--
* must be true if this node is a root.
* @param indent The indentation string to use.
* @param tellType Tells whether to display the type (class) of each node.
*/
private void print(boolean isLeft, String indent, boolean tellType) {
String nodeDisplay = value.toString();
if (tellType) {
String type = value.getClass().getName();
nodeDisplay = type + " " + value;
}
if (isLeft) {
System.out.println("" + indent + "__" + nodeDisplay);
}
else {
System.out.println(
deleteLastChar(indent) + "|__" + nodeDisplay);
}
if (isLeaf() && !isLeft)
System.out.println(deleteLastChar(indent));
if (isLeaf())
return;
if (isLeft)
indent += " |";
else {
indent = deleteLastChar(indent) + " |";
}
if (leftChild == null)
System.out.println(indent + "__null");
else
leftChild.print(true, indent, tellType);
if (rightChild == null)
System.out.println(indent + "__null");
else
rightChild.print(false, indent, tellType);
}
private String deleteLastChar(String s) {
return s.substring(0, s.length() - 1);
}
|