Binary Tree Serialization
Introduction
What is serialization?
"In the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment."
For example, if we want to serialize an array (data structure) [1, 2, 3] we can just store as (string) [1, 2, 3].
If we want to serialize a linked list, 1 -> 2 -> 3, then we can store as 1 -> 2 -> 3.
If we want to serialize a hashmap, then we can store as {"key" : "value"}. (doesn't this remind you of json?)
Some applications in practice are XML, JSON, Protocol Buffers, etc.
Question (LC.297)
Design an algorithm to serialize and deserialize a binary tree.
Example
1
/ \
2 3
/ \
4 5
Serialized: [1, 2, 3, null, null, 4, 5]Analysis
How do we do this for a binary tree? We need to store the nodes in a certain order. What order should we pick We want the ones that the root is easy to identify. Aka preorder and level order (root will be stored first).
Approach 1: BFS (Level Order Traversal)
Serialize
Step 1 Construct Queue
Step 2 Convert the queue to string
Deserialize
BFS on the encoded string
Notes: The goal of this approach is to make the thinking process as straightforward as possible. To be more elegant, we can store nulls[i] for all i to count the number nulls so far at each nodeValue. Then the left child will be values[2 * (i - nulls[i]) + 1] and the right child will be values[2 * (i - nulls[i]) + 2].
Approach 1 Code
Approach 2: DFS (Pre-order Traversal)
Serialize
Just a regular preorder traversal
Use a notebook (called result) to travel around the tree
Deserialize
We want to build this tree top down
Naturally, the recursive divide and conquer fits this role
Approach 2 Code
Reference
Wikipedia Serialization
Last updated