Skip to content

Instantly share code, notes, and snippets.

@lubaochuan
Created April 22, 2020 21:57
Show Gist options
  • Save lubaochuan/79ecc70bd4a843ce2c3ca6dfb21628d3 to your computer and use it in GitHub Desktop.
Save lubaochuan/79ecc70bd4a843ce2c3ca6dfb21628d3 to your computer and use it in GitHub Desktop.
public String toString(){
String result = "\n";
if(count != 0){
if (count > 2){
result += print(2, true, "");
}
result += tree[0]+"\n";
if (count > 1){
result += print(1, false, "");
}
}
return result;
}
private String print(int root, boolean isRight, String indent) {
String result = "";
int left = root*2+1;
int right = root*2+2;
if (right < count){
result += print(right, true, indent + (isRight ? " " : " | "));
}
result += indent;
if (isRight) {
result += " /";
} else {
result += " \\";
}
result += "----- "+tree[root]+"\n";
if (left < count) {
result += print(left, false, indent + (isRight ? " | " : " "));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment