Root
The only node without a parent; it is the structure’s entry point.
Algorithms · Data structures
A binary search tree keeps keys in an order that guides every decision: smaller to the left, larger to the right. Its efficiency depends on the tree’s shape, not only on its number of nodes.
For every node v: chiavi(sinistra(v)) < v < chiavi(destra(v))
The condition must hold for entire subtrees, not only for the two immediate children. For clarity, the lab rejects duplicates; a real implementation must choose and document a consistent policy.
The only node without a parent; it is the structure’s entry point.
A node with no children. A node may also have exactly one child.
Number of edges from the root to a node. The root has depth 0.
Number of levels on the longest path. Here an empty tree has height 0 and a leaf height 1.
Nodes are selectable. Rotations act on the selected node; search, insertion and deletion show the comparison path.
In-order
Pre-order
Post-order
Compare the key with the node: stop if equal, go left if smaller, right if larger.
Follow the same path as search and attach a new leaf at the first empty pointer.
Remove a leaf; replace a one-child node with its child; for two children, use the in-order successor.
A rotation is a local Θ(1) update. It preserves every ordering relation, so the in-order traversal remains unchanged.
x y
/ \ / \
A y → x C
/ \ / \
B C A B
Requires a right child y. y is promoted; subtree B moves from the left of y to the right of x.
y x
/ \ / \
x C → A y
/ \ / \
A B B C
This is the inverse operation. It requires a left child x and moves B from the right of x to the left of y.
| Operation | Cost | Balanced tree | Worst case |
|---|---|---|---|
| Search | Θ(h) | Θ(log n) | Θ(n) |
| Insertion | Θ(h) | Θ(log n) | Θ(n) |
| Deletion | Θ(h) | Θ(log n) | Θ(n) |
| Minimum / maximum | Θ(h) | Θ(log n) | Θ(n) |
| One rotation | Θ(1) | Θ(1) | Θ(1) |
| Full traversal | Θ(n) | Θ(n) | Θ(n) |
Each comparison removes roughly half the candidates.
Already sorted insertions may create a linked list disguised as a tree.
Left, node, right. In a BST this yields sorted keys.
Node, left, right. Useful for serializing or reconstructing shape.
Left, right, node. Useful when children must be processed before their parent.