public bool SearchTree_CustomPoint_CustomComparer(int first, int second)
        {
            int compare(Point a, Point b)
            {
                if (a.X > b.X)
                {
                    return(1);
                }
                if (a.X < b.X)
                {
                    return(-1);
                }
                return(0);
            }

            var array = new Point[] { new Point(10, 15), new Point(15, 55), new Point(20, 45) };
            var tree  = new SearchTree <Point>(compare);

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Contains(new Point(first, second)));
        }
        public bool SearchTree_ContainsMethodString_CustomComparer(string elem)
        {
            int compare(string a, string b)
            {
                if (a.CompareTo(b) > 0)
                {
                    return(1);
                }
                if (a.CompareTo(b) < 0)
                {
                    return(-1);
                }
                return(0);
            }

            var array = new string[] { "dog", "city", "hello" };
            var tree  = new SearchTree <string>(compare);

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Contains(elem));
        }
        public bool SearchTree_CustomBook_CustomComparer(int elem)
        {
            int compare(Book a, Book b)
            {
                if (a.Price > b.Price)
                {
                    return(1);
                }
                if (a.Price < b.Price)
                {
                    return(-1);
                }
                return(0);
            }

            var array = new Book[] { new Book(10), new Book(15), new Book(20) };
            var tree  = new SearchTree <Book>(compare);

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Contains(new Book(elem)));
        }
        private SearchTree <TestPayload> CreateTestTree(params int[] start)
        {
            var tree = new SearchTree <TestPayload>();

            for (int i = 0; i < start.Length; i++)
            {
                tree.Add(new TestPayload(start[i], i + 1));
            }
            return(tree);
        }
        public bool SearchTree_ContainsMethod(int[] array, int elem)
        {
            var tree = new SearchTree <int>();

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Contains(elem));
        }
        public int SearchTree_ContProperty(int[] array)
        {
            var tree = new SearchTree <int>();

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Count);
        }
        public bool SearchTree_CustomBook(int elem)
        {
            var array = new Book[] { new Book(10), new Book(15), new Book(20) };
            var tree  = new SearchTree <Book>();

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Contains(new Book(elem)));
        }
        public bool SearchTree_ContainsMethodString(string elem)
        {
            var array = new string[] { "dog", "city", "hello" };
            var tree  = new SearchTree <string>();

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Contains(elem));
        }
        public int SearchTree_ClearMethod(int[] array)
        {
            var tree = new SearchTree <int>();

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            tree.Clear();

            return(tree.Count);
        }
        public string SearchTree_InorderMethod(int[] array)
        {
            var tree = new SearchTree <int>();

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            string result = default;

            foreach (var item in tree.Inorder())
            {
                result += item + " ";
            }

            return(result);
        }
        public void SearchTree_InorderMethodString()
        {
            var array          = new string[] { "dog", "city", "hello", "world" };
            var expectedResult = "city dog hello world ";
            var tree           = new SearchTree <string>();

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            string result = default;

            foreach (var item in tree.Inorder())
            {
                result += item + " ";
            }

            CollectionAssert.AreEqual(result, expectedResult);
        }
        public bool SearchTree_ContainsMethod_CustomComparer(int[] array, int elem)
        {
            int compare(int a, int b)
            {
                if (a > b)
                {
                    return(1);
                }
                if (a < b)
                {
                    return(-1);
                }
                return(0);
            }

            var tree = new SearchTree <int>(compare);

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i]);
            }

            return(tree.Contains(elem));
        }
Example #13
0
        private static void Task4()
        {
            {
                int menu = 1;
                SearchTree <Person> searchTree = new SearchTree <Person>();

                do
                {
                    menu = TaskSearchTree_Menu();
                    Console.Clear();
                    switch (menu)
                    {
                    case 1:
                        //формирование дерева
                        int size = ReadNumber(-1, 50, "Введите количество элементов дерева: ");
                        searchTree = new SearchTree <Person>(size);
                        searchTree.Show();

                        break;

                    case 2:
                        //добавление в коллекцию
                        Console.WriteLine("Добавление элемента");
                        searchTree.Add(MakeObject());
                        searchTree.Show();
                        break;

                    case 3:
                        //Удаление
                        if (searchTree.Remove(MakeObject()))
                        {
                            Console.WriteLine("Элемент удален");
                        }
                        else
                        {
                            Console.WriteLine("Элемент не найден:");
                        }
                        searchTree.Show();
                        break;


                    case 4:
                        //Поиск
                        Person p            = MakeObject();
                        bool   searchedItem = searchTree.Contains(p);
                        if (!searchedItem)
                        {
                            Console.WriteLine($"Объект {p} не найден");
                        }
                        else
                        {
                            Console.WriteLine($"Объект {p} найден\n");
                        }
                        searchTree.Show();

                        break;

                    case 5:
                        //клонирование
                        int clone = ReadNumber(0, 3, "1. Глубокое клонирование" + "\n2. Поверхностное\n");
                        switch (clone)
                        {
                        //полное
                        case 1:
                            SearchTree <Person> cloneTree = searchTree.Clone();

                            searchTree.Show();
                            Console.WriteLine("_____________Копия создана___________");
                            cloneTree.Show();
                            Console.ReadKey();
                            break;

                        //поверхностниое
                        case 2:
                            SearchTree <Person> treeShallowCopy = searchTree.ShallowCopy();
                            Console.WriteLine("Копия создана");
                            treeShallowCopy.Show();
                            break;
                        }

                        break;

                    case 6:
                        Console.WriteLine($"Элементов в коллекции: {searchTree.Count()}" +
                                          $"\nНажмите любую клавишу для продолжения...");
                        Console.ReadKey();
                        //печать
                        break;

                    case 7:
                        searchTree.Show();
                        //печать
                        break;
                    }
                    Console.Clear();
                } while (menu != 8);
            }
        }