This Prolog library provides predicates for handling binary trees. Binary trees are represented as nil
(the empty tree) or as t(L, X, R)
, where L and R are left and right subtrees. The values in binary trees can be numbers or letters. For binary search trees, the values must be integers.
- Download the file
binary_trees_library.pl
. - Load the file into Prolog either in your code or in your interpreter with the following command
use_module('path/to/binary_tree_library.pl').
- Use the predicates.
Below are all the predicates I implemented for the library.
You will find a more detailed usage with code snippets in the binary_tree_library.pl
file.
- Returns the height
H
of the binary treeT
.
- Succeeds if predicate
P
is true for every value in the treeT
.
- Performs a right fold of a predicate over all values in a tree.
- Flattens a tree
T
, producing a list of valuesL
.
- Counts all nodes in a tree.
- Counts the number of leaves in the binary tree
T
.
- Searches for a specified element
X
in a binary treeT
.
- Performs a preorder traversal of a binary tree
T
.
- Performs an in-order traversal of a binary tree
T
.
- Performs a postorder traversal of a binary tree
T
.
- Gives a list
Result
containing all nodes of depthD
in a binary treeT
.
- Gives the depth
D
of a node of valueV
in a binary treeT
.
- Returns true if the node of value
V
is a leaf of the binary treeT
.
- Returns true if the binary tree
T
is full.
- Returns true if the binary tree
T
is perfect.
- Returns true if the binary tree
T
is quasi-perfect.
- Returns true if the binary tree
T
is a binary search tree.
- Searches for a specified element
X
in a binary search treeT
.
- Inserts a value
X
into a binary search treeT
, producing a treeT1
.
- Deletes a specified element
X
from a binary search treeT
, producing a treeT1
.
- Finds the maximum value
Max
in the binary search treeT
.
- Finds the minimum value
Min
in the binary search treeT
.
Here are some examples of trees that I have used to test the code:
my_bst1(T)
- Binary search tree with integer values.my_btree(T)
- Binary tree with non-integer values.my_full_tree(T)
- Full binary tree.my_perfect_tree(T)
- Perfect binary tree (also a binary search tree).my_quasi_perfect_tree(T)
- Quasi-perfect binary tree (also a binary search tree).my_alphabet_tree(T)
- Binary tree with alphabet letters.
Example usage:
?- my_full_tree(_T), is_full(_T).
This library is released under the MIT License (see more information about it in file LICENSE
).