27 lines
1.9 KiB
Plaintext
27 lines
1.9 KiB
Plaintext
Iterative postorder with one stack, step by step
|
|
================================================
|
|
|
|
step | action | stack (bottom..top) | output so far
|
|
1 | push 1 | [1] | []
|
|
2 | push 2 | [1 2] | []
|
|
3 | push 4 | [1 2 4] | []
|
|
4 | pop 4, visit | [1 2] | [4]
|
|
5 | peek 2: right child 5 not done, go right | [1 2] | [4]
|
|
6 | push 5 | [1 2 5] | [4]
|
|
7 | push 7 | [1 2 5 7] | [4]
|
|
8 | pop 7, visit | [1 2 5] | [4 7]
|
|
9 | pop 5, visit | [1 2] | [4 7 5]
|
|
10 | pop 2, visit | [1] | [4 7 5 2]
|
|
11 | peek 1: right child 3 not done, go right | [1] | [4 7 5 2]
|
|
12 | push 3 | [1 3] | [4 7 5 2]
|
|
13 | peek 3: right child 6 not done, go right | [1 3] | [4 7 5 2]
|
|
14 | push 6 | [1 3 6] | [4 7 5 2]
|
|
15 | push 8 | [1 3 6 8] | [4 7 5 2]
|
|
16 | pop 8, visit | [1 3 6] | [4 7 5 2 8]
|
|
17 | peek 6: right child 9 not done, go right | [1 3 6] | [4 7 5 2 8]
|
|
18 | push 9 | [1 3 6 9] | [4 7 5 2 8]
|
|
19 | pop 9, visit | [1 3 6] | [4 7 5 2 8 9]
|
|
20 | pop 6, visit | [1 3] | [4 7 5 2 8 9 6]
|
|
21 | pop 3, visit | [1] | [4 7 5 2 8 9 6 3]
|
|
22 | pop 1, visit | [] | [4 7 5 2 8 9 6 3 1]
|