C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
bst.c
Go to the documentation of this file.
1/**
2 * @file bst.c
3 *
4 * Plain binary search tree, no rebalancing. Sufficient for teaching
5 * the recursive structure; production code would reach for a red-black
6 * tree or AVL.
7 */
8
9#include "bst.h"
10#include <stdlib.h>
11
12typedef struct bst_node {
13 int value;
14 struct bst_node *left;
15 struct bst_node *right;
17
18struct bst {
20 size_t size;
21};
22
24 bst_t *t = malloc(sizeof(*t));
25 if (!t) return NULL;
26 t->root = NULL;
27 t->size = 0;
28 return t;
29}
30
31static void destroy_subtree(bst_node_t *n) {
32 if (!n) return;
35 free(n);
36}
37
39 if (!t) return;
41 free(t);
42}
43
44static bst_node_t *make_node(int v) {
45 bst_node_t *n = malloc(sizeof(*n));
46 if (!n) return NULL;
47 n->value = v;
48 n->left = n->right = NULL;
49 return n;
50}
51
52bool bst_insert(bst_t *t, int value) {
53 if (!t) return false;
54 if (!t->root) {
55 t->root = make_node(value);
56 if (!t->root) return false;
57 t->size = 1;
58 return true;
59 }
60 bst_node_t *cur = t->root;
61 while (1) {
62 if (value < cur->value) {
63 if (!cur->left) {
64 cur->left = make_node(value);
65 if (!cur->left) return false;
66 t->size++;
67 return true;
68 }
69 cur = cur->left;
70 } else if (value > cur->value) {
71 if (!cur->right) {
72 cur->right = make_node(value);
73 if (!cur->right) return false;
74 t->size++;
75 return true;
76 }
77 cur = cur->right;
78 } else {
79 return true; /* duplicate — silently ignore */
80 }
81 }
82}
83
84bool bst_contains(const bst_t *t, int value) {
85 if (!t) return false;
86 const bst_node_t *cur = t->root;
87 while (cur) {
88 if (value < cur->value) cur = cur->left;
89 else if (value > cur->value) cur = cur->right;
90 else return true;
91 }
92 return false;
93}
94
95bool bst_min(const bst_t *t, int *out_value) {
96 if (!t || !t->root) return false;
97 const bst_node_t *cur = t->root;
98 while (cur->left) cur = cur->left;
99 if (out_value) *out_value = cur->value;
100 return true;
101}
102
103bool bst_max(const bst_t *t, int *out_value) {
104 if (!t || !t->root) return false;
105 const bst_node_t *cur = t->root;
106 while (cur->right) cur = cur->right;
107 if (out_value) *out_value = cur->value;
108 return true;
109}
110
111size_t bst_size(const bst_t *t) { return t ? t->size : 0; }
112
113static void in_order_recursive(const bst_node_t *n,
114 void (*visit)(int, void *),
115 void *ctx) {
116 if (!n) return;
117 in_order_recursive(n->left, visit, ctx);
118 visit(n->value, ctx);
119 in_order_recursive(n->right, visit, ctx);
120}
121
122void bst_in_order(const bst_t *t,
123 void (*visit)(int, void *),
124 void *ctx) {
125 if (!t || !visit) return;
126 in_order_recursive(t->root, visit, ctx);
127}
void bst_in_order(const bst_t *t, void(*visit)(int, void *), void *ctx)
Definition bst.c:122
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
static bst_node_t * make_node(int v)
Definition bst.c:44
bool bst_min(const bst_t *t, int *out_value)
Definition bst.c:95
void bst_destroy(bst_t *t)
Definition bst.c:38
static void in_order_recursive(const bst_node_t *n, void(*visit)(int, void *), void *ctx)
Definition bst.c:113
bool bst_contains(const bst_t *t, int value)
Definition bst.c:84
struct bst_node bst_node_t
bst_t * bst_create(void)
Definition bst.c:23
static void destroy_subtree(bst_node_t *n)
Definition bst.c:31
bool bst_insert(bst_t *t, int value)
Definition bst.c:52
Binary search tree of int (no balancing — O(log n) average, O(n) worst-case on sorted input).
Definition bst.c:12
int value
Definition bst.c:13
struct bst_node * right
Definition bst.c:15
struct bst_node * left
Definition bst.c:14
Definition bst.c:18
size_t size
Definition bst.c:20
bst_node_t * root
Definition bst.c:19