private void Set(Node x, bool w, Node n) { if (w) { x.SetLeft(n); } else { x.SetRight(n); } if (n != null) { n.SetParent(x); } }
public void Delete(object[] row, bool datatoo) { Node x = Search(row); if (x == null) { return; } Node n; if (x.GetLeft() == null) { n = x.GetRight(); } else if (x.GetRight() == null) { n = x.GetLeft(); } else { Node d = x; x = x.GetLeft(); // todo: this can be improved while (x.GetRight() != null) { if (Trace.StopEnabled) { Trace.Stop(); } x = x.GetRight(); } // x will be replaced with n later n = x.GetLeft(); // swap d and x int b = x.GetBalance(); x.SetBalance(d.GetBalance()); d.SetBalance(b); // set x.parent Node xp = x.GetParent(); Node dp = d.GetParent(); if (d == _root) { _root = x; } x.SetParent(dp); if (dp != null) { if (dp.GetRight().Equals(d)) { dp.SetRight(x); } else { dp.SetLeft(x); } } // for in-memory tables we could use: d.rData=x.rData; // but not for cached tables // relink d.parent, x.left, x.right if (xp == d) { d.SetParent(x); if (d.GetLeft().Equals(x)) { x.SetLeft(d); x.SetRight(d.GetRight()); } else { x.SetRight(d); x.SetLeft(d.GetLeft()); } } else { d.SetParent(xp); xp.SetRight(d); x.SetRight(d.GetRight()); x.SetLeft(d.GetLeft()); } x.GetRight().SetParent(x); x.GetLeft().SetParent(x); // set d.left, d.right d.SetLeft(n); if (n != null) { n.SetParent(d); } d.SetRight(null); x = d; } bool way = From(x); Replace(x, n); n = x.GetParent(); x.Delete(); if (datatoo) { x.rData.Delete(); } while (n != null) { if (Trace.StopEnabled) { Trace.Stop(); } x = n; int sign = way ? 1 : -1; switch (x.GetBalance() * sign) { case -1: x.SetBalance(0); break; case 0: x.SetBalance(sign); return; case 1: Node r = Child(x, !way); int b = r.GetBalance(); if (b * sign >= 0) { Replace(x, r); Set(x, !way, Child(r, way)); Set(r, way, x); if (b == 0) { x.SetBalance(sign); r.SetBalance(-sign); return; } x.SetBalance(0); r.SetBalance(0); x = r; } else { Node l = Child(r, way); Replace(x, l); b = l.GetBalance(); Set(r, way, Child(l, !way)); Set(l, !way, r); Set(x, !way, Child(l, way)); Set(l, way, x); x.SetBalance((b == sign) ? -sign : 0); r.SetBalance((b == -sign) ? sign : 0); l.SetBalance(0); x = l; } break; } way = From(x); n = x.GetParent(); } }
private void Set(Node x, bool w, Node n) { if (w) { x.SetLeft(n); } else { x.SetRight(n); } if (n != null) { n.SetParent(x); } }