in Education by
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)

1 Answer

0 votes
by
The obvious recursion seems to work just fine, at least on your examples: case class Node[A](a: A, children: List[Node[A]]) def trieForest[A](lists: List[List[A]]): List[Node[A]] = { lists.filter(_.nonEmpty).groupBy(_.head).map { case (k, vs) => Node(k, trieForest(vs.map(_.tail))) }.toList } def fromPaths(paths: List[String]): Node[String] = { // assumes that there is only one tree in the // top-level forest. trieForest(paths.map(_.split("/").toList)).head } println(fromPaths(List("a/b/x", "a/b/y", "a/c", "a/c/d"))) Prints (up to indentation): Node(a, List( Node(b,List( Node(y,List()), Node(x,List()) )), Node(c,List( Node(d,List()) )) )) It can't run much faster asymptotically, because you have to look at every part of the input at least once.

Related questions

0 votes
    I have a page with PrimeNG tree and an autocomplete field. My requirement is Tree should be expanded to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    Five node splitting operations occurred when an entry is inserted into a B-tree. Then how many nodes are written? a) 14 b) 7 c) 11 d) 5...
asked Dec 23, 2020 in Technology by JackTerrance
0 votes
    Which node type represents the root-node of the DOM tree? (a) Document (b) DocumentFragment (c) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    In B+ tree the node which points to another node is called (a) Leaf node (b) External node (c) ... and Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    If a node is locked in __________ explicit locking is being done at a lower level of the tree, but ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    If a node is locked in an intention mode, explicit locking is done at a lower level of the tree. ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    B-tree of order n is a order-n multiway tree in which each non-root node contains __________ a) at most (n – 1)/2 keys ... ) at least 2n keys d) at least (n – 1)/2 keys...
asked Dec 23, 2020 in Technology by JackTerrance
0 votes
    In a B+-tree index ______ for each value, we would normally maintain a list of all records with ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Below is a crude for-loop to illustrate what I need to do. Basically, if there are any 'Variable' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Below is a crude for-loop to illustrate what I need to do. Basically, if there are any 'Variable' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I want to optimize this code instead of using td(String.valueof(dataset.get())) mutliple times. I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    How can the nodes in the node list be accessed? (a) Key (b) Index number (c) Looping (d) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    I'm using this script for building application from command line: #!/bin/bash TARGET="signtest" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
...