public static TNode GetPrevDepthFirst <TNode>(this IBidirTreeNode <TNode> node, Direction dir, Func <TNode, TNode, bool> filter) where TNode : class, IBidirTreeNode <TNode> { var cur = (TNode)node; do { cur = cur.GetPrevDepthFirst(dir); }while (cur != null && cur != cur.List.GetBegin(dir) && !filter((TNode)node, cur)); return(cur); }
public static IEnumerable <TNode> GetNodesDepthFirst <TNode>(this IBidirTreeNode <TNode> root, Direction dir) where TNode : class, IBidirTreeNode <TNode> { yield return((TNode)root); if (!root.IsLeaf) { foreach (TNode child in root.Children.GetNodes(dir)) { foreach (TNode node in child.GetNodesDepthFirst(dir)) { yield return(node); } } } }
public static void LevelOrderTraverse <TNode>(this IBidirTreeNode <TNode> root, Action <TNode> action, Direction dir) where TNode : class, IBidirTreeNode <TNode> { var queue = new Queue <TNode>(); queue.Enqueue((TNode)root); while (queue.Count > 0) { TNode node = queue.Dequeue(); action(node); if (!node.IsLeaf) { foreach (TNode child in node.Children.GetNodes(dir)) { queue.Enqueue(child); } } } }
private static void DepthFirstTraverseNode <TNode>(IBidirTreeNode <TNode> node, Action <TNode> action, Direction dir, bool preorder) where TNode : class, IBidirTreeNode <TNode> { if (preorder) { action((TNode)node); } if (!node.IsLeaf) { foreach (TNode child in node.Children.GetNodes(dir)) { DepthFirstTraverseNode(child, action, dir, preorder); } } if (!preorder) { action((TNode)node); } }
public static IEnumerable <TNode> GetNodesBreadthFirst <TNode>(this IBidirTreeNode <TNode> root, Direction dir) where TNode : class, IBidirTreeNode <TNode> { var queue = new Queue <TNode>(); queue.Enqueue((TNode)root); while (queue.Count > 0) { TNode node = queue.Dequeue(); yield return(node); if (!node.IsLeaf) { foreach (TNode child in node.Children.GetNodes(dir)) { queue.Enqueue(child); } } } }
public static IEnumerable <TNode> GetNodesDepthFirst <TNode>(this IBidirTreeNode <TNode> root, Direction dir) where TNode : class, IBidirTreeNode <TNode> { var stack = new Stack <TNode>(); stack.Push((TNode)root); while (stack.Count > 0) { TNode node = stack.Pop(); yield return(node); if (!node.IsLeaf) { foreach (TNode child in node.Children.GetNodes(dir)) { stack.Push(child); } } } }
public static TNode GetPrevDepthFirst <TNode>(this IBidirTreeNode <TNode> node, Direction dir) where TNode : class, IBidirTreeNode <TNode> { if (!node.IsLeaf) { return(node.Children.GetLast(dir)); } IBidirTreeNode <TNode> parent = node; do { node = parent; TNode prev = node.GetPrev(dir); if (prev != node.List.GetBegin(dir)) { return(prev); } parent = node.Parent; }while (parent != null); return(node.GetPrev(dir)); }
public static TNode GetNextDepthFirst <TNode>(this IBidirTreeNode <TNode> node, Direction dir) where TNode : class, IBidirTreeNode <TNode> { if (!node.IsLeaf) { return(node.Children.GetFirst(dir)); } IBidirTreeNode <TNode> parent = node; do { node = parent; TNode next = node.GetNext(dir); if (next != node.List.GetEnd(dir)) { return(next); } parent = node.Parent; }while (parent != null); return(node.GetNext(dir)); }
public static TNode GetPrevDepthFirst <TNode>(this IBidirTreeNode <TNode> node, Func <TNode, TNode, bool> filter) where TNode : class, IBidirTreeNode <TNode> { return(node.GetPrevDepthFirst(Direction.LeftToRight, filter)); }
public static TNode GetPrevDepthFirst <TNode>(this IBidirTreeNode <TNode> node) where TNode : class, IBidirTreeNode <TNode> { return(node.GetPrevDepthFirst(Direction.LeftToRight)); }
public static int DescendantCount <TNode>(this IBidirTreeNode <TNode> node) where TNode : class, IBidirTreeNode <TNode> { return(node.Children.Count + node.Children.Sum(child => child.DescendantCount())); }
public static IEnumerable <TNode> GetNodesBreadthFirst <TNode>(this IBidirTreeNode <TNode> root) where TNode : class, IBidirTreeNode <TNode> { return(GetNodesBreadthFirst(root, Direction.LeftToRight)); }
public static void LevelOrderTraverse <TNode>(this IBidirTreeNode <TNode> root, Action <TNode> action) where TNode : class, IBidirTreeNode <TNode> { LevelOrderTraverse(root, action, Direction.LeftToRight); }
public static void PostorderTraverse <TNode>(this IBidirTreeNode <TNode> root, Action <TNode> action, Direction dir) where TNode : class, IBidirTreeNode <TNode> { DepthFirstTraverseNode(root, action, dir, false); }