static void Main(string[] args) { Action <int> print = Console.WriteLine; Console.WriteLine("Test 1:"); var comparer = new IntComparer(); var binaryTree = new BinaryTree <int>(comparer); binaryTree.Add(9); binaryTree.Add(6); binaryTree.Add(12); binaryTree.Add(3); binaryTree.Add(7); binaryTree.Add(11); binaryTree.Add(15); binaryTree.Add(1); binaryTree.Add(4); binaryTree.Add(10); binaryTree.Add(14); binaryTree.Add(16); Console.WriteLine("Width traversal:"); binaryTree.WidthTraversal(print); Console.WriteLine("Heght traversal:"); binaryTree.TraversalInDeep(print); Console.WriteLine("Width recurcive traversal:"); binaryTree.TraversalInDeepRecursive(print); Console.WriteLine("Remove:"); binaryTree.Remove(9); binaryTree.TraversalInDeep(print); Console.WriteLine("Test 2:"); var binaryTree1 = new BinaryTree <int>(); binaryTree1.Add(1); binaryTree1.Add(3); binaryTree1.Add(2); binaryTree1.Add(5); binaryTree1.Remove(3); binaryTree1.WidthTraversal(print); }
static void Main(string[] args) { BinaryTree Tree = new BinaryTree(); Tree.Add(10); Tree.Add(9); Tree.Add(8); Tree.Add(7); Tree.Add(11); Tree.Add(6); Tree.Add(12); Tree.Print(Tree); }
/// <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); }
static void Main(string[] args) { var tree = new BinaryTree <int>(); tree.Add(10); tree.Add(6); tree.Add(15); tree.Add(8); tree.Add(20); tree.Add(4); foreach (var node in tree) { Console.WriteLine(node); } }
static void Main(string[] args) { BinaryTree binaryTree = new BinaryTree(); binaryTree.Add(1); binaryTree.Add(2); binaryTree.Add(7); binaryTree.Add(3); binaryTree.Add(10); binaryTree.Add(5); binaryTree.Add(8); Node node = binaryTree.Find(5); int depth = binaryTree.GetTreeDepth(); Console.WriteLine("PreOrder Traversal:"); binaryTree.TraversePreOrder(binaryTree.Root); Console.WriteLine(); Console.WriteLine("InOrder Traversal:"); binaryTree.TraverseInOrder(binaryTree.Root); Console.WriteLine(); Console.WriteLine("PostOrder Traversal:"); binaryTree.TraversePostOrder(binaryTree.Root); Console.WriteLine(); binaryTree.Remove(7); binaryTree.Remove(8); Console.WriteLine("PreOrder Traversal After Removing Operation:"); binaryTree.TraversePreOrder(binaryTree.Root); Console.WriteLine(); Console.ReadLine(); }
/// <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(); }
static void Main(string[] args) { Console.WriteLine("TREE - Binary"); BinaryTree tree = new BinaryTree(); tree.Add(5); tree.Add(7); tree.Add(2); tree.Add(1); tree.Add(6); tree.Add(3); //traversal //Depth first Console.WriteLine("\nPreorder"); tree.Preorder(tree.Root, 0); Console.WriteLine("\nPostorder"); tree.Postorder(tree.Root, 0); Console.WriteLine("\nInorder"); tree.Inorder(tree.Root, 0); //Breadth first Console.WriteLine("\nUsing given level"); DateTime startTime = DateTime.Now; tree.LevelOrder(tree.Root); Console.Write($" in {(DateTime.Now - startTime).TotalSeconds}s\n"); Console.WriteLine("\nUsing queue"); startTime = DateTime.Now; tree.LevelOrderWithQueue(tree.Root); Console.Write($" in {(DateTime.Now - startTime).TotalSeconds}s\n"); //Search Console.WriteLine("\nSearching (Preorder)"); string result = tree.Search(tree.Root, 6) != null ? "Found" : "Not found"; Console.WriteLine($"Search for 6: {result}"); result = tree.Search(tree.Root, 9) != null ? "Found" : "Not found"; Console.WriteLine($"Search for 9: {result}"); }
public static void Test() { BinaryTree <int> tree = new BinaryTree <int>(); // tree.Add(1, 5, 7, 2, 3, 8); tree.RecursiveAdd(4, 2, 6, 1, 3, 5, 7); BinaryTree <string> tree2 = new BinaryTree <string>(); tree2.RecursiveAdd("d", "b", "f", "a", "c", "e", "g"); BinaryTree <int> tree3 = new BinaryTree <int>(); Random rnd = new Random(); int[] varios = new int[1000]; for (int i = 0; i < 1000; i++) { int a = rnd.Next(0, 1000000); varios[i] = a; } // tree3.Add(7, 1, 9, 0, 3, 8, 10, 2, 5, 4, 6); tree3.Add(varios); Console.WriteLine(string.Join(", ", tree.RecursiveInOrder())); Console.WriteLine(string.Join(", ", tree2.RecursiveInOrder())); Console.WriteLine(); System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); s.Start(); string xxa = string.Join(", ", tree3.RecursiveInOrder()); s.Stop(); Console.WriteLine("RecursiveInOrder:".MetricJoin(s.Elapsed.ToString(), 19)); Thread.Sleep(100); s.Restart(); string xa = string.Join(", ", tree3.RecursiveInOrder()); s.Stop(); Console.WriteLine("RecursiveInOrder:".MetricJoin(s.Elapsed.ToString(), 19)); Thread.Sleep(100); s.Restart(); tree3.PrintInOrder2(); string xbaa = string.Join(", ", tree3.z); s.Stop(); Console.WriteLine("Print in order 2:".MetricJoin(s.Elapsed.ToString(), 19)); tree3.z.Clear(); // BinaryTree<int> tre = new BinaryTree<int>(); //tre.RecursiveAdd(5, 1, 4, 9, 2, 3, 8, 6, 7); Thread.Sleep(100); s.Restart(); tree3.PrintInOrder2(); string ax = string.Join(", ", tree3.z); s.Stop(); Console.WriteLine("Print in order 2:".MetricJoin(s.Elapsed.ToString(), 19)); tree3.x.Clear(); GC.Collect(); Thread.Sleep(100); s.Restart(); tree3.PrintInOrder1(); string pt3 = string.Join(", ", tree3.x); s.Stop(); Console.WriteLine("Print in order 1:".MetricJoin(s.Elapsed.ToString(), 19)); Thread.Sleep(100); s.Restart(); //tree3.PreOrder(); string pt4 = string.Join(", ", tree3.PreOrder()); s.Stop(); Console.WriteLine("Pre order:".MetricJoin(s.Elapsed.ToString(), 19)); // Console.WriteLine(pt4); BinaryTree <string> STR = new BinaryTree <string>(); STR.RecursiveAdd("F", "B", "A", "D", "C", "E", "G", "I", "H"); // Console.WriteLine(string.Join(", ", STR.PreOrder())); }
private void Button1_Click(object sender, EventArgs e) { tree.Add(tbInput); tree.Print_DFS(tbPrint); }