Suppose I am writing a function fromPaths(paths: List[String]): Node to build a tree from a few node paths like this:
case class Node(value: String, children: List[Node])
val paths = List("a/b/x", "a/b/y", "a/c", "a/c/d")
fromPaths(paths) // Node("a", List(Node("b", List(Node("x"), Node("y"))), Node("c", List(Node("d")))))
I can write a function addNode(root: Node, path: String): Node and then just fold it over the list. However it looks suboptimal since we traverse the tree from root to node for each "path" in paths.
How would you optimize fromPaths for the overall number of traversed nodes ?
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)