Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool IsRoot => Parent == null;
- public int Count => Children.Count;
- public void AddChild(Node node)
- {
- node = node ?? throw new ArgumentNullException(nameof(node));
- if (IsAncestorOrSelf(node)) // <- you should implement such method
- throw new ArgumentException("The node can not be an ancestor or self");
- if (IsDescendant(node)) // <- you should implement such method
- throw new ArgumentException("The node can not be a descendant");
- node.Parent = this;
- Children.Add(node);
- }
- public IEnumerable<Node> GetChildren()
- {
- return Children.ToArray();
- }
- public IEnumerable<Node> GetDescendants()
- {
- foreach (var child in Children)
- {
- yield return child;
- foreach (var descendant in child.GetDescendants())
- {
- yield return descendant;
- }
- }
- }
- public Node DeepCopy()
- {
- var other = (Node)MemberwiseClone();
- other.Children = new List<Node>(collection: Children);
- other.Parent = Parent?.DeepCopy();
- other.Value = new Node(value: Value);
- return other;
- }
- public void RemoveChild(Node node)
- {
- if (node != this && Children.Contains(node))
- {
- Children.Remove(node);
- }
- }
- public class Node<T>
- {
- public T Value { get; }
- ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement