public void Add(NAryTree <T> node) { if (node is null) { throw new ArgumentNullException(nameof(node)); } node.Parent = this; ChildNodes.Add(node); Recount(); }
public void SearchNodesDepthFirst(Action start, Action <NAryTree <T> > preGroup, Action <NAryTree <T> > preItem, Action <NAryTree <T> > perItem, Action <NAryTree <T> > postItem, Action <NAryTree <T> > postGroup, Action end) { NAryTree <T> last = null; var visited = new Stack <NAryTree <T> >(); var toVisit = new Stack <NAryTree <T> >(); toVisit.Push(this); start?.Invoke(); while (toVisit.Count > 0) { var here = toVisit.Pop(); while (visited.Count > 0 && visited.Peek() != here.Parent) { last = visited.Pop(); postGroup?.Invoke(last); postItem?.Invoke(last); } preItem?.Invoke(here); perItem?.Invoke(here); if (here.Children.Count == 0) { postItem?.Invoke(here); } else { visited.Push(here); preGroup?.Invoke(here); foreach (var child in here.Children.AsEnumerable().Reverse()) { toVisit.Push(child); } } last = here; } while (visited.Count > 0) { last = visited.Pop(); postGroup?.Invoke(last); postItem?.Invoke(last); } end?.Invoke(); }