public override BinNode <T> Insert(T e) { BinNode <T> x = Search(e); if (x != null) { return(x); } //空树,第一次插入节点 if (Hot == null) { Root = new BinNode <T>(e); Size++; UpdateHeight(Root); } else { if (Lt(e, Hot.Data)) { x = Hot.InsertAsLc(e); } else { x = Hot.InsertAsRc(e); } Size++; UpdateHeightAbove(x); } //一直到根节点 for (BinNode <T> g = Hot; g != null; g = g.Parent) { if (!g.AvlBalanced()) { bool isLchild = g.IsLChild; bool isRoot = g == Root; var y = RotateAt(TallerChild(TallerChild(g))); if (!isRoot) { if (isLchild) { y.Parent.LChild = y; } else { y.Parent.RChild = y; } } break; } else { UpdateHeight(g); } } return(x); }
public override BinNode <T> Insert(T e) { BinNode <T> x = Search(e); if (x != null) { return(x); } if (Hot == null) { Root = new BinNode <T>(e); Size++; UpdateHeight(Root); Root.Color = RbColor.RbBlack; return(x); } x = Lt(e, Hot.Data) ? Hot.InsertAsLc(e) : Hot.InsertAsRc(e); Size++; UpdateHeightAbove(x); x.Color = RbColor.RbRed; SolveDoubleRed(x); return(x); }