const Lexem *leftmost_child( const Lexem *t ) { if ( t->type == Lexem::APPLY or t->type == Lexem::SELECT or t->type == Lexem::CHANGE_BEHAVIOR ) return leftmost_child( t->children[ 0] ); // a(15) if ( t->type > 0 ) if ( ( t->num & 1 )/*need_left_arg*/ or ( ( t->num & 4 )/*MayNeedlarg*/ and t->children[0] ) ) return leftmost_child( t->children[ 0 ] ); // a', a-b return t; }
chooser::pitem chooser::delete_element(pitem p) { pitem res = nullptr; if (!p->L && !p->R) { if (p->parent) { res = p->parent; if (is_left_child(p)) { res->L = nullptr; } else { res->R = nullptr; } } delete p; } else if (!p->L || !p->R) { if (p->parent) { res = p->parent; if (is_left_child(p)) { res->L = p->L ? p->L : p->R; } else { res->R = p->L ? p->L : p->R; } p->L = p->R = nullptr; delete p; } else { if (p->L) { right_rotale(p); } else { left_rotale(p); } return delete_element(p); } } else { pitem q = leftmost_child(p->R); if (q != p->R) { std::swap(p->L, q->L); std::swap(p->R, q->R); recalc(p); recalc(q); const pitem p_par = p->parent; make_parent(q->parent, p); make_parent(p_par, q); } else { left_rotale(p); } assert(!p->L || !p->R); delete_element(p); res = q->parent ? q->parent : q; } recalc(res); return splay(res); }