/// <summary> /// BuildBT istantiates a BinaryTree, and adds nodes to it, then logs all of the traverses /// </summary> public static void BuildBT() { BinaryTree BT = new BinaryTree(new Node(5)); BT.Root.LeftChild = new Node(8); BT.Root.RightChild = new Node(3); BT.Root.LeftChild.LeftChild = new Node(9); BT.Root.RightChild.LeftChild = new Node(10); BT.Root.RightChild.RightChild = new Node(11); // InOrder should be 9-8-5-10-3-11 BT.InOrder(BT.Root); // PreOrder should be 5-8-9-3-10-11 BT.PreOrder(BT.Root); // PostOrder should be 9-8-10-11-3-5 BT.PostOrder(BT.Root); // Breadth should be 5-8-3-9-10-11 BT.BreadthFirst(BT.Root); }
/// <summary> /// Makes a Binary Tree and searches for the node with the value 300. /// </summary> static void MakeBT() { BinaryTree bt = new BinaryTree(); Node node100 = new Node(100); bt.Add(node100, new Node(50)); bt.Add(node100, new Node(200)); bt.Add(node100, new Node(25)); bt.Add(node100, new Node(75)); bt.Add(node100, new Node(150)); bt.Add(node100, new Node(300)); bt.Add(node100, new Node(60)); bt.Add(node100, new Node(90)); //bt.PreOrder(node100); bt.InOrder(node100); //bt.PostOrder(node100); //bt.BreadthFirst(node100); Console.WriteLine("Find a node and print it out"); Console.WriteLine(bt.Search(node100, 300).Value); }
public static void BinaryTreeInOrder() { Node L1L2 = new Node(99, null, null); Node L1R2 = new Node(89, null, null); Node L1 = new Node(79, L1L2, L1R2); Node R1L2 = new Node(90, null, null); Node R1R2 = new Node(100, null, null); Node R1 = new Node(70, R1L2, R1R2); Node top = new Node(60, L1, R1); BinaryTree BinaryTree = new BinaryTree(top); BinaryTree.nodes = new List <Node>(); List <Node> inOrder = BinaryTree.InOrder(top); Console.WriteLine("==============In-Order=============="); foreach (Node item in inOrder) { Console.WriteLine(item.Value); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); Node n5 = new Node(5); BinaryTree tree1 = new BinaryTree(n1); tree1.Root.Left = n2; tree1.Root.Right = n3; tree1.Root.Left.Left = n4; tree1.Root.Left.Right = n5; Print(tree1.PreOrder(tree1.Root)); tree1.searchValues.Clear(); Print(tree1.InOrder(tree1.Root)); tree1.searchValues.Clear(); Print(tree1.PostOrder(tree1.Root)); Node n6 = new Node(1); Node n7 = new Node(2); Node n8 = new Node(3); Node n9 = new Node(4); Node n10 = new Node(5); BinarySearchTree bst1 = new BinarySearchTree(n6); }
/// <summary> /// Demonstrate binary tree manipulations /// </summary> static void BinaryTreesDemo() { BinaryTree <int> bT = new BinaryTree <int>(); bT.Add(0); bT.Add(1); bT.Add(2); bT.Add(3); bT.Add(4); bT.Add(5); bT.Add(6); Console.WriteLine("Preordered tree:"); bT.PreOrder(bT.Root); Array.ForEach(bT.ToArray(), elm => Console.Write($"{elm}, ")); Console.WriteLine(); Console.WriteLine("Inordered tree:"); bT.InOrder(bT.Root); Array.ForEach(bT.ToArray(), elm => Console.Write($"{elm}, ")); Console.WriteLine(); Console.WriteLine("Postordered tree:"); bT.PostOrder(bT.Root); Array.ForEach(bT.ToArray(), elm => Console.Write($"{elm}, ")); Console.WriteLine(); }
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(); }
static void Main(string[] args) { BinarySearchTree tree = new BinarySearchTree(); tree.Add(34, tree.Root); tree.Add(3, tree.Root); tree.Add(32, tree.Root); tree.Add(14, tree.Root); tree.Add(27, tree.Root); tree.Add(67, tree.Root); tree.Add(234, tree.Root); Console.WriteLine("Binary Search Tree Add: 34, 3, 67"); Console.WriteLine(); Console.WriteLine("Root.Value:"); Console.WriteLine(tree.Root.Value); Console.WriteLine("RightChild.Value"); Console.WriteLine(tree.Root.RightChild.Value); Console.WriteLine("LeftChild.Value:"); Console.WriteLine(tree.Root.LeftChild.Value); Console.WriteLine("Contins: 3"); bool truth = tree.Contains(3, tree.Root); Console.WriteLine(truth); Console.WriteLine("Binary Tree: PreOrder"); BinaryTree <int> bT = new BinaryTree <int>(); Node <int> node1 = new Node <int>() { Value = 1 }; Node <int> node2 = new Node <int>() { Value = 2 }; Node <int> node3 = new Node <int>() { Value = 3 }; Node <int> node4 = new Node <int>() { Value = 4 }; Node <int> node5 = new Node <int>() { Value = 5 }; Node <int> node6 = new Node <int>() { Value = 6 }; Node <int> node7 = new Node <int>() { Value = 7 }; bT.Root = node1; bT.Root.LeftChild = node2; bT.Root.RightChild = node3; bT.Root.LeftChild.LeftChild = node4; bT.Root.LeftChild.RightChild = node5; bT.Root.RightChild.LeftChild = node6; bT.Root.RightChild.RightChild = node7; List <int> pre = new List <int>(); List <int> inorder = new List <int>(); List <int> post = new List <int>(); bT.PreOrder(bT.Root, pre); bT.InOrder(bT.Root, inorder); bT.PostOrder(bT.Root, post); pre.ForEach(i => Console.Write("{0} ", i)); Console.WriteLine(); Console.WriteLine("Binary Tree: InOrder"); inorder.ForEach(i => Console.Write("{0} ", i)); Console.WriteLine(); Console.WriteLine("Binary Tree: PostOrder"); post.ForEach(i => Console.Write("{0} ", i)); }
static void Main(string[] args) { Console.WriteLine("Binary Tree!"); BinaryTree myTree = new BinaryTree(1); myTree.root.Left = new Node(2); myTree.root.Right = new Node(3); myTree.root.Left.Left = new Node(4); myTree.root.Left.Right = new Node(5); List <int> PreOrderList = new List <int>(); List <int> InOrderList = new List <int>(); List <int> PostOrderList = new List <int>(); myTree.PreOrder(myTree.root, PreOrderList); /// <summary> /// Pre-order tree traversal /// </summary> foreach (int val in PreOrderList) { Console.Write($"{val} "); } Console.WriteLine("Print PreOrder data traversal values"); /// <summary> /// In-order tree traversal /// </summary> myTree.InOrder(myTree.root, InOrderList); foreach (int val in InOrderList) { Console.Write($"{val} "); } Console.WriteLine("Print InOrder data traversal values"); /// <summary> /// Post-order tree traversal /// </summary> myTree.PostOrder(myTree.root, PostOrderList); foreach (int val in PostOrderList) { Console.Write($"{val} "); } Console.WriteLine("Print PostOrder data traversal values"); Console.WriteLine(); int[] bstValues = { 1, 2, 3, 4, 5 }; /// <summary> /// Print values of binary search tree /// </summary> BinarySearchTree BST = new BinarySearchTree(); foreach (int num in bstValues) { BST.root = BST.Add(BST.root, num); } List <int> bstInOrder = new List <int>(); BST.InOrder(BST.root, bstInOrder); foreach (int num in bstInOrder) { Console.Write($"{num}"); } Console.WriteLine(" Binary Search Tree InOrder data traversal values"); }