Serialize And Deserialize A Given N-Ary Tree
//Serialize the tree.Writing the tree values to a file or array is serialization. //Storing the data from the tree in an array. Or we can store the node itself in an object array.We have to //design accordingly.
I recently faced this question during the interview and the interviewer asking me to create two function. Function1 should take the n-ary tree and convert to byte array and function2 should take the byte[] and build the n-ary tree. If it was a binary tree, i would have made pre-order traversal with the special character for null and stored in an array and converted to byte[] but here n-ary tree (with many children). I don't know how to store this and rebuild the n-ary tree with an array. Any ideas or formula to store this n-ary tree into array? I appreciate your help. It is needed to distinguish between two cases: 1.
Complete n-ary Trees and 2. Sparse n-ary or non-Complete Trees. In the first case, assuming that N=5 so each level i in the Tree will have exactly (i.e. No less, no more) 5^i nodes; on the other hand, in the second case, this rule isn't valid since the Tree can be filled randomly by construction. Complete n-ary Trees can be serialized into an array; simply extending from the complete Binary Search Tree: nodes (intermediary and leaves) are related each other by the level i and the actual N by the forum Ni+1+c* where c is the c-th child. Adopting a level-order traversal, the Tree can be optimally serialized in an array of bytes (no further characters are needed, read hereafter). A comprehensive explanation.
This way, you won't miss a beat when you're travelling from room to room. Soundbars & SoundPlatesTM: Featuring sleek, slim designs and superior sonic depth, our Soundbars and SoundPlates will bring your TV's audio system to a whole new level. With features like Bluetooth technology, CD players, and multiple USB inputs, our home audio systems give you a range of formats to choose from. Shelf Stereo Systems: Get powerful sound from a robust system of speakers designed to work together.
Unfortunately, for the non-Complete n-ary Trees, as anticipated, the above formula doesn't apply. Therefore a different approach needs to be adopted. One approach would consist in serializing special characters indicating the absence of children, or better serializing the NULL values of the children pointers.
This approach is not optimal, since a further number of characters are needed (totally N additional bytes are needed) but it's a pretty straightforward and viable one. Let's examine an example: A / B C D / E F G adopting a pre-order traversal, the above non-Complete n-ary Tree is serialized as follows A B E / F / / C / D G / / / the '/' maps the NULL and provides the ability to deserialize the Tree into the original one.
A pre-order traversal is adopted to visit the Tree and output the above array of characters. In total *2*N* bytes are serialized, since in that case the Tree values are exactly 1 character each. As deserialization algorithm, still the pre-order traversal can be adopted: few modifications are needed to recognize the NULLs mapping pattern, as above represented. A C++ code example is. Concluding, serializing and deserializing a non-Complete or sparse n-ary Tree is a bit more tricky and needs a further amount of bytes to forcely map the NULLs. Looks to me like a good use for recursion. Write a method (subroutine, function) that writes out one node and all the tree below it.
It would write out the node, then write out the number of children (zero for leaf nodes), then call itself to write each child if there are any. Now call the method on the top node in the tree and you've serialized it. To deserialize, write a method that reads in a node. It would read in the node itself, read in the number of child nodes, then read in each child node, if any. Call it once on the stream and it will hand you the top node with all the descendant nodes in place--the entire tree.
Harsathu patti enna solla. Manasu iruka manasu iruka akka magale mp3 song download.
The moral of this story is that recursion (which is really just a convenient way to get and use a stack) is really useful for working with graphs. And a lot more things than you might think are graphs.