A hash table maps a key to a bucket with a hash function, giving average O(1) insert, lookup and delete but no ordering of keys. A binary search tree (BST) keeps keys in sorted order, so lookups cost O(log n) when the tree is balanced and O(n) when it degenerates, but it supports ordered traversal, minimum, maximum and range queries. Choose a hash table for fast exact-key lookups and a BST when you need sorted data.
C has neither in its standard library, so you write them yourself. This article gives compact, correct implementations of a chained hash table and a BST, then compares them.
A hash table stores entries in an array of buckets. A hash function turns a key into an index, usually by taking the hash modulo the array size. Two keys can land in the same bucket (a collision). The two classic remedies are separate chaining (each bucket is a linked list) and open addressing (probe other slots).
The version below uses chaining, a fixed number of buckets and integer keys. Real tables also resize when the load factor (entries divided by buckets) grows, typically around 0.75, which keeps chains short.
Keys 1 and 17 collide because both are 1 modulo 16, and the chain handles it. Unlike many tutorials, ht_put updates an existing key instead of inserting a duplicate, and ht_get returns a status so that a stored value of -1 is not confused with "not found".
In a BST every node's left subtree holds smaller keys and its right subtree holds larger keys. Searching compares the key and goes left or right, so the cost is the tree height. Inserting keys in sorted order into a plain BST builds a linked list of height n; self-balancing trees such as AVL and red-black trees prevent this.
The in-order traversal prints the keys already sorted. A hash table cannot do this without collecting and sorting the keys.
| Feature | Hash table | Binary search tree |
|---|---|---|
| Average lookup, insert, delete | O(1) | O(log n) if balanced |
| Worst case | O(n) (many collisions) | O(n) (unbalanced tree) |
| Ordered iteration | Not supported | In-order traversal, sorted |
| Min, max, range queries | Linear scan | O(log n) plus output size |
| Memory | Bucket array plus entries | One node per key, two pointers |
| Needs | A good hash function and resizing | A comparison function, ideally balancing |
| Resizing cost | Occasional rehash of all keys | None, grows node by node |
strcmp in the tree.malloc result and free the structure when done, as shown in destroy.For exact lookups it is usually faster on average, but the constant factors, collisions and rehashing matter. For small collections a sorted array or a BST can be just as fast.
If every key lands in one bucket, the chain contains all n entries and a lookup walks it. A good hash function and resizing make this very unlikely.
Use a self-balancing variant such as an AVL or red-black tree, or a skip list or B-tree depending on the workload. A plain BST gives no balance guarantee.
Not in the ISO C standard library. POSIX offers hcreate/hsearch and tsearch, but they are limited, so most projects write their own or use a library.
Hash tables trade ordering for average constant-time access; binary search trees keep keys sorted and give logarithmic operations when balanced. In C you manage the memory, the collision strategy and the balancing yourself, so pick the structure by the queries you need: exact lookup favors the hash table, ordered or range queries favor the tree.