Пример #1
0
        static void Main()
        {
            BinarySearchTree<string> testSubject = new BinarySearchTree<string>("Dani");
            Console.WriteLine(testSubject.Root.NodeValue);
            testSubject.AddElement("Ivan");
            testSubject.AddElement("Petar");
            testSubject.AddElement("Asen");
            testSubject.AddElement("Eve");

            TreeNode wantedItem = testSubject.SearchElement("Ivan");
            Console.WriteLine(wantedItem);

            //Deleting item proved to be too hard for me. IEnumerable<t> implementation too.
        }
        public void Inorder_TypeParameterIsPoint_ComparerIsCustom()
        {
            var tree = new BinarySearchTree <Point>(new Point()
            {
                X = 0
            }, new CustomPointComparer());

            for (int i = 1; i < 5; i++)
            {
                tree.AddElement(new Point()
                {
                    X = i
                });
            }

            var expected = new Point[5];

            for (int i = 1; i < 5; i++)
            {
                expected[i] = new Point()
                {
                    X = i
                };
            }

            var areSame = AreSame(tree.Inorder(), expected, (a, b) => a.X == b.X);

            Assert.IsTrue(areSame);
        }
        public void Inorder_TypeParameterIsBook_ComparerIsCustom()
        {
            var tree = new BinarySearchTree <Book>(new Book()
            {
                Pages = 1
            }, new CustomBookComparer());

            for (int i = 2; i < 5; i++)
            {
                tree.AddElement(new Book()
                {
                    Pages = i
                });
            }

            var expected = new Book[4];

            for (int i = 3; i > -1; i--)
            {
                expected[i] = new Book()
                {
                    Pages = i + 1
                };
            }

            var areSame = AreSame(tree.Inorder(), expected, (a, b) => a.Pages == b.Pages);

            Assert.IsTrue(areSame);
        }
Пример #4
0
        static void Main()
        {
            BinarySearchTree<int> tree = new BinarySearchTree<int>();
            tree.AddElement(13);
            tree.AddElement(17);
            tree.AddElement(101);
            tree.AddElement(44);
            tree.AddElement(9);
            tree.AddElement(11);

            BinarySearchTree<int> tree2 = (BinarySearchTree<int>)tree.Clone();
            //tree2.AddElement(45);
            //tree2.AddElement(99);

            Console.WriteLine("Tree: " + tree.ToString());
            Console.WriteLine("Tree2: " + tree2.ToString());
            Console.WriteLine("Tree equals Tree2: " + tree.Equals(tree2));
            Console.WriteLine("Tree == Tree2: " + (tree == tree2));
            Console.WriteLine("Tree != Tree2: " + (tree != tree2));

            Console.Write("Traverse with foreach: ");
            foreach (TreeNode<int> item in tree)
                Console.Write(item.Value + " ");

            Console.WriteLine();

            Console.WriteLine("Tree hash: {0}", tree.GetHashCode());
            Console.WriteLine("Tree2 hash: {0}", tree2.GetHashCode());
        }
        public void Inorder_TypeParameterIsString_ComparerIsDefault(string[] expected, string[] parameters)
        {
            var tree = new BinarySearchTree <string>(parameters[0]);

            for (int i = 1; i < parameters.Length; i++)
            {
                tree.AddElement(parameters[i]);
            }

            var areSame = AreSame(tree.Inorder(), expected, (a, b) => a == b);

            Assert.IsTrue(areSame);
        }
        public void Postorder_TypeParameterIsInt_ComparerIsDefault(int[] expected, int[] parameters)
        {
            var tree = new BinarySearchTree <int>(parameters[0]);

            for (int i = 1; i < parameters.Length; i++)
            {
                tree.AddElement(parameters[i]);
            }

            var areSame = AreSame <int>(tree.Postorder(), expected, (a, b) => a == b);

            Assert.IsTrue(areSame);
        }
        public void Inorder_TypeParameterIsInt_ComparerIsCustom(int[] expected, int[] parameters)
        {
            var comparer = new CustomIntComparer();
            var tree     = new BinarySearchTree <int>(parameters[0], comparer);

            for (int i = 1; i < parameters.Length; i++)
            {
                tree.AddElement(parameters[i]);
            }

            var areSame = AreSame <int>(tree.Inorder(), expected, (a, b) => a == b);

            Assert.IsTrue(areSame);
        }
        static void Main()
        {
            BinarySearchTree<int> tree1 = new BinarySearchTree<int>(10);

            tree1.AddElement(5);
            tree1.AddElement(15);
            tree1.AddElement(5);
            tree1.AddElement(15);
            tree1.AddElement(5);
            tree1.AddElement(15);

            tree1.PrintTree();
        }