// private insertNode function to hide the root private void insertNode(ref BSTNode currentNode, int newData) { // base case: the currentNode is null if (currentNode == null) { // instantiate a new BSTNode BSTNode newNode = new BSTNode(newData); // set the currentNode to the newNode currentNode = newNode; return; } // the newData is less than the currentNode's data else if (newData < (currentNode.getData())) { // traverse left insertNode(ref currentNode.left, newData); } // the newData is greater than the currentNode's data else if (newData > (currentNode.getData())) { // traverse right insertNode(ref currentNode.right, newData); } // otherwise it's a duplicate else { // do not add the duplicates return; } }
// Morris traversal // algorithm from http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/ public void inOrderTraversalWithoutRecursionOrStack() { Console.WriteLine("Traversal of the tree with NO stack and NO recursion:"); // declare a node to keep track of predecessors // and start at the root BSTNode pre = null; BSTNode current = this.root; while (current != null) { // if the current node doesn't have a left child if (current.left == null) { // print out the data Console.Write(current.getData() + " "); // iterate to the right current = current.right; } else { // find the right child of the right-most node // in current's left subtree pre = current.left; while ((pre.right != null) && (pre.right != current)) { // iterate to the right as much as possible pre = pre.right; } // at this point we should be at the right-most node // in the left subtree of current if (pre.right == null) { // make current the right child of pre // and iterate to the left pre.right = current; current = current.left; } // undo the changes we made to the tree else { pre.right = null; // print out the data Console.Write(current.getData() + " "); // iterate to the right current = current.right; } } } Console.Write("\n"); }
// algorithm is the one Evan showed us in lecture public void inOrderTraversalWithoutRecursion() { Console.WriteLine("Traversal of the tree using a stack but no recursion:"); // instantiate a stack to simulate the call stack var nodes = new Stack <BSTNode>(); // starting at the root BSTNode current = this.root; while (true) { // if the root is null if (current == null) { // and there's nothing on the stack if (nodes.Count == 0) { // we're done break; } // otherwise pop a node off the stack current = nodes.Pop(); // print out the data Console.Write(current.getData() + " "); // iterate to the right current = current.right; continue; } // if the node doesn't have a left if (current.left == null) { // print out the data Console.Write(current.getData() + " "); // iterate to the right current = current.right; } // it has a left else { // push the current node on the stack // and iterate to the left nodes.Push(current); current = current.left; } } Console.Write("\n"); }
// private inOrderTraversal function private void inOrderTraversal(BSTNode currentNode) { if (currentNode != null) { // recursive call to the left inOrderTraversal(currentNode.getLeft()); // print out the data Console.Write(currentNode.getData() + " "); // recursive call to the right inOrderTraversal(currentNode.getRight()); } }