Exemple #1
0
        static void Main(string[] args)
        {
            AVLTree <int> avletree = new AVLTree <int>(Comparer <int> .Default);

            avletree.Add(1);

            avletree.PrintTree();

            avletree.Add(2);
            avletree.PrintTree();
            avletree.Add(3);
            avletree.PrintTree();
            avletree.Add(4);
            avletree.PrintTree();
            avletree.Add(5);
            avletree.PrintTree();



            avletree.Add(4);

            avletree.Add(3);
            avletree.PrintTree();

            avletree.Delete(3);
            avletree.PrintTree();



            avletree.Add(1);
            avletree.PrintTree();

            avletree.Add(6);
            avletree.PrintTree();


            avletree.Add(5);
            avletree.PrintTree();

            avletree.Add(2);
            avletree.PrintTree();

            bool exist = avletree.Search(5);

            Console.WriteLine("exist = ", exist);

            int min = avletree.SearchMin();

            Console.WriteLine("min = ", min);
        }
Exemple #2
0
        static void RealMain()
        {
            int[] baseArray = InitialArray();
            var   tree      = new AVLTree();
            var   stream    = new MemoryStream();
            var   formatter = new BinaryFormatter();

            Action <int>[] delAVL = new Action <int>[] { tree.Insert, tree.Remove };
            Console.WriteLine("AVL Tree test. Fill, Search, Remove and Serialize");
            using (new OperationTimer("AVLTreeTest. Fill, Search, Remove and Serialize time test."))
            {
                for (int oper = 0; oper < 2; oper++)
                {
                    for (int i = 0; i < MaxCount; i++)
                    {
                        delAVL[oper](baseArray[i]);
                    }
                    if (oper == 0)
                    {
                        formatter.Serialize(stream, tree);
                    }
                    Console.WriteLine("Tree height: " + tree.GetHeight().ToString());
                    Console.Write(oper == 0 ? $"item = {baseArray[500]}, Search item result = {tree.Search(baseArray[500])}" : "removing has completed");
                    Console.WriteLine();
                }
            }
            stream.Position = 0;
            tree            = (AVLTree)formatter.Deserialize(stream);
            stream.Dispose();
            Console.WriteLine("Deserializing the tree...\nDeserialized tree height: " + tree.GetHeight());
            Console.ReadKey();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            AVLTree tree = new AVLTree();
            bool    flag = true;

            while (flag)
            {
                Console.Clear();
                Console.WriteLine("Demo: AVLTree of Integer");
                Console.WriteLine("--------------------------------");
                Console.WriteLine("1 - Insert node");
                Console.WriteLine("2 - Delete node");
                Console.WriteLine("3 - Search node");
                Console.WriteLine("4 - Traverse Tree In-Order");
                Console.WriteLine("5 - Display Tree");
                Console.WriteLine("6 - Exit");
                Console.WriteLine("--------------------------------");
                ConsoleKeyInfo cki = Console.ReadKey(true);
                switch (cki.Key)
                {
                case ConsoleKey.D1:
                    Console.WriteLine("Enter node to insert:");
                    string insertInput = Console.ReadLine();
                    if (Int32.TryParse(insertInput, out int insertNode))
                    {
                        tree.Insert(insertNode);
                        Console.WriteLine("Node " + insertNode + " has been inserted.");
                    }
                    else
                    {
                        Console.WriteLine("Invalid input. Enter integer number");
                    }
                    Console.WriteLine("\nPress any key...");
                    Console.ReadLine();
                    break;

                case ConsoleKey.D2:
                    Console.WriteLine("Enter node to delete:");
                    string deleteInput = Console.ReadLine();
                    if (Int32.TryParse(deleteInput, out int deleteNode))
                    {
                        if (tree.Search(deleteNode) == null)
                        {
                            Console.WriteLine("No such node in the tree");
                        }
                        else
                        {
                            tree.Delete(deleteNode);
                            Console.WriteLine("Node " + deleteNode + " has been deleted.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input. Enter integer number");
                    }
                    Console.WriteLine("\nPress any key...");
                    Console.ReadLine();
                    break;

                case ConsoleKey.D3:
                    tree.PrintTree();
                    Console.WriteLine("Enter node to search:");
                    string getInput = Console.ReadLine();
                    if (Int32.TryParse(getInput, out int getNode))
                    {
                        if (tree.Search(getNode) != null)
                        {
                            Console.WriteLine("Node " + getNode + " is present in the tree");
                        }
                        else
                        {
                            Console.WriteLine("No such node in the tree");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input. Enter integer number");
                    }
                    Console.WriteLine("\nPress any key...");
                    Console.ReadLine();
                    break;

                case ConsoleKey.D4:
                    Console.WriteLine("Current tree In-Order traverse:");
                    tree.TraverseInOrder();
                    Console.WriteLine("\nPress any key...");
                    Console.ReadLine();
                    break;

                case ConsoleKey.D5:
                    Console.WriteLine("Current tree(from left to right):");
                    tree.PrintTree();
                    Console.WriteLine("\nPress any key...");
                    Console.ReadLine();
                    break;

                case ConsoleKey.D6:
                    flag = false;
                    break;

                default:
                    Console.WriteLine("Invalid menu item, enter numbers 1-4");
                    Console.WriteLine("\nPress any key...");
                    Console.ReadLine();
                    break;
                }
            }
        }