January 2018
Intermediate to advanced
332 pages
7h 36m
English
Now that we have the pseudo code in place, the code for serialization becomes quite simple, let us add the following to a file called recursion.js next to our serializer:
var _ = require('lodash');class Recursion { constructor(tree) { this.tree = tree; } // serialize method which accepts list of nodes serialize(nodes) { // initialize response this.currentState = this.currentState || ''; // loop over all nodes _.forEach(nodes, (node) => { // depth first traversal, extracting nodes at each level // traverse one level down var childNodes = this.tree[node]; // add current node to list of serialized nodes this.currentState += ` ${node}`; // has child nodes if (childNodes) { // recursively repeat this.serialize(childNodes); } ...
Read now
Unlock full access