Skip to content

Instantly share code, notes, and snippets.

@Acarus
Created June 8, 2015 14:23
Show Gist options
  • Save Acarus/af050eee226f5b07b19c to your computer and use it in GitHub Desktop.
Save Acarus/af050eee226f5b07b19c to your computer and use it in GitHub Desktop.
PrintTree
public void testPrint() {
Stack s1 = new Stack();
s1.push(root);
int emptyLeaf = 32;
boolean isRowEmpty = false;
while(isRowEmpty == false) {
Stack s2 = new Stack();
isRowEmpty = true;
for(int i = 0; i < emptyLeaf; i++)
System.out.print(' ');
while(s1.isEmpty() == false) {
Node temp = s1.pop();
if(temp != null) {
System.out.print(temp.item);
s2.push(temp.leftChild);
s2.push(temp.rightChild);
if(temp.leftChild != null || temp.rightChild != null)
isRowEmpty = false;
} else {
System.out.print("--");
s2.push(null);
s2.push(null);
}
for(int i = 0; i < emptyLeaf * 2 - 2; i++)
System.out.print(" ");
}
System.out.println();
emptyLeaf /= 2;
while(s2.isEmpty() == false) {
s1.push(s2.pop());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment