Question
I want you to implement an In Order, Post Order and Pre Order, and Breadth First traversal that output a string of the entire tree.
I want you to implement an In Order, Post Order and Pre Order, and Breadth First traversal that output a string of the entire tree. Also, implement a simple FindSmallest, recursively, that finds the smallest element in the tree (the very leftmost element in the tree). The unfinished code has been provided below.
using System; using System.Diagnostics;
namespace BinarySearchTreeLab {
class BinarySearch { static void Main(string[] args) { Node root = null; BinarySearchTree bst = new BinarySearchTree(); int SIZE = 10; // tested on up to 200k elements and it works fine int[] testArray = new int[SIZE]; Random rnd = new Random(); Console.WriteLine("Elements to be inserted into the BST"); for (int i=0; i { testArray[i] = rnd.Next(1, 100); Console.WriteLine(testArray[i]); } for (int i = 0; i < SIZE; i++) { root = bst.insert(root, testArray[i] ); } Console.WriteLine("Elements in the Tree, in some order. "); // - Lab: Make the following lines of code work. // Lab: Pre-order, Post Order, In order traversals Console.WriteLine(bst.preOrder(root)); Console.WriteLine(bst.postOrder(root)); Console.WriteLine(bst.inOrder(root));
Console.WriteLine();
Console.ReadKey(); } } }
------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace BinarySearchTreeLab { class Node { public int value; public Node left; public Node right; }
class BinarySearchTree { public Node insert(Node root, int v) { if (root == null) { root = new Node(); root.value = v; }
// insertion logic, if the value (v )is < root, insert to the root.left // otherwise it's >=, so insert to the right else if (v < root.value) { root.left = insert(root.left, v); } else { root.right = insert(root.right, v); }
return root; } // : Take the code from here, and implement 3 different traversals as strings // public string traverse (Node root)
public void traverse(Node root) { if (root == null) { return; } Console.WriteLine( root.value.ToString()); traverse(root.left); traverse(root.right);
} // Implement this // note that in order, pre order and post order are all just rearranging the order // of the traverse method basically public string inOrder(Node root) { return ""; }
public string preOrder(Node root) { return ""; }
public string postOrder(Node root) { return ""; }
public string breadthFirst(Node root) { return ""; }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started