This package uses binary trees to represent arrays of N elements
where N is fixed, unlike library(arrays)
. To load the
package, enter the query
| ?- use_module(library(trees)).
Binary trees have the following representation: t
denotes the
empty tree, and t(
Label,
Left,
Right)
denotes the
binary tree with label Label and children Left and
Right.
gen_label(
?Index,
+Tree,
?Label)
Label labels the Index-th element in the Tree. Can be
used to enumerate all Labels by ascending Index. Use
get_label/3
instead if Index is instantiated.
get_label(
+Index,
+Tree,
?Label)
Label labels the Index-th element in the Tree.
list_to_tree(
+List,
-Tree)
Constructs a binary Tree from List where
get_label(
K,
Tree,
Lab)
iff Lab is the
Kth element of List.
map_tree(
:Pred,
+OldTree,
-NewTree)
OldTree and NewTree are binary trees of the same shape and
Pred(Old,New) is true for corresponding elements of the two trees.
put_label(
+I,
+OldTree,
+Label,
-NewTree)
Constructs NewTree which has the same shape and elements as
OldTree, except that the I-th element is Label.
put_label(
+I,
+OldTree,
?OldLabel,
-NewTree,
?NewLabel)
Constructs NewTree which has the same shape and elements as
OldTree, except that the I-th element is changed from
OldLabel to NewLabel.
tree_size(
+Tree,
?Size)
Calculates as Size the number of elements in the Tree.
tree_to_list(
+Tree,
?List)
Is the converse operation to list_to_tree/2
. Any mapping or
checking operation can be done by converting the tree to a list, mapping
or checking the list, and converting the result, if any, back to a tree.