|
C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
|
Binary search tree of int (no balancing — O(log n) average, O(n) worst-case on sorted input).
More...
#include <stdbool.h>#include <stddef.h>Go to the source code of this file.
Typedefs | |
| typedef struct bst | bst_t |
Functions | |
| bool | bst_contains (const bst_t *t, int value) |
| bst_t * | bst_create (void) |
| void | bst_destroy (bst_t *t) |
| void | bst_in_order (const bst_t *t, void(*visit)(int value, void *ctx), void *ctx) |
| bool | bst_insert (bst_t *t, int value) |
| bool | bst_max (const bst_t *t, int *out_value) |
| bool | bst_min (const bst_t *t, int *out_value) |
| size_t | bst_size (const bst_t *t) |
Binary search tree of int (no balancing — O(log n) average, O(n) worst-case on sorted input).
Operations:
| op | average | worst |
|---|---|---|
| insert | O(log n) | O(n) |
| contains | O(log n) | O(n) |
| min / max | O(log n) | O(n) |
| in_order | O(n) | O(n) |
Definition in file bst.h.
| bool bst_contains | ( | const bst_t * | t, |
| int | value | ||
| ) |
Definition at line 84 of file bst.c.
References bst_node::left, bst_node::right, bst::root, and bst_node::value.
| bst_t * bst_create | ( | void | ) |
| void bst_destroy | ( | bst_t * | t | ) |
Definition at line 38 of file bst.c.
References destroy_subtree(), and bst::root.
| void bst_in_order | ( | const bst_t * | t, |
| void(*)(int value, void *ctx) | visit, | ||
| void * | ctx | ||
| ) |
Visits every node in ascending order, calling visit on each value.
| bool bst_insert | ( | bst_t * | t, |
| int | value | ||
| ) |
Inserts value. Duplicates are ignored. Returns true on success.
Definition at line 52 of file bst.c.
References bst_node::left, make_node(), bst_node::right, bst::root, bst::size, and bst_node::value.
| bool bst_max | ( | const bst_t * | t, |
| int * | out_value | ||
| ) |
Definition at line 103 of file bst.c.
References bst_node::right, bst::root, and bst_node::value.
| bool bst_min | ( | const bst_t * | t, |
| int * | out_value | ||
| ) |
Definition at line 95 of file bst.c.
References bst_node::left, bst::root, and bst_node::value.