Save a tree with marks
transcript
A tree must go into a file. The file must give it back. Walk in preorder. Mark every gap.
Start at eight. Go left to three. One has no children. Write two hashes. Then six, with two hashes. Then ten, a hash, and fourteen.
Keep a stack of waiting nodes. The last one gets the next token. Left slot first, then right. A full node leaves the stack.
A tree can be a chain. One hundred thousand nodes nest deep. Python allows about one thousand calls. A stack has no such limit.
Saving uses a stack too. Push the right child first. Then push the left child. The left child is on top, so it comes out first.
Twelve then three is twelve, comma, three. One then twenty three is one, comma, twenty three. Without commas, both read as one two three. So always write the commas.
Save with marks. Load with a stack. Linear time, no recursion. Full breakdown in the lecture.