private static void LevelOrder( ITraversing node, Visitor visitor, ICollection <ITraversing> visited) { if (node == null || visitor == null) { return; } var queue = new Queue <ITraversing>(new[] { node }); while (queue.Count > 0) { var next = queue.Dequeue(); TraversingHelper.CheckCircularity(visited, next); if (visitor(next)) { return; } next.Traverse(child => { if (child != null) { queue.Enqueue(child); } return(false); }); } }
public static void TraverseSelfSiblingOrAncestor( this ITraversing node, Visitor visitor, bool withSelf, bool withAncestor) { if (withSelf && visitor(node)) { return; } var self = node; var parent = node.Parent; if (parent == null) { return; } var children = parent.Children; if (children.Any(sibling => self != sibling && visitor(sibling))) { return; } if (withAncestor) { TraverseAncestors(parent, visitor, true); } }
internal static void CheckCircularity( ICollection <ITraversing> visited, ITraversing node) { if (visited.Contains(node)) { throw new Exception($"Circularity detected for node {node}"); } visited.Add(node); }
public static void TraverseChildren( this ITraversing node, Visitor visitor, bool withSelf) { if (withSelf && visitor(node)) { return; } node.Children.Any(child => visitor(child)); }
private static bool PostOrder( ITraversing node, Visitor visitor, ICollection <ITraversing> visited) { TraversingHelper.CheckCircularity(visited, node); if (node == null || visitor == null) { return(true); } node.Traverse(child => PostOrder(child, visitor, visited)); return(visitor(node)); }
public static void TraverseDescendantsReverse( this ITraversing node, Visitor visitor, bool withSelf) { if (withSelf) { Traversal.ReverseLevelOrder(node, visitor); } else { var self = node; Traversal.ReverseLevelOrder(node, n => self != n && visitor(n)); } }
public static void TraverseRoot(this ITraversing node, Visitor visitor) { ITraversing parent; var root = node; var visited = new[] { node }.ToList(); while ((parent = root.Parent) != null) { CheckCircularity(visited, parent); root = parent; } visitor(root); }
public static void TraverseAncestors( this ITraversing node, Visitor visitor, bool withSelf) { var parent = node; var visited = new[] { node }.ToList(); if (withSelf && visitor(node)) { return; } while ((parent = parent.Parent) != null && !visitor(parent)) { CheckCircularity(visited, parent); } }
private static void ReverseLevelOrder( ITraversing node, Visitor visitor, ICollection <ITraversing> visited) { if (node == null || visitor == null) { return; } var queue = new Queue <ITraversing>(new[] { node }); var stack = new Stack <ITraversing>(); while (queue.Count > 0) { var next = queue.Dequeue(); TraversingHelper.CheckCircularity(visited, next); stack.Push(next); var level = new List <ITraversing>(); next.Traverse(child => { if (child != null) { level.Insert(0, child); } return(false); }); foreach (var l in level) { queue.Enqueue(l); } } while (stack.Count > 0) { if (visitor(stack.Pop())) { return; } } }
private bool Visit(ITraversing node) { _visited.Add((TreeNode)node); return(false); }
public static void Traverse(ITraversing node, TraversingAxis axis, Visitor visitor) { if (visitor == null) { return; } switch (axis) { case TraversingAxis.Self: node.TraverseSelf(visitor); break; case TraversingAxis.Root: node.TraverseRoot(visitor); break; case TraversingAxis.Child: node.TraverseChildren(visitor, false); break; case TraversingAxis.Sibling: node.TraverseSelfSiblingOrAncestor(visitor, false, false); break; case TraversingAxis.SelfOrChild: node.TraverseChildren(visitor, true); break; case TraversingAxis.SelfOrSibling: node.TraverseSelfSiblingOrAncestor(visitor, true, false); break; case TraversingAxis.Ancestor: node.TraverseAncestors(visitor, false); break; case TraversingAxis.SelfOrAncestor: node.TraverseAncestors(visitor, true); break; case TraversingAxis.Descendant: node.TraverseDescendants(visitor, false); break; case TraversingAxis.DescendantReverse: node.TraverseDescendantsReverse(visitor, false); break; case TraversingAxis.SelfOrDescendant: node.TraverseDescendants(visitor, true); break; case TraversingAxis.SelfOrDescendantReverse: node.TraverseDescendantsReverse(visitor, true); break; case TraversingAxis.SelfSiblingOrAncestor: node.TraverseSelfSiblingOrAncestor(visitor, true, true); break; } }
public static void Traverse(this ITraversing node, Visitor visitor) { node.Traverse(TraversingAxis.Child, visitor); }
public static void TraverseSelf(this ITraversing node, Visitor visitor) { visitor(node); }
public static void ReverseLevelOrder(ITraversing node, Visitor visitor) { ReverseLevelOrder(node, visitor, new List <ITraversing>()); }
public static void PreOrder(ITraversing node, Visitor visitor) { PreOrder(node, visitor, new List <ITraversing>()); }