-
-
Save lubaochuan/79ecc70bd4a843ce2c3ca6dfb21628d3 to your computer and use it in GitHub Desktop.
This file contains 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
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