//Time: O(n) & Space: O(1) (modifies tree and reverts back) public static void inorderMorris(BST tree) { Console.WriteLine("Inorder Morris traversal (threaded binary tree): "); Node node = tree.root; while (node != null) { if (node.left == null) { Console.Write(" {0} ", node.data); node = node.right; } else { Node succ = node.left; while (succ.right != null && succ.right != node) succ = succ.right; if (succ.right == null) { succ.right = node; node = node.left; } else if (succ.right == node) { succ.right = null; Console.Write(" {0} ", node.data); node = node.right; } } } Console.WriteLine(); }
//Time: O(n) & Space: O(1) public static void inorderIter(BST tree) { Stack<Node> stk = new Stack<Node>(); Node node = tree.root; bool done = false; Console.WriteLine("Inorder traversal Iterative Using Stack"); while (!done) { if (node != null) { stk.Push(node); node = node.left; } else { if (stk.Count > 0) { node = stk.Pop(); Console.Write(" {0} ", node.data); node = node.right; } else done = true; } } Console.WriteLine(); }
public static void driver() { BST tree = new BST(); tree.buildTree(); BST mirror = new BST(); mirror.buildTree(); mirror.ConvertToMirror(mirror.root); Traversals.levelorder(mirror); Traversals.levelorder(tree); Console.WriteLine("Ismirror = {0}", mirror.IsMirror(mirror.root, tree.root)); //Traversals.driver(tree); }
public static void driver() { int[] inorder = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int[] preorder = { 5, 4, 2, 1, 3, 6, 8, 7, 9, 10 }; Node root = InorderAndPreorderToTree(preorder, inorder, 0, inorder.Length - 1); BST tree = new BST(); tree.root = root; Traversals.levelorder(tree); Traversals.inorderIter(tree); Traversals.preorderIter(tree); }
static void Main(string[] args) { BST tree = new BST(); tree.insert(50); tree.insert(2); tree.insert(7); tree.insert(94); tree.insert(24); tree.insert(24); tree.insert(71); tree.insert(30); tree.insert(49); //Console.WriteLine("Count: " + tree.count()); // Should be 9 //Console.WriteLine("Min: " + tree.min()); // Should be 2 //Console.WriteLine("Max: " + tree.max()); // Should be 94 //Console.WriteLine("Depth: " + tree.depth()); // Should be 7 tree.prettyprint(); // Prints the values in order Console.WriteLine(); tree.delete(50); tree.prettyprint(); // Prints the values in order Console.WriteLine(); tree.printInRange(5,80); Console.ReadLine(); /*tree.delete(49); // test for value not in tree tree.delete(51); // test for value not in tree tree.delete(50); tree.delete(2); tree.delete(7); tree.delete(94); tree.delete(24); tree.delete(24); tree.delete(71); tree.delete(30); tree.delete(49);*/ //Console.WriteLine("Count: " + tree.count()); // Should be 0 //Console.WriteLine("Min: " + tree.min()); // Should be -1 //Console.WriteLine("Max: " + tree.max()); // Should be -1 //Console.WriteLine("Depth: " + tree.depth()); // Should be 0 //tree.prettyprint(); // Prints the values in order }
public static void driver(BST tree) { levelorder(tree); inorderIter(tree); Console.WriteLine("Inorder traversal recursive : "); inorderRecur(tree.root); Console.WriteLine(); inorderMorris(tree); preorderIter(tree); Console.WriteLine("Inorder traversal recursive : "); preorderRecur(tree.root); Console.WriteLine(); preorderMorris(tree); postorderIter(tree); Console.WriteLine("Inorder traversal recursive : "); postorderRecur(tree.root); Console.WriteLine(); }
//Time: O(n) & Space: O(1) public static void preorderMorris(BST tree) { Node node = tree.root; while (node != null) { if (node.left == null) { Console.Write(" {0} ", node.data); node = node.right; } else { Node succ = node.left; while (succ.right != null && succ.right != node) succ = succ.right; if (succ.right == null) { succ.right = node; Console.Write(" {0} ", node.data); node = node.left; } else if (succ.right == node) { succ.right = null; node = node.right; } } } Console.WriteLine(); }
//Time: O(n) & Space: O(n) public static void preorderIter(BST tree) { Node node = tree.root; if (node == null) return; Console.WriteLine("Preorder Iterative traversal: "); Stack<Node> stk = new Stack<Node>(); stk.Push(node); while(stk.Count > 0) { node = stk.Pop(); Console.Write(" {0} ", node.data); if (node.right != null) stk.Push(node.right); if (node.left != null) stk.Push(node.left); } Console.WriteLine(); }
//Time: O(n) & Space: O(2n) (two stacks public static void postorderIter(BST tree) { Node node = tree.root; if (node == null) return; Console.WriteLine("Post order traversal Iter using two stacks: "); Stack<Node> stk = new Stack<Node>(); Stack<Node> res = new Stack<Node>(); stk.Push(node); while (stk.Count > 0) { node = stk.Pop(); res.Push(node); if (node.left != null) stk.Push(node.left); if (node.right != null) stk.Push(node.right); } while (res.Count > 0) { node = res.Pop(); Console.Write(" {0} ", node.data); } Console.WriteLine(); }
//print per line: O(n) public static void levelorder(BST tree) { if (tree.root == null) return; Node node = tree.root; Queue<Node> queue = new Queue<Node>(); queue.Enqueue(node); int nextlevel = 1; int numnodes; Console.WriteLine("Level order traversal (print per line)"); while (queue.Count > 0) { numnodes = nextlevel; nextlevel = 0; for (int i = 0; i < numnodes; i++) { node = queue.Dequeue(); Console.Write(" {0} ", node.data); if (node.left != null) { queue.Enqueue(node.left); nextlevel++; } if (node.right != null) { queue.Enqueue(node.right); nextlevel++; } } Console.WriteLine(); } }