Skip to content

Instantly share code, notes, and snippets.

@vasalf
Created Nov 27, 2018
Embed
What would you like to do?
Split with pairs
/*
void split(node *v, int x, node *&l, node *&r) {
if (!v) {
l = r = nullptr;
return;
}
if (x < v->x) {
split(v->R, x, v->R, r);
l = v;
} else {
split(v->L, x, l, v->L);
r = v;
}
}
*/
pair<node*, node*> split(node *v, int x) {
if (!v) {
return make_pair(nullptr, nullptr);
}
if (x < v->x) {
pair<node*, node*> p = split(v->R, x);
v->R = p.first;
return make_pair(v, p.second);
} else {
pair<node*, node*> p = split(v->L, x);
v->L = p.second;
return make_pair(p.first, v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment