/// <summary> /// Return a new dict with the root key-value pair removed /// </summary> AvlNode <T> RemoveRoot() { if (IsEmpty) { return(this); } if (left.IsEmpty) { return(right); } if (right.IsEmpty) { return(left); } //Neither are empty: if (left._count < right._count) { //LTDict has fewer, so promote from GTDict to minimize depth AvlNode <T> min; var newgt = right.RemoveMin(out min); var newroot = new AvlNode <T> (min.Value, left, newgt); return(newroot.FixRootBalance()); } else { AvlNode <T> max; var newlt = left.RemoveMax(out max); var newroot = new AvlNode <T> (max.Value, newlt, right); return(newroot.FixRootBalance()); } }
AvlNode <T> RemoveMin(out AvlNode <T> min) { if (IsEmpty) { min = Empty; return(Empty); } if (left.IsEmpty) { //We are the minimum: min = this; return(right); } //Go down: var newlt = left.RemoveMin(out min); var newroot = new AvlNode <T> (Value, newlt, right); return(newroot.FixRootBalance()); }