public void AddChild(TreeNodeSmart <T> child, int index) { this.DescendantsCount++; children.Insert(index, child); child.Parent = this; // Высота if (child.Height + 1 > this.Height) { int old = this.Height; this.Height = child.Height + 1; if (this.Parent != null) { Parent.childOrderChanged(old, this.Height); } } if (this.Parent != null) { Parent.descendantAdded(); } }
public virtual void RemoveChildAt(int index) { TreeNodeSmart <T> child = children[index]; this.DescendantsCount--; children.RemoveAt(index); // Высота if (child.Height + 1 == this.Height) { int oldOrder = this.Height; if (children.Count > 0) { this.Height = children.Max(delegate(TreeNodeSmart <T> obj) { return(obj.Height); }) + 1; } else { this.Height = 0; } if (oldOrder != this.Height && Parent != null) { Parent.childOrderChanged(oldOrder, this.Height); } } if (Parent != null) { Parent.descendantRemoved(); } }
// ----------------------------------- // ----------- swapping -------------- // ----------------------------------- /// <summary> /// Swaps the node with its child of index <paramref name="i"/>. /// </summary> /// <param name="i">The number of child index to swap with.</param> public void SwapWithChild(int i) { if (i < 0 || i >= this.children.Count) { throw new ArgumentException("There is no child with such index."); } TreeNodeSmart <T> child = this.children[i]; // Каждому из детей ребенка говорим, что теперь я - ваш родитель. foreach (TreeNodeSmart <T> childChild in child.children) { childChild.Parent = this; } // Каждому из собственных детей говорим, что теперь ребенок - ваш родитель. for (int j = 0; j < children.Count; j++) { if (i != j) { this.children[j].Parent = child; } } // Делаем себя своим ребенком вместо child. this.children[i] = this; // Меняемся детьми List <TreeNodeSmart <T> > childChildren = child.children; child.children = this.children; this.children = childChildren; // Родителями if (this.Parent != null) { this.Parent.children.Remove(this); this.Parent.children.Add(child); } child.Parent = this.Parent; this.Parent = child; // Высотами int tmp = child.Height; child.Height = this.Height; this.Height = tmp; // Количеством потомков tmp = child.DescendantsCount; child.DescendantsCount = this.DescendantsCount; this.DescendantsCount = tmp; return; }
public void AddChild(TreeNodeSmart <T> child) { AddChild(child, children.Count); }