C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
bst.h
Go to the documentation of this file.
1/**
2 * @file bst.h
3 * @brief Binary search tree of `int` (no balancing — O(log n) average,
4 * O(n) worst-case on sorted input).
5 *
6 * Operations:
7 * | op | average | worst |
8 * |--------------|----------|----------|
9 * | insert | O(log n) | O(n) |
10 * | contains | O(log n) | O(n) |
11 * | min / max | O(log n) | O(n) |
12 * | in_order | O(n) | O(n) |
13 */
14
15#ifndef BST_H
16#define BST_H
17
18#include <stdbool.h>
19#include <stddef.h>
20
21typedef struct bst bst_t;
22
23bst_t *bst_create(void);
24void bst_destroy(bst_t *t);
25
26/** Inserts `value`. Duplicates are ignored. Returns true on success. */
27bool bst_insert(bst_t *t, int value);
28
29bool bst_contains(const bst_t *t, int value);
30
31bool bst_min(const bst_t *t, int *out_value);
32bool bst_max(const bst_t *t, int *out_value);
33
34size_t bst_size(const bst_t *t);
35
36/** Visits every node in ascending order, calling `visit` on each value. */
37void bst_in_order(const bst_t *t, void (*visit)(int value, void *ctx), void *ctx);
38
39#endif /* BST_H */
bool bst_max(const bst_t *t, int *out_value)
Definition bst.c:103
size_t bst_size(const bst_t *t)
Definition bst.c:111
bool bst_min(const bst_t *t, int *out_value)
Definition bst.c:95
void bst_destroy(bst_t *t)
Definition bst.c:38
bool bst_contains(const bst_t *t, int value)
Definition bst.c:84
bst_t * bst_create(void)
Definition bst.c:23
bool bst_insert(bst_t *t, int value)
Definition bst.c:52
void bst_in_order(const bst_t *t, void(*visit)(int value, void *ctx), void *ctx)
Definition bst.c:18