This module provides predicates for manipulated trees. The tree data-type is
defined polymorphically over payload data types A as:
tree(A) ---> node(A, list(tree(A))).
Thus, in this representation, data of type A is associated with each node
including the root node, and every node has list of child nodes, which will
be empty for leaf nodes.
- empty_tree(?N:A, ?T:tree(A)) is det
- Unify T with a root node tree containing N.
- tree_node(+T:tree(A), -N:tree(A)) is nondet
- Unifies N with all the nodes in a tree.
- node_label(+N:tree(A), -X:A) is det
- node_children(+N:tree(A), -C:list(tree(A))) is det
- maptree(P:pred(?(X:A)), ?TA:tree(A)) is nondet
- maptree(P:pred(?(X:A),?(Y:B)), ?TA:tree(A), ?TB:tree(B)) is nondet
- Map over tree using unary or binary predicate P.
- tree_depth(+T1:tree(_), -D:natural) is det
- Maximum depth of tree.
- tree_canonical(+T1:tree(A), -T2:tree(A)) is det
- Construct canonical form of a given trie by sorting all children into standard order.
- print_tree(+T:tree(A)) is det
- print_tree(+Pre:atom, +T:tree(A)) is det
- Prints a drawing of the tree. It uses unicode box-drawing characters to
draw the edges so your terminal must be set-up correctly to show these
properly. A node with data X is labeled with whatever
print(node(X))
produces and so can be customised by declaring clauses of portray/1.
If the prefix Pre is supplied, the tree is started at the current position
in the output stream, but subsequent new lines are prefixed with Pre,
to allow arbitrary indenting.
If the child list for any node is a frozen variable, the variable is
unfrozen.
- tree_cursor(+T:tree(A), -C:cursor(A)) is det
- Constructs a cursor representing the given tree and a current position
within that tree. Uses a zipper-like data type, as described in the functional
programming literature. Initial position is the root node.
- get_node(-N:tree(A))// is det
- Gets the subtree N at the current cursor position.
- set_node(+N:tree(A))// is det
- Replaces the subtree at the current cursor with N.
- cursor_node(+C:cursor(A), -N:tree(A)) is det
- Relation between a cursor and the subtree at the current position.
- cursor_move(+Dir:oneof([down,up,left,right]), +C1:cursor(A), -C2:cursor(A)) is semidet
- Move a cursor around the tree. Dir can be one of:
- up - move to the parent of the current node if not at the root already.
- down - move to the current node's first child if it exists.
- right - move to the current node's next sibling if there is one.
- left - move to the previous sibling if not already the first.
- cursor_add_sibling(?N:tree(A), +C1:cursor(A), -C2:cursor(A)) is det
- Add a N sibling after the current node and move the cursor to it.
- cursor_ins_sibling(?N:tree(A), +C1:cursor(A), -C2:cursor(A)) is det
- Insert a sibling N before the current node and move the cursor to it.
- cursor_ins_child(?N:tree(A), +C1:cursor(A), -C2:cursor(A)) is det
- Insert a child of the current node as first in the list of children and
move down to the newly inserted node.
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.
- print_tree(Arg1, Arg2)
- maptree(Arg1, Arg2, Arg3)