This module provides non-mutating insert and delete operations as well as common operations such as walking the tree. It also features “nested” versions of insert, delete, and search functions, that allow operations on nested trees.
The empty tree is represented by a #nil value. For example, to create a tree with one node, you could call this expression:
(aa-insert #nil ‘animals ‘moose)
AA node keys may be either numbers, strings, or symbols. Some functions will also take a list of multiple keys instead of a single key, which signals that a nested operation is intended.
A “nested” tree refers to a tree which is the value of a node in another tree. For convenience, aa-delete, aa-insert, and aa-search can operate on a set of nested trees, by receiving a list of keys instead of a single key. aa-insert can even create nested trees in this way. This allows you, in this respect, to treat a tree like a multi-dimension array. For example, in a game you might want to store data like so:
(aa-insert world-tree (list ‘rooms ‘kitchen ‘object-descriptions ‘knife) “A sharp cutting tool.”)
Scheme procedure: aa-delete tree key
Performs a non-mutating delete on tree. Deletes the node in tree which has key key. If there is no such node, it simply returns tree. O(log n) worst case efficiency.
If a list of keys is provided instead of a single key, aa-delete will attempt a nested tree delete, starting the search with the first key in the list. If a nested tree of some required level does not exist, it will simply return the original tree unmodified.
Scheme procedure: aa-insert tree key #:optional value)
Performs a non-mutating insert on tree, inserting a node having key key and value value. O(log n) worst case efficiency.
If a list of keys is provided instead of a single key, aa-insert will attempt a nested tree insert, starting the search with the first key in the list. If a nested tree of some required level does not exist, that tree will be created and inserted into the the previous level tree.
Scheme procedure: aa-map fn tree
Maps fn over the key-value pairs in tree, moving from smallest to greatest key. So, fn must take an argument of the form (key . value). O(n) efficiency.
Scheme procedure: aa-map-keys fn tree
Maps fn over the keys in tree, moving from smallest to greatest key. O(n) efficiency.
Scheme procedure: aa-map-reverse fn tree
The equivalent of aa-map, but moves from greatest to smallest key. O(n) efficiency.
Scheme procedure: aa-map-values fn tree
Maps fn over the values in tree, moving from smallest to greatest key. O(n) efficiency.
Scheme procedure: aa-search tree key
Searches for the node in the tree with key key, and returns the value of that node. If such a node is found, the value will be returned wrapped in a single element list. If no such node is found, a #nil value will be returned. So, to determine whether or not a node was found, you can simply do a nil? test on the result. This approach is helpful, as it is always possible that a node with the correct key might be storing a value such as #nil or #f. O(log n) worst case efficiency.
If a list of keys is provided instead of a single key, aa-search will attempt a nested tree search, starting the search with the first key in the list. If a nested tree of some required level does not exist, the function will simply return #nil.
Scheme procedure: aa-to-list tree
Returns the key value pairs of tree in list form, starting with the smallest key. O(n) efficiency.