Next: bst bnodes iterating breadth-first, Previous: bst bnodes iterating post-order, Up: bst bnodes iterating [Index]
Level–order iteration: visit the tree level by level. Example:
5-------10----12 | | | 1--3--4 7--9 11 | | | 2 6 8
the order of the forwards iteration is: 5, 1, 10, 3, 7, 12, 2, 4, 6, 9, 11, 8. To do it we need a moving cursor that always “turns right” keeping the count of the level. The order of the backwards iteration is: 5, 10, 1, 12, 7, 3, 11, 9, 6, 4, 2, 8.
The implementation of the level–order iteration is particularly slow, but it has the current node as the only state. The level–order iteration is usually called “breadth–first iteration” or “breadth–first search”, that name is reserved for another implementation (see Breadth–first iterations).
The following syntactic bindings are exported by the library
(vicare containers binary-search-trees)
. The bindings whose
name is prefixed with $
are unsafe operations: they do
not validate their arguments before accessing them.
Like fold-left
for lists, visit the nodes of a binary tree
with a level–order forwards iteration. The argument root must be
#f
to represent an empty tree or an instance of
<binary-node>
representing the root of a tree.
Perform the first step in a level–order forwards iteration. The argument
root must be #f
to represent an empty tree or an instance of
<binary-node>
representing the root of a tree. If the tree is
empty: return #f
; otherwise return an instance of
<binary-node>
representing the first visited node.
Advance a level–order forwards iteration by one step starting from
bnode, which must be an instance of <binary-node>
. If
bnode is the last in the iteration: return #f
; otherwise
return an instance of <binary-node>
representing the next
visited node.
Like fold-right
for lists,a visit the nodes of a binary tree with
a level–order backwards iteration. The argument root must be
#f
to represent an empty tree or an instance of
<binary-node>
representing the root of a tree.
Perform the first step in a level–order backwards iteration. The argument
root must be #f
to represent an empty tree or an instance of
<binary-node>
representing the root of a tree. If the tree is
empty: return #f
; otherwise return an instance of
<binary-node>
representing the first visited node.
Advance a level–order backwards iteration by one step starting from
bnode, which must be an instance of <binary-node>
. If
bnode is the last in the iteration: return #f
; otherwise
return an instance of <binary-node>
representing the next
visited node.
Next: bst bnodes iterating breadth-first, Previous: bst bnodes iterating post-order, Up: bst bnodes iterating [Index]