/** * Method declaration * * * @param x * @param n * * @throws Exception */ private void replace(Node x, Node n) { if (x.Equals(root)) { root = n; if (n != null) { n.setParent(null); } } else { set(x.getParent(), from(x), n); } }
/** * Method declaration * * * @param x * * @return * * @throws Exception */ public Node next(Node x) { if ((++iNeedCleanUp & 127) == 0) { x.rData.cleanUpCache(); } Node r = x.getRight(); if (r != null) { x = r; Node l = x.getLeft(); while (l != null) { if (Trace.STOP) { Trace.stop(); } x = l; l = x.getLeft(); } return(x); } Node ch = x; x = x.getParent(); while (x != null && ch.Equals(x.getRight())) { if (Trace.STOP) { Trace.stop(); } ch = x; x = x.getParent(); } return(x); }
/** * Method declaration * * * @param x * @param n * * @throws Exception */ private void replace(Node x, Node n) { if (x.Equals(root)) { root = n; if (n != null) { n.setParent(null); } } else { set(x.getParent(), from(x), n); } }
/** * Method declaration * * * @param i * * @throws Exception */ public void insert(Node i) { object[] data = i.getData(); Node n = root, x = n; bool way = true; int compare = -1; while (true) { if (Trace.STOP) { Trace.stop(); } if (n == null) { if (x == null) { root = i; return; } set(x, way, i); break; } x = n; compare = compareRow(data, x.getData()); Trace.check(compare != 0, Trace.VIOLATION_OF_UNIQUE_INDEX); way = compare < 0; n = child(x, way); } while (true) { if (Trace.STOP) { Trace.stop(); } int sign = way ? 1 : -1; switch (x.getBalance() * sign) { case 1: x.setBalance(0); return; case 0: x.setBalance(-sign); break; case -1: Node l = child(x, way); if (l.getBalance() == -sign) { replace(x, l); set(x, way, child(l, !way)); set(l, !way, x); x.setBalance(0); l.setBalance(0); } else { Node r = child(l, !way); replace(x, r); set(l, !way, child(r, way)); set(r, way, l); set(x, way, child(r, !way)); set(r, !way, x); int rb = r.getBalance(); x.setBalance((rb == -sign) ? sign : 0); l.setBalance((rb == sign) ? -sign : 0); r.setBalance(0); } return; } if (x.Equals(root)) { return; } way = from(x); x = x.getParent(); } }
/** * Method declaration * * * @param x * * @return * * @throws Exception */ private bool from(Node x) { if (x.Equals(root)) { return true; } if (Trace.ASSERT) { Trace.assert(x.getParent() != null); } return x.Equals(x.getParent().getLeft()); }