public void AddChild(IAmANodeInATree <T> child, int index = -1) { if (index < -1) { throw new ArgumentException("The index can not be lower then -1"); } if (index > _children.Count - 1) { throw new ArgumentException("The index ({0}) can not be higher then index of the last item. Use the AddChild() method without an index to add at the end".FormatInvariant(index)); } if (!child.IsRoot) { throw new ArgumentException("The child with value [{0}] can not be added because it is not a root node.".FormatInvariant(child)); } if (Root == child) { throw new ArgumentException("The child with value [{0}] is the rootnode of the parent.".FormatInvariant(child)); } if (child.SelfAndDescendants.Any(n => this == n)) { throw new ArgumentException("The child with value [{0}] can not be added to itself or its descendants.".FormatInvariant(child)); } child.Parent = this; if (index == -1) { _children.Add(child); } else { _children.Insert(index, child); } }
public void MakeMeAChildOf(IAmANodeInATree <T> parent) { if (!IsRoot) { throw new ArgumentException("This node [{0}] already has a parent".FormatInvariant(parent), "parentNode"); } parent.AddChild(this); }
public void MakeMeARoot() { if (IsRoot) { throw new InvalidOperationException("This node is already a root.".FormatInvariant(this)); } Parent.ChildList.Remove(this); Parent = null; }
public void AddLastSibling(IAmANodeInATree <T> child) { Parent.AddChild(child); }
public void AddFirstChild(IAmANodeInATree <T> child) { AddChild(child, 0); }
private bool Other(IAmANodeInATree <T> node) => !ReferenceEquals(node, this);