23 lines
1.9 KiB
Plaintext
23 lines
1.9 KiB
Plaintext
Iterative postorder with two stacks, step by step
|
|
=================================================
|
|
|
|
step | action | s1 (bottom..top) | s2 (bottom..top) | output so far
|
|
1 | move 1 to s2, push its children on s1 | [2 3] | [1] | []
|
|
2 | move 3 to s2, push its children on s1 | [2 6] | [1 3] | []
|
|
3 | move 6 to s2, push its children on s1 | [2 8 9] | [1 3 6] | []
|
|
4 | move 9 to s2, push its children on s1 | [2 8] | [1 3 6 9] | []
|
|
5 | move 8 to s2, push its children on s1 | [2] | [1 3 6 9 8] | []
|
|
6 | move 2 to s2, push its children on s1 | [4 5] | [1 3 6 9 8 2] | []
|
|
7 | move 5 to s2, push its children on s1 | [4 7] | [1 3 6 9 8 2 5] | []
|
|
8 | move 7 to s2, push its children on s1 | [4] | [1 3 6 9 8 2 5 7] | []
|
|
9 | move 4 to s2, push its children on s1 | [] | [1 3 6 9 8 2 5 7 4] | []
|
|
10 | pop 4 from s2 to output | [] | [1 3 6 9 8 2 5 7] | [4]
|
|
11 | pop 7 from s2 to output | [] | [1 3 6 9 8 2 5] | [4 7]
|
|
12 | pop 5 from s2 to output | [] | [1 3 6 9 8 2] | [4 7 5]
|
|
13 | pop 2 from s2 to output | [] | [1 3 6 9 8] | [4 7 5 2]
|
|
14 | pop 8 from s2 to output | [] | [1 3 6 9] | [4 7 5 2 8]
|
|
15 | pop 9 from s2 to output | [] | [1 3 6] | [4 7 5 2 8 9]
|
|
16 | pop 6 from s2 to output | [] | [1 3] | [4 7 5 2 8 9 6]
|
|
17 | pop 3 from s2 to output | [] | [1] | [4 7 5 2 8 9 6 3]
|
|
18 | pop 1 from s2 to output | [] | [] | [4 7 5 2 8 9 6 3 1]
|