Get an adjacency list structure.
Get an adjacency list structure.
the index of the root node in the parents list
parents(i) refers to the node id of the parent of i. For the root node, parents(i) == i. Since there is only one root to a tree, there should only be one index i such that parents(i) == i.
a map-based adjacency list structure where keys represent parents, values are child lists.
type of nodes
type of collection for children
the implementation of the tree
a list of nodes.
index in nodes of the root node.
a function taking a node that returns an ID
a function taking a node and a that returns ID for it children
a builder function
Build a tree from a sequence of nodes in some raw form with a parent list.
Build a tree from a sequence of nodes in some raw form with a parent list.
This is useful when it is difficult to actually build the tree from the bottom up. This method can efficiently build a tree from the bottom up given a tree in a tabular format. O(n) time and O(n) auxiliary space.
For a binary search tree with three nodes, we may want to do something like the following:
scala> case class StringTree(value: String, subtrees: Seq[StringTree]) extends Tree[String, StringTree] defined class StringTree scala> val f = (v: String, c: Seq[String], ch: Seq[StringTree]) => StringTree(v, ch) f: (String, Seq[String], Seq[StringTree]) => StringTree = <function3> scala> Tree(Seq("zero", "one", "two"), Seq(1,1,1), f) res0: StringTree = StringTree(one,List(StringTree(zero,List()), StringTree(two,List())))
the value type of the tree
the tree representation
a sequence of nodes in raw form
parents(i) refers to the node id of the parent of i. For the root node, parents(i) == i. Since there is only one root to a tree, there should only be one index i such that parents(i) == i.
a function that builds a tree given a node in its raw form and its children in raw form and tree form.
a tree of type TreeImpl
Build a tree from a sequence of nodes in some raw form with an adjacency list.
Build a tree from a sequence of nodes in some raw form with an adjacency list.
This is useful when it is difficult to actually build the tree from the bottom up. This method can efficiently build a tree from the bottom up given a tree in a tabular format. O(n) time and O(n) auxiliary space.
For a binary search tree with three nodes, we may want to do something like the following:
scala> case class StringTree(value: String, subtrees: Seq[StringTree]) extends Tree[String, StringTree] defined class StringTree scala> val f = (v: String, c: Seq[String], ch: Seq[StringTree]) => StringTree(v, ch) f: (String, Seq[String], Seq[StringTree]) => StringTree = <function3> scala> Tree(Seq("zero", "one", "two"), Map(1 -> List(0,2)), 1, f) res0: StringTree = StringTree(one,List(StringTree(zero,List()), StringTree(two,List())))
the value type of the tree
the container type of the descendants in the tree representation
the tree representation
a sequence of nodes in raw form
an adjacency list
the index of the root in the nodes parameter
a function that builds a tree given a node in its raw form and its children in raw form and tree form.
a tree of type TreeImpl
Determine the root node.
Determine the root node.
parents(i) refers to the node id of the parent of i. For the root node, parents(i) == i. Since there is only one root to a tree, there should only be one index i such that parents(i) == i.
Some(i) if there exists an i such that i == parents(i); otherwise, None
Determine the topological sort order where the root of the tree is visited last.
Determine the topological sort order where the root of the tree is visited last.
the tree root
a map-based adjacency list structure where keys represent parents, values are child lists.
the number of nodes in the tree
zero-based order where i is the node and a(i) is the order to visit the node.