static void Main(string[] args) { Node node6 = new Node(6, null, null); Node node5 = new Node(5, null, null); Node node4 = new Node(4, null, null); Node node3 = new Node(3, null, null); Node node2 = new Node(2, node5, node6); Node node1 = new Node(1, node3, node4); Node root = new Node(0, node1, node2); BinaryTree tree = new BinaryTree(root); Console.WriteLine("BINARY TREE"); Console.WriteLine("Preorder:"); List <Node> preorder = tree.PreOrder(root); foreach (Node item in preorder) { Console.WriteLine(item.Value); } preorder.Clear(); Console.WriteLine("\nInorder:"); List <Node> inorder = tree.InOrder(root); foreach (Node item in inorder) { Console.WriteLine(item.Value); } inorder.Clear(); Console.WriteLine("\nPostorder:"); List <Node> postorder = tree.PostOrder(root); foreach (Node item in postorder) { Console.WriteLine(item.Value); } postorder.Clear(); Console.WriteLine("\n========================"); Node node12 = new Node(350, null, null); Node node11 = new Node(250, null, null); Node node10 = new Node(150, null, null); Node node9 = new Node(50, null, null); Node node8 = new Node(300, node11, node12); Node node7 = new Node(100, node9, node10); Node root2 = new Node(200, node7, node8); BinarySearchTree binaryTree = new BinarySearchTree(root2); Console.WriteLine("BINARY SEARCH TREE"); Console.WriteLine("Searching for 100..."); Console.WriteLine($"Found: {binaryTree.Search(root2, 100).Value}"); Console.WriteLine("\nSearching for 75..."); Console.WriteLine($"Found: {binaryTree.Search(root2, 75).Value}"); Console.WriteLine("\nAdding 75 to the tree..."); binaryTree.Add(root2, 75); Console.WriteLine($"Success! Added {binaryTree.Search(root2, 75).Value}"); }
static void Main(string[] args) { var binaryTree = new BinaryTree(); binaryTree.Root = new Node(1); binaryTree.Root.Left = new Node(2); binaryTree.Root.Right = new Node(3); binaryTree.Root.Left.Left = new Node(4); binaryTree.Root.Left.Right = new Node(5); Console.WriteLine("InOrder Traversal"); binaryTree.DepthFirstTraversalsInOrderTraversal(binaryTree.Root); Console.WriteLine("PreOrder Traversal"); binaryTree.DepthFirstTraversalsPreOrderTraversal(binaryTree.Root); Console.WriteLine("PostOrder Traversal"); binaryTree.DepthFirstTraversalsPostOrderTraversal(binaryTree.Root); Console.WriteLine("LevelOrder Traversal"); binaryTree.BreadFirstTraversalsLevelOrderTraversal(binaryTree.Root); /* * The left subtree of a node contains only nodes with keys lesser than the node’s key. * The right subtree of a node contains only nodes with keys greater than the node’s key. * The left and right subtree each must also be a binary search tree. * There must be no duplicate nodes. */ var binarySearchTree = new BinarySearchTree(); binarySearchTree.Insert(8); binarySearchTree.Insert(3); binarySearchTree.Insert(1); binarySearchTree.Insert(6); binarySearchTree.Insert(4); binarySearchTree.Insert(7); binarySearchTree.Insert(10); binarySearchTree.Insert(14); binarySearchTree.Insert(13); Console.WriteLine("BSTk"); int searchFor = 7; Console.WriteLine($"{searchFor} exists in BST: {binarySearchTree.Exists(searchFor)}"); searchFor = 17; Console.WriteLine($"{searchFor} exists in BST: {binarySearchTree.Exists(searchFor)}"); searchFor = 7; Console.WriteLine($"{searchFor} found in BST: {binarySearchTree.Search(binarySearchTree.Root, searchFor) != null}"); searchFor = 17; Console.WriteLine($"{searchFor} found in BST: {binarySearchTree.Search(binarySearchTree.Root, searchFor) != null}"); Console.WriteLine("InOrder Traversal"); binarySearchTree.DepthFirstTraversalsInOrderTraversal(binarySearchTree.Root); Console.WriteLine("PreOrder Traversal"); binarySearchTree.DepthFirstTraversalsPreOrderTraversal(binarySearchTree.Root); Console.WriteLine("PostOrder Traversal"); binarySearchTree.DepthFirstTraversalsPostOrderTraversal(binarySearchTree.Root); Console.WriteLine("LevelOrder Traversal"); binarySearchTree.BreadFirstTraversalsLevelOrderTraversal(binarySearchTree.Root); }
public static void Main(string[] args) { Node node1 = new Node(1); BinaryTree tree = new BinaryTree(node1); tree.Root.Left = new Node(2); tree.Root.Right = new Node(3); List <Node> preResult = tree.PreOrder(tree.Root); Console.WriteLine("PreOrder Traversal: " + Print(preResult)); tree.result.Clear(); List <Node> inResult = tree.InOrder(tree.Root); Console.WriteLine("InOrder Traversal: " + Print(inResult)); tree.result.Clear(); List <Node> postResult = tree.PostOrder(tree.Root); Console.WriteLine("PostOrder Traversal: " + Print(inResult)); tree.result.Clear(); BinarySearchTree searchTree = new BinarySearchTree(new Node(100)); searchTree.Add(new Node(50), searchTree.Root); Node searchResult = searchTree.Search(50, searchTree.Root); Console.WriteLine(searchResult.Value); }
/// <summary> /// Demonstrate binary search tree abilities /// </summary> static void BSTDemo() { BinarySearchTree bst = new BinarySearchTree(); bst.Add(50); bst.Add(30); bst.Add(70); bst.Add(10); bst.Add(5); bst.Add(7); bst.Add(40); bst.Add(45); bst.Add(80); Console.WriteLine("Binary search tree (as a sorted array):"); Array.ForEach(bst.ToArray(), elm => Console.Write($"{elm}, ")); bst.Search(999); try { bst.Add(40); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine(ex.Message); } Console.WriteLine(); }
/// <summary> /// Build BST instantiates a new BinarySearchTree, and adds nodes accordingly (in order), then console Logs an AddNode, and two Searches. /// </summary> public static void BuildBST() { BinarySearchTree BST = new BinarySearchTree(new Node(100)); BST.AddNode(75, BST.Root); BST.AddNode(90, BST.Root); BST.AddNode(120, BST.Root); // This method returns the node, so we are going to check the value of the return Console.WriteLine(BST.AddNode(150, BST.Root).Value); Console.WriteLine("^ his add should be : 150 +++"); // this method returns null if it doesn't exist, and the node if it does. if (BST.Search(BST.Root, 69) == null) { Console.WriteLine("This Console.WriteLine should appear if searching a non-existing node returns null"); } Console.WriteLine(BST.Search(BST.Root, 90).Value); Console.WriteLine("^ This Search should return 90 ++++"); }
/// <summary> /// Makes a Binary Search Tree and prints it to the console. Searches for the node with a value 60. /// </summary> static void MakeBSTree() { BinarySearchTree bst = new BinarySearchTree(); Node node100 = new Node(100); bst.Add(node100, new Node(50)); bst.Add(node100, new Node(200)); bst.Add(node100, new Node(25)); bst.Add(node100, new Node(75)); bst.Add(node100, new Node(150)); bst.Add(node100, new Node(300)); bst.Add(node100, new Node(60)); bst.Add(node100, new Node(90)); //bst.PreOrder(node100); bst.InOrder(node100); //bst.PostOrder(node100); //bst.BreadthFirst(node100); Console.WriteLine("Find a node and print it out"); Console.WriteLine(bst.Search(node100, 60).Value); }
public static void TreeExample() { Node node7 = new Node(null, null, 70); Node node6 = new Node(null, null, 60); Node node5 = new Node(null, null, 50); Node node4 = new Node(null, null, 40); Node node3 = new Node(node6, node7, 30); Node node2 = new Node(node4, node5, 20); Node node1 = new Node(node2, node3, 10); BinaryTree BT1 = new BinaryTree(node1); BT1.nodes = new List <Node>(); BinaryTree BT2 = new BinaryTree(node1); BT2.nodes = new List <Node>(); BinaryTree BT3 = new BinaryTree(node1); BT3.nodes = new List <Node>(); List <Node> preOrder = BT1.PreOrder(node1); List <Node> inOrder = BT2.InOrder(node1); List <Node> postOrder = BT3.PostOrder(node1); Console.WriteLine("PreOrder search results"); foreach (Node item in preOrder) { Console.WriteLine($"{item.Value}"); } BT1.nodes.Clear(); Console.WriteLine("==========================="); Console.WriteLine("InOrder search results"); foreach (Node item in inOrder) { Console.WriteLine($"{item.Value}"); } BT2.nodes.Clear(); Console.WriteLine("==========================="); Console.WriteLine("PostOrder search results"); foreach (Node item in postOrder) { Console.WriteLine($"{item.Value}"); } BT3.nodes.Clear(); Console.WriteLine("==========================="); Node nodeSeven = new Node(null, null, 70); Node nodeSix = new Node(null, null, 50); Node nodeFive = new Node(null, null, 30); Node nodeFour = new Node(null, null, 10); Node nodeThree = new Node(nodeSix, nodeSeven, 60); Node nodeTwo = new Node(nodeFour, nodeFive, 20); Node nodeOne = new Node(nodeTwo, nodeThree, 40); BinarySearchTree BTS = new BinarySearchTree(nodeOne); BinaryTree BT = new BinaryTree(nodeOne); BT.nodes = new List <Node>(); List <Node> search = BT.InOrder(nodeOne); Console.WriteLine("BinarySearchTree results"); foreach (Node item in search) { Console.WriteLine($"{item.Value}"); } BT.nodes.Clear(); Console.WriteLine("==========================="); Console.WriteLine("Binary Search: searching for 50"); Console.WriteLine($"Search result: {BTS.Search(50, nodeOne).Value}"); Console.WriteLine("==========================="); Console.WriteLine("Adding to Binary Search Tree"); Node nodeEight = new Node(null, null, 25); Console.WriteLine($"Node to add: {nodeEight.Value}"); BTS.Add(nodeOne, nodeEight); List <Node> add = BT.InOrder(nodeOne); foreach (Node item in add) { Console.WriteLine($"{item.Value}"); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); BinarySearchTree test = new BinarySearchTree(); test.Add(5); test.Add(4); test.Add(3); Console.WriteLine($"Test one two three {test.Root.Left.Data}"); Node find = test.Search(4); Console.WriteLine($"hey {find.Left.Data} and {test.Root.Data}"); Node Root = new Node(2); Root.Left = new Node(3); Root.Right = new Node(4); Root.Left.Left = new Node(5); Root.Left.Right = new Node(6); Root.Right.Left = new Node(7); Root.Right.Right = new Node(8); List <int> post = BinaryTree.PostOrder(Root); Console.WriteLine(); foreach (var item in post) { Console.Write($"{item} "); } List <int> inb = BinaryTree.InOrder(Root); foreach (var item in inb) { Console.Write($"{item} "); } Console.WriteLine(); List <int> pre = BinaryTree.PreOrder(Root); foreach (var item in pre) { Console.Write($"{item} "); } Console.WriteLine(); Node okay = null; List <int> po = BinaryTree.PreOrder(okay); foreach (var item in po) { Console.WriteLine(item); } Console.ReadKey(); }