/// <summary> /// Helper function to Traverse. Recursive. /// </summary> /// <param name="current">The current node.</param> /// <param name="method">The traversal method.</param> void TraverseLoop(BinaryNode <T> current, TraverseMethods method) { //Sorry, the writer of this function is allergic to curly braces!! if (method == TraverseMethods.preOrder) { Console.Write(current.val + ", "); } if (current.left != null) { TraverseLoop(current.left, method); } if (method == TraverseMethods.inOrder) { Console.Write(current.val + ", "); } if (current.right != null) { TraverseLoop(current.right, method); } if (method == TraverseMethods.postOrder) { Console.Write(current.val + ", "); } }
void TraverseLoop(AVLNode <T> current, TraverseMethods method) { if (method == TraverseMethods.preOrder) { Console.Write(current.val + ", "); } if (current.left != null) { TraverseLoop(current.left, method); } if (method == TraverseMethods.inOrder) { Console.Write(current.val + ", "); } if (current.right != null) { TraverseLoop(current.right, method); } if (method == TraverseMethods.postOrder) { Console.Write(current.val + ", "); } }
/// <summary> /// Traverse the tree. /// </summary> /// <param name="method">The method for the traversal.</param> public void Traverse(TraverseMethods method) { Console.Write("Traversing tree " + method.ToString() + ": "); if (topNode == null) { return; } TraverseLoop(topNode, method); Console.Write("\n"); }