internal override Segment[] BalanceWith(Segment segment) { LeafSegment s1 = this; LeafSegment s2 = (LeafSegment)segment; if (s1.Count <= MinSize) { if (s2.Count <= MaxSize) { return(MakeLeaves(s1.ToArray().Append(s2.ToArray()))); } else { // borrow MinSize from s2 return(new Segment[] { MakeLeaf(s1.ToArray().Append(s2.GetRange(0, MinSize).ToArray())), s2.GetRange(MinSize, s2.Count - MinSize) }); } } else if (s2.Count <= MinSize) { if (s1.Count <= MaxSize) { return(MakeLeaves(s1.ToArray().Append(s2.ToArray()))); } else { // borrow MinSize from s1 return(new Segment[] { s1.GetRange(0, s1.Count - MinSize), MakeLeaf(s1.GetRange(s1.Count - MinSize, MinSize).ToArray().Append(s2.ToArray())) }); } } return(new Segment[] { s1, s2 }); }