/// <summary> /// Walks through the tree, and calls the handler for each node. /// </summary> /// <param name="handler">The method to be called for each nodes</param> public void Walk(WalkHandler handler) { List <Element> ancestors = new List <Element>(); handler?.Invoke(this, ancestors); Walk(this, ancestors, handler); }
private void Walk(Element parent, List <Element> ancestors, WalkHandler handler) { if (parent.children != null) { List <Element> _ancestors = new List <Element>(ancestors) { parent }; foreach (Element child in parent.children) { handler?.Invoke(child, _ancestors); Walk(child, _ancestors, handler); } } }