Mark As Completed Discussion

Good evening! Here's our prompt for today.

Given a binary tree representation, can you come up with a method to serialize the tree and then deserialize it to get the exact same tree?

Description

What is Serialization and Deserialization?

Serialization is a process that converts a data structure to a suitable format that can be stored either on a file or in memory. It can also be a format for data transmission across a network. Deserialization is the reverse process of recovering the original data structure. In this tutorial, I will show you how you can convert a binary tree with integer keys into an integer array and vice versa. You can then save the integer array in a text or binary file. You can also convert this integer array into a suitable format for data transmission over a network.

There are many ways of serializing a binary tree. Most of the methods involve traversing the tree, while storing each key in a data structure. Understanding a traversal is therefore the key to understanding the serialization and its reverse deserialization process. In this tutorial, we'll look at preorder traversal and how it can be used for serializing and deserializing a binary tree.

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's how we would solve this problem...

How do I use this guide?

Preorder Traversal of a Binary Tree

There are many ways of traversing a tree, depth first or breadth first. For depth first traversal, the most popular methods are inorder, preorder and postorder traversal. The rule for preorder traversal is:

SNIPPET
1Process root, process left sub-tree, process right sub-tree

What does this rule mean for a binary tree? The figure below shows the recursive preorder traversal of all keys in the binary tree.

Preorder Traversal of a Binary Tree

Pseudo-code for Preorder Traversal

The simplest algorithm for preorder traversal exploits the recursive structure of a binary tree. The easiest method is to write a recursive preorder traversal routine as given here.

You can go back and look at the figure to see how the preorder recursive routine is working.

TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Serializing Using Preorder Traversal

We can modify the preorder routine to serialize a binary tree. All we need is a designated key to specify a null link. For this example, let's assume that we can only store zero or positive keys in the tree. Here, we can use -1 as a special key to denote a null link. The figure below shows the output array for the tree of our earlier example.

Serializing Using Preorder Traversal

The pseudo-code for the serialization process is given below:

TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Deserialization Using Preorder Traversal

If we serialize using preorder traversal, we have to apply the same preorder procedure to recursively build the tree. We'll take the keys from the array one by one and build the tree accordingly. The routine arrayToTree takes two arguments, i.e., the array of keys and an index. The base case would be true either when index passed is out of bound or if the key at index position is -1. Calling the routine with arrkeys and index = 0, would return the root node. The figure below illustrates how the tree is built.

Deserialization Using Preorder Traversal
TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Java Code for Serialization and Deserialization

The Java code for both serialization (treeToArray method) and deserialization (arrayToTree method) is given below. For the treeToArray method, I have used an ArrayList object to add keys to the list. ArrayList is a convenient data structure to use if we don't know the size of the array in advance.

Note that for all the recursive routines, we need a public function to be called from outside the class and a corresponding helper function.

Wrapping it up

I hope you enjoyed working on this problem. Note, preorder traversal is not the only method of serialization. You can use methods based on inorder or postorder traversal. You can also use a level order traversal if you wish. Do try this out for different trees and see how the recursion is working. If n is the number of keys in the tree, then both serialization and deserialization have a complexity of O(n), as each key is traversed only once. Also, the array for storing the binary tree requires O(n) space as all keys in addition of null links are stored. Even if the links are double the size of total keys, the space complexity remains O(n).

JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

One Pager Cheat Sheet

  • We can serialize and deserialize a binary tree by using preorder traversal to convert it to an integer array.
  • By using the preorder traversal algorithm, the root node, left sub-tree, and right sub-tree of a binary tree can be processed in that order.
  • We can serialize a binary tree using preorder traversal, using a designated key -1 to specify a null link.
  • We can deserialize a binary tree by recursively building it using preorder traversal.
  • The Java code for serialization (treeToArray) and deserialization (arrayToTree) of a binary tree has a complexity of O(n) and a space complexity of O(n), as all keys and null links are stored.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Great job getting through this. Let's move on.

If you had any problems with this tutorial, check out the main forum thread here.