BST
A binary search tree is a binary tree whose inorder walk is sorted. For every node, every value in the left subtree is less, and every value in the right subtree is greater (or not-less, if you allow duplicates on one side).
Detailed Explanation
Search follows the same comparison you used to insert. Start at the root. If the key is smaller, go left. If larger, go right. If equal, you found it. Average time is O(height). In a balanced tree height is log n. In a tree that received sorted input and always inserted right, height is n and you have a linked list.
Insert walks the same path and hangs a new leaf where it fell off. Delete has three cases: a leaf is a unlink; a one-child node is replaced by that child; a two-child node is replaced by its inorder successor (the leftmost node of the right subtree) and then that successor is deleted. Missing a case leaves a cycle or drops a subtree.
C implementations store int keys or a void * plus a compare function. The compare must be a consistent order: cmp(a,b) < 0 iff cmp(b,a) > 0, and equality is transitive. A compare that uses a changing field will corrupt the tree.
Balancing is extra machinery: AVL rotations, red-black colors, treaps. Without it, you should not advertise O(log n). If the keys are random, a plain BST is often fine. If the keys are timestamps or autoincrement ids, they are sorted, and a plain BST is a list. Use a balanced tree, a hash table, or an array plus bsearch.
An inorder walk prints keys in order without a separate sort. That is the property that makes a BST worth drawing on a whiteboard. It is also a way to test the tree: if inorder is not sorted, an insert or delete is wrong.
Code Examples
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *left;
struct Node *right;
};
static struct Node *insert(struct Node *n, int value)
{
if (n == NULL) {
struct Node *leaf = malloc(sizeof *leaf);
if (leaf == NULL) {
return NULL;
}
leaf->value = value;
leaf->left = leaf->right = NULL;
return leaf;
}
if (value < n->value) {
n->left = insert(n->left, value);
} else if (value > n->value) {
n->right = insert(n->right, value);
}
return n;
}
static int contains(const struct Node *n, int value)
{
while (n != NULL) {
if (value == n->value) {
return 1;
}
n = (value < n->value) ? n->left : n->right;
}
return 0;
}
static void tree_free(struct Node *n)
{
if (n == NULL) {
return;
}
tree_free(n->left);
tree_free(n->right);
free(n);
}
int main(void)
{
struct Node *root = NULL;
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
printf("%d %d\n", contains(root, 3), contains(root, 4)); /* prints: 1 0 */
tree_free(root);
return 0;
}Common Gotchas
Sorted insertion builds a list
`O(n)` search. If you cannot bound the shape, use a hash table or a balanced tree.
Delete of a two-child node that does not steal the successor correctly drops a subtree
test delete in all three cases.
Duplicates need a policy
reject, store a count, or send equals left (or right) consistently. Mixing sides breaks search.
A compare function that mutates the keys, or that disagrees with itself, is undefined in practice
the walk will miss nodes that are still reachable the other way.