Perhaps the picture will help. I find recursion difficult as well, and I've found pictures useful. It might be nice to open the diagram in a separate window and have the explanation on the side.
First of all, recursion uses something called a stack. It's the pile of four rectangles you see in the diagram. For example, there are two empty stacks at the very end. Suppose a function A()
calls a function B()
before A
has terminated. Then what needs to happen is we execute A()
partway through, then execute B()
, then go back and finish executing A()
. But when we go to execute B()
, we need to remember where we were in A()
. Thus, we need to store the information about A()
and B()
in individual rectangles in the stack. That way, after we finish executing B()
, we know where we left off in A()
and can finish the function.
So maybe it will help if we step through the recursion with the diagram of the stack. Also suppose we have this:
public static void main( String[] args ) {
Postorder(3);
}
1
So initially, main runs and its contents get added to the bottom of the stack, as we see in part 1.
1->2
But when main()
calls Postorder(3)
, it hasn't terminated yet. So, in another stack frame, we add the contents of the Postorder(3)
function call. You can see this in part 2. The yellow arrows remember where we left off in each stack frame before we went to execute another function.
2->3
Now, we're executing Postorder(3)
and we reach the function call Postorder(5)
. But Postorder(3)
hasn't finished running, so in another stack frame, we have to add the contents of Postorder(5)
. You can see this in part 3.
3->4
Now we're executing Postorder(5)
. We reach the function call Postorder(1)
. But Postorder(5)
hasn't finished running, so in another stackframe, we have to add the contents of Postorder(1)
. For simplicity, since 1 has no subnodes, we'll say that Postorder(1)
is equivalent to Print(1)
. This corresponds to part 4.
4->5
Now, in part 4, Print(1) executes, and Postorder(1)
terminates. When Postorder(1)
terminates, it can be removed from the stack. Also, since Postorder(1)
finished, we can resume executing Postorder(5)
. The yellow arrow tells us that we left off at line 1 of Postorder(5)
before we jumped off to execute another function. Well, we can now move on to line 2 of Postorder(5)
. This corresponds to part 5.
5->6
Line 2 of Postorder(5)
is the command Postorder(4)
. Since Postorder(5)
has not yet finished executing, we must add the contents of Postorder(4)
to another stack frame. This corresponds to part 6.
...
It's pretty much the same idea from then on. Let me know if you still want me to step through the remaining 8 parts. It gets a bit tedious afterwards. Hopefully this visual helps.
PostOrder(root.left)
calls get returned. You check if 1 has been visited inSystem.out.print(root.data + " ")
, just like all the other nodes. Remember,PostOrder(root.left)
andPostOrder(root.right)
called from node 1 will return immediately when they hit theroot == null
condition, leaving only System.out.print to be executed for node 1. – Robert Harvey