Tree.SingleRootTree
Tree.SingleRootTree
SingleRootTree node is a thin wrapper around Tree that helps
when you have one root node, and want to be able to reference the root without
having to record the node ID yourself. You create it with a root node,
and the root can never be removed. It exposes the same functionality as
Tree, plus convenience functions for directly operating on the root node,
without having to know its ID.
You can still create multiple roots, since a root is simply a node that is not a child of another node. However, this code tracks one very special root node for you.
Use Tree when you need a general-purpose forest (zero or more
roots). Use SingleRootTree when your data naturally has a single entry
point and you want a more convenient API.
The SingleRootTree manages a Tree nd keeps track of the ID of the single root node.
Create a new tree with a single root node.
tree = SingleRootTree.new "root"
SingleRootTree.nodeCount tree == 1
Functions for the root-node
Get the root node's value.
tree = SingleRootTree.new "root"
SingleRootTree.getRoot tree == Just "root"
Get the children ids of the root node.
tree = SingleRootTree.new "root"
|> SingleRootTree.appendRootChild 1
SingleRootTree.getRootChildren tree == [1]
Get the root node's id.
tree = SingleRootTree.new "root"
SingleRootTree.getRootId tree == 0
Append a child id to the root node's children array.
result = SingleRootTree.insert "child" tree
tree2 = SingleRootTree.appendRootChild result.id result.tree
SingleRootTree.children (SingleRootTree.getRootId tree2) tree2 == [1]
Append multiple child ids to the root node's children array.
tree2 = SingleRootTree.appendRootChildren [1, 2, 3] tree
Replace the root node's entire children array.
tree2 = SingleRootTree.setRootChildren [2, 3] tree
SingleRootTree.children (SingleRootTree.getRootId tree2) tree2 == [2, 3]
Get the root's children with both id and value for each child.
SingleRootTree.getRootChildrenWithNodes tree
== [ { id = 1, node = "childA" }, { id = 2, node = "childB" } ]
Wrappers for Tree functions
Insert a node into the tree. Returns the auto-assigned id and the updated tree.
result = SingleRootTree.insert "child" tree
-- result == { id = 1, tree = ... }
Remove a single node by id. Cannot remove the root node.
trimmed = SingleRootTree.remove 2 tree
Remove a node and all of its descendants recursively. Cannot remove the root node.
trimmed = SingleRootTree.removeRecursive 1 tree
Get the full entry for a node: its value and children ids.
SingleRootTree.get 0 tree
== Just { value = "root", children = [1, 2] }
Get just the value of a node, ignoring its children.
SingleRootTree.getNode 0 tree == Just "root"
Get the children ids of a node.
SingleRootTree.children 0 tree == [1, 2]
Get the values of a node's children, without ids.
SingleRootTree.childrenNodes 0 tree == ["childA", "childB"]
Get the children of a node, with both id and value for each child. Returns an empty array if the node does not exist.
-- Given node 0 has children [1, 2]:
SingleRootTree.childrenWithNodes 0 tree
== [ { id = 1, node = "childA" }, { id = 2, node = "childB" } ]
Find the parent id of a node.
SingleRootTree.parent 1 tree == Just 0
Append a child id to a parent's children array.
tree2 = SingleRootTree.appendChild 0 1 tree
Replace a node's entire children array.
tree2 = SingleRootTree.setChildren 0 [1, 2] tree
Update a node's value by applying a function to it.
tree2 = SingleRootTree.updateNode 0 String.toUpper tree
Replace a node's value entirely, preserving its children.
tree2 = SingleRootTree.replaceNode 0 "goodbye" tree
Transform every node value in the tree.
mapped = SingleRootTree.map String.toUpper tree
Fold over every node in the tree.
values =
SingleRootTree.foldl
(\id value childIds acc -> Array.pushLast value acc)
Array.empty
tree
Get the ids of all root nodes.
SingleRootTree.roots tree == [0]
Get all descendant ids of a node, recursively.
SingleRootTree.descendants 0 tree == [1, 2, 3]
Depth-first traversal starting from a given node.
SingleRootTree.depthFirst 0 tree
== [ { id = 0, value = "root", depth = 0 }, ... ]
Flatten the entire tree into an array of records.
SingleRootTree.toArray tree
== [ { id = 0, value = "root", children = [1] }, ... ]
Get all node ids in the tree.
SingleRootTree.ids tree == [0, 1, 2]
Check whether a node id exists in the tree.
SingleRootTree.isMember 0 tree == True
Get the total number of nodes in the tree.
SingleRootTree.nodeCount tree == 3
Return a hierarchical representation of the Tree for debugging purposes. It needs a helper function to convert a node to a String.
SingleRootTree.prettyPrint identity tree
-- "(0) root\n (1) child"