public IEnumerable <Node <N, E> > Nodes(NodeLoc loc) { switch (loc) { case NodeLoc.In: for (int i = 0; i < inEdges.Count; i++) { yield return(graphLookup.GetNode(graphLookup.GetEdge(inEdges [i]).Start)); } break; case NodeLoc.Out: for (int i = 0; i < outEdges.Count; i++) { yield return(graphLookup.GetNode(graphLookup.GetEdge(outEdges[i]).End)); } break; case NodeLoc.Any: for (int i = 0; i < inEdges.Count; i++) { yield return(graphLookup.GetNode(graphLookup.GetEdge(inEdges [i]).Start)); } for (int i = 0; i < outEdges.Count; i++) { yield return(graphLookup.GetNode(graphLookup.GetEdge(outEdges[i]).End)); } break; } }
public List <NodeLoc> TopViewNodeTraversal(TreeNode node) { List <NodeLoc> lst = new List <NodeLoc>(); TreeNode cur = node; Queue <NodeLoc> q = new Queue <NodeLoc>(); if (node != null) { q.Enqueue(new NodeLoc(node, 0)); } while (q.Count > 0) { NodeLoc n = q.Dequeue(); if (!lst.Any(x => x.Location == n.Location)) //already existing; { lst.Add(n); } if (n.Node.Left != null) { q.Enqueue(new NodeLoc(n.Node.Left, n.Location - 1)); } if (n.Node.Right != null) { q.Enqueue(new NodeLoc(n.Node.Right, n.Location + 1)); } } lst.Sort((x, y) => { return(x.Location.CompareTo(y.Location)); }); return(lst); }