Skip to content

Instantly share code, notes, and snippets.

@fabianbaechli
Created April 22, 2025 14:42
Show Gist options
  • Save fabianbaechli/55a46ad3ea75678dbc3c4f89c169e313 to your computer and use it in GitHub Desktop.
Save fabianbaechli/55a46ad3ea75678dbc3c4f89c169e313 to your computer and use it in GitHub Desktop.
struct node* BSTTreeInsert(struct node* p, struct node* r) {
struct node* y = NULL;
struct node* x = r;
while (x != NULL) {
// y is one step behind y = x;
if (x -> key < p -> key) {
x = x -> rgt;
} else {
x = x -> lft;
}
}
// Three cases
if (y == NULL) r = p;
else if (y->key < p->key) y->rgt = p;
else y->lft = p;
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment