static void Main(string[] args) { BinarySearchTree tree = new BinarySearchTree(); Node node; string input; int numInput; while (true) { Console.WriteLine("Enter a number into the tree"); input = Console.ReadLine(); if (input.Contains("delete")) { Console.WriteLine("Delete a number from the tree"); node = new Node(Convert.ToInt32(Console.ReadLine())); tree.Delete(node); continue; } if (input.Contains("search")) { Console.WriteLine("Search for a number in the tree"); numInput = Convert.ToInt32(Console.ReadLine()); tree.Search(numInput, tree.head); continue; } if (input.Contains("isempty")) { Console.WriteLine(tree.IsEmpty()); continue; } node = new Node(Convert.ToInt32(input)); tree.Insert(node, tree.head, null); } }
public static void BinaryTreeSearchTraversal() { //instantiating a new object of the BinarySearchTree class BinarySearchTree binarySearchTree = new BinarySearchTree(); Node nodeA = new Node(100); Node nodeB = new Node(50); Node nodeC = new Node(150); Node nodeD = new Node(25); Node nodeE = new Node(75); Node nodeF = new Node(300); //nodes that we'll be adding and searching for Node nodeG = new Node(200); Node nodeH = new Node(15); //implementing the Add functionality binarySearchTree.Add(nodeA, nodeB); binarySearchTree.Add(nodeA, nodeC); binarySearchTree.Add(nodeA, nodeD); binarySearchTree.Add(nodeA, nodeE); binarySearchTree.Add(nodeA, nodeF); //adding a node of a larger value Console.WriteLine($"Adding a node {nodeG.Value} to our tree"); binarySearchTree.Add(nodeA, nodeG); //adding a node of a lesser value Console.WriteLine($"Adding a node {nodeH.Value} to our tree"); binarySearchTree.Add(nodeA, nodeH); //implementing the search functionality for the Binary Search Tree search Console.WriteLine($"Searching a Binary Search Tree for a value: {nodeB.Value}"); binarySearchTree.Search(nodeB, 50); }
public static void TestBinarySearchTree() { BinarySearchTree searchTree = new BinarySearchTree(); Node nodeA = new Node(8); Node nodeB = new Node(3); Node nodeC = new Node(5); Node nodeD = new Node(7); Node nodeE = new Node(9); Node nodeF = new Node(4); Node nodeG = new Node(10); searchTree.Add(nodeA, nodeB); searchTree.Add(nodeA, nodeC); searchTree.Add(nodeA, nodeD); searchTree.Add(nodeA, nodeE); Console.WriteLine("Adding Less node"); searchTree.Add(nodeA, nodeF); Console.WriteLine("Adding More node"); searchTree.Add(nodeA, nodeG); Console.WriteLine("Finding Node"); searchTree.Search(nodeA, 7); }
public static void BinarySearchTree() { Console.Clear(); Console.WriteLine("This is a Binary Search Tree"); Node n1 = new Node(15); Node n2 = new Node(20); Node n3 = new Node(10); Node n4 = new Node(70); Node n5 = new Node(30); Node n6 = new Node(40); Node n7 = new Node(50); Node n8 = new Node(5); BinarySearchTree binarySearchTree = new BinarySearchTree(n1); binarySearchTree.Add(n1, n2); binarySearchTree.Add(n1, n3); binarySearchTree.Add(n1, n4); binarySearchTree.Add(n1, n5); binarySearchTree.Add(n1, n6); binarySearchTree.Add(n1, n7); binarySearchTree.Add(n1, n8); Console.Clear(); Console.WriteLine("This is BreadthFirst:"); Console.WriteLine("The order should be:"); Console.WriteLine("15 -> 10 -> 20 -> 5 -> 70 -> 30 -> 40 -> 50"); Console.WriteLine("The traversal starts now:"); // 15 -> 10 -> 20 -> 5 -> 70 -> 30 -> 40 -> 50 -> binarySearchTree.BreadthFirst(n1); Console.ReadLine(); Console.Clear(); Console.WriteLine("This is PreOrder:"); Console.WriteLine("The order should be:"); Console.WriteLine("15 -> 10 -> 5 -> 20 -> 70 -> 30 -> 40 -> 50"); Console.WriteLine("The traversal starts now:"); // 15 -> 10 -> 5 -> 20 -> 70 -> 30 -> 40 -> 50 binarySearchTree.PreOrder(n1); Console.ReadLine(); Console.Clear(); Console.WriteLine("This is InOrder"); Console.WriteLine("The order should be:"); Console.WriteLine("5 -> 10 -> 15 -> 20 -> 30 -> 40 -> 50 -> 70"); Console.WriteLine("The traversal starts now:"); // 5 -> 10 -> 15 -> 20 -> 30 -> 40 -> 50 -> 70 binarySearchTree.InOrder(n1); Console.ReadLine(); Console.Clear(); Console.WriteLine("This is PostOrder:"); Console.WriteLine("The order should be:"); Console.WriteLine("5 -> 10 -> 50 -> 40 -> 30 -> 70 -> 20 -> 15"); Console.WriteLine("The traversal starts now:"); // 5 -> 10 -> 50 -> 40 -> 30 -> 70 -> 20 -> 15 binarySearchTree.PostOrder(n1); Console.ReadLine(); Console.Clear(); Console.WriteLine("Here is a search for a value in the tree"); try { Node node = binarySearchTree.Search(n1, 20); Console.WriteLine(node.Value); Console.ReadLine(); Console.WriteLine("Here is a search for a value not in the tree"); Node node2 = binarySearchTree.Search(n1, 25); Console.WriteLine(node2.Value); } catch (NullReferenceException) { Console.WriteLine("A null reference exception was thrown"); Console.ReadLine(); } }
private static void BinarySearchTreeDemo() { BinarySearchTree tree = new BinarySearchTree(); int choice = 1; do { Console.Clear(); Console.WriteLine("1.Display the Tree\n2.Insert a node\n3.Delete a node\n4.Search a node\n5.Find Number of elements\n6.Find Height of the tree\n7.InOrderTraverse" + "\n8.PreOrderTraverse\n9.PostOrderTraverse\n10.LevelOrderTraverse\n11.Exit\nPlease enter a choice"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: tree.display(tree.Root, 1); Console.ReadKey(); break; case 2: Console.WriteLine("Enter the element to add\n"); int value = Convert.ToInt32(Console.ReadLine()); bool result = tree.Insert(ref tree.Root, value); if (result) { Console.WriteLine("Added successfully"); } else { Console.WriteLine("Could not add the element"); } Console.ReadKey(); break; case 3: Console.WriteLine("Enter the element to delete\n"); int valueToDelete = Convert.ToInt32(Console.ReadLine()); bool delResult = tree.Delete(ref tree.Root, valueToDelete); if (delResult) { Console.WriteLine("Deleted successfully"); } else { Console.WriteLine("Could not delete the element"); } Console.ReadKey(); break; case 4: Console.WriteLine("Enter the element to search\n"); int valueToSearch = Convert.ToInt32(Console.ReadLine()); bool searchResult = tree.Search(ref tree.Root, valueToSearch); if (searchResult) { Console.WriteLine("The element exists"); } else { Console.WriteLine("The element doesn't exists"); } Console.ReadKey(); break; case 5: int numberOfElements = tree.FindNumberOfElements(ref tree.Root); Console.WriteLine("The total number of elements in the tree is " + numberOfElements); Console.ReadKey(); break; case 6: int heightOfTree = tree.FindHeightOfTree(ref tree.Root); Console.WriteLine("The total height of the tree is " + heightOfTree); Console.ReadKey(); break; case 7: tree.InOrderTraverse(tree.Root); Console.ReadKey(); break; case 8: tree.PreOrderTraverse(tree.Root); Console.ReadKey(); break; case 9: tree.PostOrderTraverse(tree.Root); Console.ReadKey(); break; case 10: tree.LevelOrderTraverse(tree.Root); Console.ReadKey(); break; case 11: break; default: Console.WriteLine("Please enter a valid choice"); Console.ReadKey(); break; } } while (choice != 11); }
public static void Main(string[] args) { // Declare placeholder variables - used later by various methods string menuSelection = "", newNodeInput = "", nodeSearchValue = ""; // Instantiate BinaryTree and BinarySearchTree objects, add initial values BinaryTree binTree = new BinaryTree(new Node(25)); binTree.Add(binTree.Root, new Node(10)); binTree.Add(binTree.Root, new Node(5)); binTree.Add(binTree.Root, new Node(7)); binTree.Add(binTree.Root, new Node(1)); binTree.Add(binTree.Root, new Node(40)); binTree.Add(binTree.Root, new Node(50)); binTree.Add(binTree.Root, new Node(30)); BinarySearchTree binSearchTree = new BinarySearchTree(new Node(25)); binSearchTree.Add(binSearchTree.Root, new Node(10)); binSearchTree.Add(binSearchTree.Root, new Node(5)); binSearchTree.Add(binSearchTree.Root, new Node(7)); binSearchTree.Add(binSearchTree.Root, new Node(1)); binSearchTree.Add(binSearchTree.Root, new Node(40)); binSearchTree.Add(binSearchTree.Root, new Node(50)); binSearchTree.Add(binSearchTree.Root, new Node(30)); // Loop until the user enters the "8" key to exit the application do { // Prompt user to select an option from the menu PrintMainMenu(); menuSelection = Console.ReadLine(); Console.Clear(); switch (menuSelection) { case "1": // Adds a Node to the Binary Tree Console.WriteLine("What value you like add to the Binary Tree?"); newNodeInput = Console.ReadLine(); Console.Clear(); if (int.TryParse(newNodeInput, out int binTree_Add)) { binTree.Add(binTree.Root, new Node(binTree_Add)); Console.WriteLine("Success!"); } else { Console.WriteLine("Sorry, unable to add that value to the Binary Tree."); } PromptToReturnToMainMenu(); break; case "2": // Adds a Node to the Binary Search Tree Console.WriteLine("What value you like add to the Binary Search Tree?"); newNodeInput = Console.ReadLine(); Console.Clear(); if (int.TryParse(newNodeInput, out int binSearchTree_Add)) { binSearchTree.Add(binSearchTree.Root, new Node(binSearchTree_Add)); Console.WriteLine("Success!"); } else { Console.WriteLine("Sorry, unable to add that value to the Binary Search Tree."); } PromptToReturnToMainMenu(); break; case "3": // Searches for a value in the Binary Tree Console.WriteLine("What value you like search for in the Binary Tree?"); nodeSearchValue = Console.ReadLine(); Console.Clear(); if (int.TryParse(nodeSearchValue, out int binTree_Search)) { Node foundBinTreeNode = binTree.Search(binTree.Root, binTree_Search); if (foundBinTreeNode != null) { Console.WriteLine("Found!"); } else { Console.WriteLine("That value does not exist within the Binary Tree"); } } else { Console.WriteLine("Sorry, please input an integer to search for."); } PromptToReturnToMainMenu(); break; case "4": // Searches for a value in the Binary Search Tree Console.WriteLine("What value you like search for in the Binary Search Tree?"); nodeSearchValue = Console.ReadLine(); Console.Clear(); if (int.TryParse(nodeSearchValue, out int binSearchTree_Search)) { Node foundBinSearchTreeNode = binSearchTree.Search(binSearchTree.Root, binSearchTree_Search); if (foundBinSearchTreeNode != null) { Console.WriteLine("Found!"); } else { Console.WriteLine("That value does not exist within the Binary Search Tree"); } } else { Console.WriteLine("Sorry, please input an integer to search for."); } PromptToReturnToMainMenu(); break; case "5": // Prints the values in the Binary Tree in "Preorder" sequence binTree.PreOrder(binTree.Root); PromptToReturnToMainMenu(); break; case "6": // Prints the values in the Binary Tree in "Postorder" sequence binTree.PostOrder(binTree.Root); PromptToReturnToMainMenu(); break; case "7": // Prints the values in the Binary Tree in "Inorder" sequence binTree.InOrder(binTree.Root); PromptToReturnToMainMenu(); break; case "8": // Prints the values in the Binary Tree in "Breadth First" sequence binTree.BreadthFirst(binTree.Root); PromptToReturnToMainMenu(); break; case "9": // Exits the Program Environment.Exit(0); break; default: // Handles cases where user doesn't enter a valid menu option Console.WriteLine("That did not match one of the menu options. Try again.\n"); break; } } while (menuSelection != "9"); }
static void Main(string[] args) { // create the tree Node root = new Node(1); Node leftChild = new Node(2); Node rightChild = new Node(3); Node LeftChildLeft = new Node(4); Node LeftChildRight = new Node(5); Node RightChildLeft = new Node(6); Node RightChildRight = new Node(7); root.Left = leftChild; root.Right = rightChild; leftChild.Left = LeftChildLeft; leftChild.Right = LeftChildRight; rightChild.Left = RightChildLeft; rightChild.Right = RightChildRight; BinaryTree bt = new BinaryTree(root); // Prints preorder WriteLine("======== Pre-order ========"); bt.PreOrder(); WriteLine("======== In-order ========"); bt.InOrder(); WriteLine("======== Post-order ========"); bt.PostOrder(); WriteLine("Adding bst with values 3, 1, 2 in that order."); // Creates a new bst BinarySearchTree bst = new BinarySearchTree(new Node(3)); bst.Add(new Node(1)); bst.Add(new Node(2)); WriteLine("Searching bst for 5, should not be found"); Node result = bst.Search(5); if (result == null) { WriteLine("5 was not found"); } WriteLine("done"); root = new Node(1); leftChild = new Node(2); rightChild = new Node(3); root.Left = leftChild; root.Right = rightChild; bt = new BinaryTree(root); bool resultEnd = true; List <Node> listBack = bt.PostOrder(); int[] expected = { 2, 3, 1 }; for (int i = 0; i < expected.Length; i++) { WriteLine((int)(listBack[i].Value)); if (expected[i] != (int)listBack[i].Value) { resultEnd = false; } } WriteLine(resultEnd); }