📄 avlset.ccp
字号:
if (rthread(l)) r->lt = l; else r->lt = l->rt; l->rt = r; set_rthread(l, 0); set_rthread(t, lthread(l)); if (lthread(l)) t->rt = l; else t->rt = l->lt; l->lt = t; set_lthread(l, 0); if (bf(l) == AVLRIGHTHEAVY) set_bf(t, AVLLEFTHEAVY); else set_bf(t, AVLBALANCED); if (bf(l) == AVLLEFTHEAVY) set_bf(r, AVLRIGHTHEAVY); else set_bf(r, AVLBALANCED); set_bf(l, AVLBALANCED); t = l; return; } } } else { if (rthread(t)) return; _del(t, t->rt); if (!_need_rebalancing) return; switch (bf(t)) { case AVLRIGHTHEAVY: set_bf(t, AVLBALANCED); return; case AVLBALANCED: set_bf(t, AVLLEFTHEAVY); _need_rebalancing = 0; return; case AVLLEFTHEAVY: <T>AVLNode* l = t->lt; switch (bf(l)) { case AVLBALANCED: if (rthread(l)) t->lt = l; else t->lt = l->rt; set_lthread(t, rthread(l)); l->rt = t; set_rthread(l, 0); set_bf(t, AVLLEFTHEAVY); set_bf(l, AVLRIGHTHEAVY); _need_rebalancing = 0; t = l; return; case AVLLEFTHEAVY: if (rthread(l)) t->lt = l; else t->lt = l->rt; set_lthread(t, rthread(l)); l->rt = t; set_rthread(l, 0); set_bf(t, AVLBALANCED); set_bf(l, AVLBALANCED); t = l; return; case AVLRIGHTHEAVY: <T>AVLNode* r = l->rt; set_rthread(l, lthread(r)); if (lthread(r)) l->rt = r; else l->rt = r->lt; r->lt = l; set_lthread(r, 0); set_lthread(t, rthread(r)); if (rthread(r)) t->lt = r; else t->lt = r->rt; r->rt = t; set_rthread(r, 0); if (bf(r) == AVLLEFTHEAVY) set_bf(t, AVLRIGHTHEAVY); else set_bf(t, AVLBALANCED); if (bf(r) == AVLRIGHTHEAVY) set_bf(l, AVLLEFTHEAVY); else set_bf(l, AVLBALANCED); set_bf(r, AVLBALANCED); t = r; return; } } }} void <T>AVLSet::del(<T&> item){ if (root == 0) return; _need_rebalancing = 0; _already_found = 0; _found_node = 0; _target_item = &item; _del(root, root); if (_found_node) { delete(_found_node); if (--count == 0) root = 0; }}// build an ordered array of pointers to tree nodes back into a tree// we know that at least one element existsstatic <T>AVLNode* _do_treeify(int lo, int hi, int& h){ int lh, rh; int mid = (lo + hi) / 2; <T>AVLNode* t = _hold_nodes[mid]; if (lo > mid - 1) { set_lthread(t, 1); if (mid == 0) t->lt = 0; else t->lt = _hold_nodes[mid-1]; lh = 0; } else { set_lthread(t, 0); t->lt = _do_treeify(lo, mid-1, lh); } if (hi < mid + 1) { set_rthread(t, 1); if (mid == _max_hold_index) t->rt = 0; else t->rt = _hold_nodes[mid+1]; rh = 0; } else { set_rthread(t, 0); t->rt = _do_treeify(mid+1, hi, rh); } if (lh == rh) { set_bf(t, AVLBALANCED); h = lh + 1; } else if (lh == rh - 1) { set_bf(t, AVLRIGHTHEAVY); h = rh + 1; } else if (rh == lh - 1) { set_bf(t, AVLLEFTHEAVY); h = lh + 1; } else // can't happen abort(); return t;}static <T>AVLNode* _treeify(int n){ <T>AVLNode* t; if (n == 0) t = 0; else { int b; _max_hold_index = n-1; t = _do_treeify(0, _max_hold_index, b); } delete _hold_nodes; return t;}void <T>AVLSet::_kill(<T>AVLNode* t){ if (t != 0) { if (!lthread(t)) _kill(t->lt); if (!rthread(t)) _kill(t->rt); delete t; }}<T>AVLSet::<T>AVLSet(<T>AVLSet& b){ if ((count = b.count) == 0) { root = 0; } else { _hold_nodes = new <T>AVLNodePtr [count]; <T>AVLNode* t = b.leftmost(); int i = 0; while (t != 0) { _hold_nodes[i++] = new <T>AVLNode(t->item); t = b.succ(t); } root = _treeify(count); }}int <T>AVLSet::operator == (<T>AVLSet& y){ if (count != y.count) return 0; else { <T>AVLNode* t = leftmost(); <T>AVLNode* u = y.leftmost(); for (;;) { if (t == 0) return 1; else if (!(<T>EQ(t->item, u->item))) return 0; else { t = succ(t); u = y.succ(u); } } }}int <T>AVLSet::operator <= (<T>AVLSet& y){ if (count > y.count) return 0; else { <T>AVLNode* t = leftmost(); <T>AVLNode* u = y.leftmost(); for (;;) { if (t == 0) return 1; else if (u == 0) return 0; int cmp = <T>CMP(t->item, u->item); if (cmp == 0) { t = succ(t); u = y.succ(u); } else if (cmp < 0) return 0; else u = y.succ(u); } }}void <T>AVLSet::operator |=(<T>AVLSet& y){ <T>AVLNode* t = leftmost(); <T>AVLNode* u = y.leftmost(); int rsize = count + y.count; _hold_nodes = new <T>AVLNodePtr [rsize]; int k = 0; for (;;) { if (t == 0) { while (u != 0) { _hold_nodes[k++] = new <T>AVLNode(u->item); u = y.succ(u); } break; } else if (u == 0) { while (t != 0) { _hold_nodes[k++] = t; t = succ(t); } break; } int cmp = <T>CMP(t->item, u->item); if (cmp == 0) { _hold_nodes[k++] = t; t = succ(t); u = y.succ(u); } else if (cmp < 0) { _hold_nodes[k++] = t; t = succ(t); } else { _hold_nodes[k++] = new <T>AVLNode(u->item); u = y.succ(u); } } root = _treeify(k); count = k;}void <T>AVLSet::operator &= (<T>AVLSet& y){ <T>AVLNode* t = leftmost(); <T>AVLNode* u = y.leftmost(); int rsize = (count < y.count)? count : y.count; _hold_nodes = new <T>AVLNodePtr [rsize]; int k = 0; for (;;) { if (t == 0) break; if (u == 0) { while (t != 0) { <T>AVLNode* tmp = succ(t); delete t; t = tmp; } break; } int cmp = <T>CMP(t->item, u->item); if (cmp == 0) { _hold_nodes[k++] = t; t = succ(t); u = y.succ(u); } else if (cmp < 0) { <T>AVLNode* tmp = succ(t); delete t; t = tmp; } else u = y.succ(u); } root = _treeify(k); count = k;}void <T>AVLSet::operator -=(<T>AVLSet& y){ <T>AVLNode* t = leftmost(); <T>AVLNode* u = y.leftmost(); int rsize = count; _hold_nodes = new <T>AVLNodePtr [rsize]; int k = 0; for (;;) { if (t == 0) break; else if (u == 0) { while (t != 0) { _hold_nodes[k++] = t; t = succ(t); } break; } int cmp = <T>CMP(t->item, u->item); if (cmp == 0) { <T>AVLNode* tmp = succ(t); delete t; t = tmp; u = y.succ(u); } else if (cmp < 0) { _hold_nodes[k++] = t; t = succ(t); } else u = y.succ(u); } root = _treeify(k); count = k;}int <T>AVLSet::owns(Pix i){ if (i == 0) return 0; for (<T>AVLNode* t = leftmost(); t != 0; t = succ(t)) if (Pix(t) == i) return 1; return 0;}int <T>AVLSet::OK(){ int v = 1; if (root == 0) v = count == 0; else { int n = 1; <T>AVLNode* trail = leftmost(); <T>AVLNode* t = succ(trail); while (t != 0) { ++n; v &= <T>CMP(trail->item, t->item) < 0; trail = t; t = succ(t); } v &= n == count; } if (!v) error("invariant failure"); return v;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -