public static void Iterator() { var root = new Node { Value = "ROOT", Left = new Node { Value = "LEFT" }, Right = new Node { Value = "RIGHT" } }; var tree = new BreadthFirstTree(root); var traversal = new StringBuilder(); using (IEnumerator<Node> iterator = tree.GetEnumerator()) { while (iterator.MoveNext()) { string currentValue = iterator.Current.Value; traversal.Append(currentValue); traversal.Append(";"); } } Console.WriteLine(traversal.ToString()); }
// (ROOT) // / \ // (A) (B) // / \ / // (C) (D) (E) private BinaryTreeParser() { m_ValueBuffer = new StringBuilder(); m_Stack1 = new Stack<Node>(); m_Stack2 = new Stack<Node>(); m_Root = new Node(); m_Stack1.Push(m_Root); }
private void AddLeft() { Node top = m_Stack2.Peek(); if (top.Left != null) { m_Stack2.Pop(); } Node current = m_Stack2.Peek(); var left = new Node(); current.Left = left; m_Stack1.Push(left); }
private void AddRight() { Node top = m_Stack2.Peek(); if (top.Right != null) { m_Stack2.Pop(); } Node current = m_Stack2.Peek(); var right = new Node(); current.Right = right; m_Stack1.Push(right); }
public DepthFirstTree(Node root) : base(root) { }
public BreadthFirstTree(Node root) : base(root) { }