A traversal visits every node in a tree exactly once; the only thing that varies is the order in which a node is processed relative to its children. Binary trees admit three classic depth-first orderings — inorder, pre-order, and post-order — each of which has distinct algorithmic uses. Choosing the right traversal can mean the difference between producing a sorted sequence, serialising the tree structure, or computing a value that depends on all descendants before their ancestor. The UCALDAS BST implementation provides all three, plus a visualDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/ed/llms.txt
Use this file to discover all available pages before exploring further.
print_tree helper.
Inorder (Left → Root → Right)
Inorder traversal processes the left subtree first, then the current node, then the right subtree. Because of the BST ordering property, this always visits nodes in ascending sorted order — a useful property for producing sorted output or checking whether a tree is a valid BST.inorder method
inorder defaults to starting at the root when called with no arguments, but it can also be called on any subtree root by passing a node explicitly — useful for partial traversals during debugging.
Pre-order (Root → Left → Right)
Pre-order traversal processes the current node first, then recurses into the left subtree, then the right subtree. The root is always the first node printed. This order is commonly used to serialise or clone a tree, because inserting nodes in pre-order sequence into a new BST recreates the exact same structure.pre_order and __pre_order methods
pre_order method handles the empty-tree guard and prints the label, then delegates the recursive work to the private __pre_order helper. The name-mangling prefix (__) follows Python convention for implementation-private methods that should not be called directly.
Post-order (Left → Right → Root)
Post-order traversal processes both subtrees before the current node. The root is always the last node printed. This order is natural for operations that must fully process all descendants before acting on an ancestor — for example, computing the size of each subtree, or deleting an entire tree without leaving dangling parent pointers.pos_order and __pos_order methods
Complete Usage Example
The following example builds a small BST and runs all three traversals, demonstrating how the same set of nodes produces three different orderings:All three traversals on the same tree
20 30 40 50 70), pre-order starts with the root (50), and post-order ends with the root (50).
Printing the Tree
Theprint_tree method produces a sideways ASCII representation of the tree structure — right subtree at the top, left subtree at the bottom — which is much easier to read in a terminal than a flat list of values.
print_tree method
Printing the tree visually
┌── connector marks a right child; └── marks a left child. Vertical bars (│) show where a branch continues past the current level.
print_tree is a pre-order traversal internally: it processes the right subtree (printed above), then the current node, then the left subtree (printed below). The visual rotation is a side-effect of printing right-to-left when displayed vertically in a terminal.Traversal Comparison
| Traversal | Visit Order | Output for [50, 30, 70, 20, 40] | Common Use |
|---|---|---|---|
| Inorder | Left → Root → Right | 20 30 40 50 70 | Sorted output, BST validation |
| Pre-order | Root → Left → Right | 50 30 20 40 70 | Tree serialisation, cloning |
| Post-order | Left → Right → Root | 20 40 30 70 50 | Tree deletion, expression evaluation |