Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
21.2. Flatten tree records and keep each node
Medium
Topics
This question is in
21. Recursion and nested dataTrees arrive nested: a menu with submenus, a folder with folders, a comment with replies, each node holding a children array. Tables, search indexes and virtualized lists want the opposite: one flat list of rows, where each row still knows where it sat in the tree.
Write flattenTree(roots) in flatten-tree.js. It walks the tree depth first and returns one row per node. A row has the node's own fields without children, plus three fields that record the node's place: parentId, depth and path.
Signature
function flattenTree(roots) // returns an array of rowsroots: an array of root nodes, or a single root node. A node is an object with anid, any other fields, and optionallychildren, an array of nodes.- Returns a new array of new objects. Each row is the node's fields except
children, then:parentId: theidof the node's parent, ornullfor a root;depth: 0 for a root, one more for each level down;path: an array of ids from the root down to this node, the node's own id last.
Requirements
- A root becomes a row with
parentId: null,depth: 0andpath: [id]. - A child's row has its parent's
id, the parent's depth plus one, and the parent's path plus its own id. - Rows are in depth-first order: a node, then all of its descendants, then its next sibling. Children keep their order.
rootsmay be an array (several roots, or none) or one node object.- Rows have no
childrenfield. Every other field is kept as it is, including0,"",falseandnull; object fields are not copied deeply. - Rows are new objects, and the tree is not changed.
- Each row has its own
patharray: changing one row's path never changes another's. - An
idof0is a real id: its children getparentId: 0. - A missing,
nullor emptychildrenmeans a leaf.null,undefinedand holes inside achildrenarray are skipped. - A node that appears under two parents is listed under each, with that parent's
parentIdandpath. - A node that contains itself (directly or deeper) makes
flattenTreethrow aTypeError. parentId,depthandpathalways describe the tree, even if a node already has fields with those names.- A chain 2,000 levels deep works.
Examples
flattenTree({
id: "docs",
children: [
{ id: "guide", title: "Guide", children: [{ id: "install" }] },
{ id: "api" },
],
});
// [
// { id: "docs", parentId: null, depth: 0, path: ["docs"] },
// { id: "guide", title: "Guide", parentId: "docs", depth: 1, path: ["docs", "guide"] },
// { id: "install", parentId: "guide", depth: 2, path: ["docs", "guide", "install"] },
// { id: "api", parentId: "docs", depth: 1, path: ["docs", "api"] },
// ]flattenTree([]); // []
flattenTree([{ id: 0, children: [{ id: 1 }] }]); // the second row has parentId 0const node = { id: "x", children: [] };
node.children.push(node);
flattenTree(node); // throws TypeErrorEnvironment
- Plain JavaScript in a browser page; the tests call the global
flattenTreefromflatten-tree.js. - Rebuilding a tree from rows is not part of this problem.
Hints
- Besides the node, what does a recursive call need to know to fill in
parentId,depthandpath? - Which nodes can make a cycle: every node seen so far, or only the ones you are still inside of?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it
Back to the question
Loading the editor…