Trees
BST Search
Search a binary search tree for one present and one absent value.
Algorithm
Basic Implementation
basic.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node { int value; struct Node* left; struct Node* right; } Node;
Node* node_new(int value, Node* left, Node* right) {
Node* node = (Node*)malloc(sizeof(Node));
node->value = value; node->left = left; node->right = right; return node;
}
void append(char* out, const char* text) { strcat(out, text); }
void render(Node* node, char* out) {
char buf[16];
if (node == NULL) { append(out, "_"); return; }
sprintf(buf, "%d", node->value); append(out, buf);
if (node->left != NULL || node->right != NULL) {
append(out, "("); render(node->left, out); append(out, ","); render(node->right, out); append(out, ")");
}
}
Node* sample_tree(void) {
return node_new(4, node_new(2, node_new(1, NULL, NULL), node_new(3, NULL, NULL)),
node_new(6, node_new(5, NULL, NULL), node_new(7, NULL, NULL)));
}
void print_list(int* values, int n) {
printf("[");
for (int i = 0; i < n; i++) { if (i) printf(", "); printf("%d", values[i]); }
printf("]\n");
}
int search(Node* root, int target) { Node* node = root; while (node) { if (target == node->value) return 1; node = target < node->value ? node->left : node->right; } return 0; }
int main(void) { Node* root = sample_tree(); printf("%s\n", search(root, 5) ? "5 found" : "5 not found"); printf("%s\n", search(root, 8) ? "8 found" : "8 not found"); }
Complexity
- Time: O(h) per search
- Space: O(1) iterative
Implementation notes
- C defines
typedef struct Node { int value; struct Node* left; struct Node* right; } Node, with raw child pointers that are eitherNULLor point at heap-allocated nodes. sample_tree()builds4(2(1,3),6(5,7))through nestednode_newcalls;node_newusesmalloc(sizeof(Node)), and this checked executable does not free those nodes.search(Node* root, int target)uses an iterativeNode* nodecursor. It returns1ontarget == node->value, otherwise followsleftorrightuntil the cursor becomesNULL, then returns0.- The search helper reads pointers and scalar values only; it does not mutate
root, child links, or node contents. - The trace for target
5follows4 -> right,6 -> left, then matches5. The trace for target8follows4 -> right,6 -> right,7 -> right, then reachesNULLand reports not found. mainprints the boolean result through ternary string literals:5 foundand8 not foundon separate lines. Visible memory is heap nodes, stack cursor/target locals, and string literals.
search path
A comparison chooses one subtree at each step, so whole branches are skipped.