public static <AnyType> void mysteryPrint(BinaryNode<AnyType> t) {
if( t! = null) {
System.out.println(t.getElement() );
mysteryPrint(t.getLeft() );
System.out.println(t.getElement() );
mysteryPrint(t.getRight() );
System.out.println(t.getElement() );
}
}
A
B C
D E
I have made the tree above and I was wondering whats the easiest was to trace this code find the output?
I am getting ABBDDB so far and I lose track.
Without tracing, it’s relatively straightforward to see what a given tree should print. The structure of the method means that a string will look like this:
So all you have to do is continually substitute that pattern for the left and right sub-trees (with empty strings for non-existent sub-trees) and get the string.
For your example tree, it looks like this (using [ and ] to show substitutions):
So the method prints out ABBDDDBACEEECCA