Esempio n. 1
0
        static void Main(string[] args)
        {
            Tree <int> tree = new Tree <int>(10);

            tree.Root.LeftChild             = new Node <int>(20);
            tree.Root.RightChild            = new Node <int>(15);
            tree.Root.RightChild.LeftChild  = new Node <int>(11);
            tree.Root.RightChild.RightChild = new Node <int>(16);
            tree.Root.LeftChild.LeftChild   = new Node <int>(30);
            tree.Root.LeftChild.RightChild  = new Node <int>(25);
            Console.WriteLine(String.Join(", ", tree.PreOrder(tree.Root)));
            Console.WriteLine(String.Join(", ", tree.InOrder(tree.Root)));
            Console.WriteLine(String.Join(", ", tree.PostOrder(tree.Root)));
            Console.WriteLine(String.Join(", ", tree.BreadthFirstTraversal(tree.Root)));

            BinaryTree bTree = new BinaryTree();

            bTree.Add(10);
            bTree.Add(15);
            bTree.Add(5);
            bTree.Add(200);
            bTree.Add(250);
            Console.WriteLine(String.Join(", ", bTree.InOrder(bTree.Root)));
            Console.WriteLine(bTree.Contains(15));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Tree tree = new Tree();

            tree.Root.value = "F";
            var b = tree.InsertChild(tree.Root, "B");
            var g = tree.InsertChild(tree.Root, "G");
            var a = tree.InsertChild(b, "A");
            var d = tree.InsertChild(b, "D");
            var c = tree.InsertChild(d, "C");
            var e = tree.InsertChild(d, "E");
            var i = tree.InsertChild(g, "I");
            var h = tree.InsertChild(i, "H");


            Console.Write("Pre-Order:\t");
            tree.PreOrder(tree.Root);
            Console.WriteLine();

            Console.Write("Post-Order:\t");
            tree.PostOrder(tree.Root);
            Console.WriteLine();

            Console.Write("In-Order:\t");
            tree.InOrder(tree.Root);
            Console.WriteLine();

            Console.Write("Level-Order:\t");
            tree.LevelOrder(tree.Root);
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            Tree <int> tree = new Tree <int>();
            string     input;
            int        value;

            while (true)
            {
                Console.WriteLine("I = insert, D = delete, m = minimum, M = maximum, S = search, PO = pre-order, IO = in-order, pO = post-order");

                input = Console.ReadLine();

                if (input == "I")
                {
                    Console.WriteLine("What doth thee wanteth to ins'rt?");
                    value = int.Parse(Console.ReadLine());
                    tree.Insert(value);
                    Console.WriteLine("OK");
                }

                if (input == "D")
                {
                    Console.WriteLine("What doth thee wanteth to fordid?");
                    value = int.Parse(Console.ReadLine());
                    tree.Delete(value);
                    Console.WriteLine("OK");
                }

                if (input == "m")
                {
                    Console.WriteLine("Of what sub-tree?");
                    value = int.Parse(Console.ReadLine());
                    Console.WriteLine(tree.Minimum(value).ToString());
                }

                if (input == "M")
                {
                    Console.WriteLine("Of what sub-tree?");
                    value = int.Parse(Console.ReadLine());
                    Console.WriteLine(tree.Maximum(value).ToString());
                }

                if (input == "S")
                {
                    Console.WriteLine("What doth thee wanteth to searcheth f'r?");
                    value = int.Parse(Console.ReadLine());
                    if (tree.Search(value))
                    {
                        Console.WriteLine("");
                        Console.WriteLine("This value is somewh're in the tree.  If 't be true thee wanteth to knoweth its exact location, prithee payeth me $5.");
                    }
                    else
                    {
                        Console.WriteLine("");
                        Console.WriteLine("Err'r numb'r 666.");
                        Console.WriteLine("Eith'r this value is not in the tree, 'r thee oweth me wage.");
                        Console.WriteLine("Whatev'r the case, i suggesteth yond thee starteth running.");
                    }
                }

                if (input == "PO")
                {
                    Console.WriteLine("");
                    int[] array = new int[tree.PreOrder().Length];
                    for (int i = 0; i < array.Length; i++)
                    {
                        array[i] = tree.PreOrder()[i];
                    }
                    for (int i = 0; i < array.Length; i++)
                    {
                        Console.Write(array[i] + ", ");
                    }
                }

                if (input == "IO")
                {
                    Console.WriteLine("");
                    int[] array = new int[tree.InOrder().Length];
                    for (int i = 0; i < array.Length; i++)
                    {
                        array[i] = tree.InOrder()[i];
                    }
                    for (int i = 0; i < array.Length; i++)
                    {
                        Console.Write(array[i] + ", ");
                    }
                }

                if (input == "pO")
                {
                    Console.WriteLine("");
                    int[] array = new int[tree.PostOrder().Length];
                    for (int i = 0; i < array.Length; i++)
                    {
                        array[i] = tree.PostOrder()[i];
                    }
                    for (int i = 0; i < array.Length; i++)
                    {
                        Console.Write(array[i] + ", ");
                    }
                }

                if (input == "z")
                {
                    tree.Insert(20);
                    tree.Insert(10);
                    tree.Insert(30);
                    tree.Insert(5);
                    tree.Insert(15);
                    tree.Insert(25);
                    tree.Insert(35);
                }



                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("");
                }
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Tree tree = new Tree();
            tree.Root.value = "F";
            var b = tree.InsertChild(tree.Root, "B");
            var g = tree.InsertChild(tree.Root, "G");
            var a = tree.InsertChild(b, "A");
            var d = tree.InsertChild(b, "D");
            var c = tree.InsertChild(d, "C");
            var e = tree.InsertChild(d, "E");
            var i = tree.InsertChild(g, "I");
            var h = tree.InsertChild(i, "H");

            Console.Write("Pre-Order:\t");
            tree.PreOrder(tree.Root);
            Console.WriteLine();

            Console.Write("Post-Order:\t");
            tree.PostOrder(tree.Root);
            Console.WriteLine();

            Console.Write("In-Order:\t");
            tree.InOrder(tree.Root);
            Console.WriteLine();

            Console.Write("Level-Order:\t");
            tree.LevelOrder(tree.Root);
            Console.WriteLine();
        }