// Suppose t != null. void RemoveTarget(AvlSetNode <T> t) { if (t.TypedLeft == null || t.TypedRight == null) { var c = t.TypedLeft ?? t.TypedRight; if (t.TypedParent == null) { Root = c; } else if (t.TypedParent.TypedLeft == t) { t.TypedParent.TypedLeft = c; } else { t.TypedParent.TypedRight = c; } t.TypedParent?.UpdateHeight(true); } else { var t2 = t.SearchNextNode() as AvlSetNode <T>; t.Key = t2.Key; RemoveTarget(t2); } }
AvlSetNode <T> Add(AvlSetNode <T> node, T item) { if (node == null) { ++Count; return(new AvlSetNode <T> { Key = item }); } var d = compare(item, node.Key); if (d == 0) { return(node); } if (d < 0) { node.TypedLeft = Add(node.TypedLeft, item); } else { node.TypedRight = Add(node.TypedRight, item); } var lrh = node.LeftHeight - node.RightHeight; if (lrh > 2 || lrh == 2 && node.TypedLeft.LeftHeight >= node.TypedLeft.RightHeight) { node = node.RotateToRight() as AvlSetNode <T>; node.TypedRight.UpdateHeight(); } else if (lrh < -2 || lrh == -2 && node.TypedRight.LeftHeight <= node.TypedRight.RightHeight) { node = node.RotateToLeft() as AvlSetNode <T>; node.TypedLeft.UpdateHeight(); } node.UpdateHeight(); return(node); }