How to serialize and deserialize a general binary tree?
A full Binary is a Binary Tree where every node has either 0 or 2 children. It is easy to serialize such trees as every internal node has 2 children. We can simply store preorder traversal and store a bit with every node to indicate whether the node is an internal node or a leaf node. How to store a general Binary Tree?
When to use / in a binary tree?
For example in the following diagram ‘ is used to indicate an internal node set bit, and ‘/’ is used as NULL marker. The diagram is taken from here . Please note that there are always more leaf nodes than internal nodes in a Binary Tree (Number of leaf nodes is number of internal nodes plus 1, so this optimization makes sense.
What’s the difference between deserialization and serialization?
Serialization is to store tree in a file so that it can be later restored. The structure of tree must be maintained. Deserialization is reading tree back from file.
When does a binary tree have to be complete?
A Binary Tree is complete if all levels are completely filled except possibly the last level and all nodes of last level are as left as possible (Binary Heaps are complete Binary Tree). For a complete Binary Tree, level order traversal is sufficient to store the tree.
How to save space on a binary tree?
A simple solution is to store both Inorder and Preorder traversals. This solution requires space twice the size of Binary Tree. We can save space by storing Preorder traversal and a marker for NULL pointers.
How is a binary tree stored in a file?
We have given a binary tree containing N number of nodes where each node has some value. We need to serialize and deserialize the binary tree. The process of storing a tree in a file without disturbing its structure is called serialization.
What’s the difference between serialization and deserialization in Java?
Serialization is to store tree in a file so that it can be later restored. The structure of tree must be maintained. Deserialization is reading tree back from file. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution.