private void PostOrder(Node node, PerformFunction function, bool includeRoot) { var stack = new Stack <Node>(); var outputStack = new Stack <Node>(); if (includeRoot) { stack.Push(node); while (stack.Count > 0) { var currentNode = stack.Pop(); outputStack.Push(currentNode); for (int i = 0; i < currentNode.Children.Count; i++) { stack.Push(currentNode.Children[i]); } } } else { for (int i = 0; i < node.Children.Count; i++) { stack.Push(node.Children[i]); } while (stack.Count > 0) { var currentNode = stack.Pop(); outputStack.Push(currentNode); for (int i = 0; i < currentNode.Children.Count; i++) { stack.Push(currentNode.Children[i]); } } } while (outputStack.Count > 0) { _nodelist.Add(outputStack.Pop()); function(node); } }
private void LevelOrder(Node node, PerformFunction function, bool includeRoot) { var queue = new Queue <Node>(); if (includeRoot) { _nodelist.Add(node); } function(node); queue.Enqueue(node); while (queue.Count > 0) { foreach (Node currentNode in queue.Dequeue().Children) { _nodelist.Add(currentNode); function(node); queue.Enqueue(currentNode); } } }
private void PreOrder(Node node, PerformFunction function, bool includeRoot) { var stack = new Stack <Node>(); if (includeRoot) { stack.Push(node); while (stack.Count > 0) { var currentNode = stack.Pop(); _nodelist.Add(currentNode); function(currentNode); for (int i = currentNode.Children.Count - 1; i >= 0; i--) { stack.Push(currentNode.Children[i]); } } } else { for (int i = node.Children.Count - 1; i >= 0; i--) { stack.Push(node.Children[i]); } while (stack.Count > 0) { var currentNode = stack.Pop(); _nodelist.Add(currentNode); function(currentNode); for (int i = currentNode.Children.Count - 1; i >= 0; i--) { stack.Push(currentNode.Children[i]); } } } }
public PreOrderIterator(Node node, PerformFunction function, bool includeRoot = true) : base(node) { PreOrder(_node, function, includeRoot); }