示例#1
0
        public static void Main(String[] args)
        {
            RedBlackTree      redBlackTree = new RedBlackTree();
            Random            random       = new Random();
            PersonReader      pr           = new PersonReader("people.dat");
            Person            p            = null;
            Person            test         = null;
            BirthdayGenerator bg           = new BirthdayGenerator(1832, 1923);

            for (int i = 0; i < 100; i++)
            {
                p = pr.NextPerson();
                redBlackTree.Insert(p.Age);
                random.Next();
                if (i == 10)
                {
                    test = p;
                }
            }
            // redBlackTree.Insert(993);
            // System.TimeSpan pt = (System.TimeSpan)redBlackTree.Search(p.LifeSpan);
            int pt = (int)redBlackTree.Search(p.Age);

            redBlackTree.Display();
            Console.WriteLine("The number " + pt + " has been found.");

            Console.Read();
        }
示例#2
0
        public static void Main(String[] args)
        {
            RedBlackTree redBlackTree = new RedBlackTree();

            BinaryTree.BinarySearchTree binarySearchTree = new BinaryTree.BinarySearchTree();


            for (int i = 0; i < 90; i++)
            {
                redBlackTree.Insert(i);
            }


            for (int p = 0; p < 90; p++)
            {
                binarySearchTree.Insert(p, p.ToString());
            }

            DateTime startTime = DateTime.Now;

            redBlackTree.Search(99449);
            DateTime endTime     = DateTime.Now;
            TimeSpan timeElasped = (TimeSpan)(endTime - startTime);

            Console.WriteLine("Red Black Tree Search Time: " + timeElasped.Milliseconds.ToString() + " milliseconds.");
            redBlackTree.Display();


            startTime = DateTime.Now;
            binarySearchTree.Search(binarySearchTree.Root, "99449");
            endTime     = DateTime.Now;
            timeElasped = (TimeSpan)(endTime - startTime);
            Console.WriteLine("Binary Tree Search Time: " + timeElasped.Milliseconds.ToString() + " milliseconds.");
            Console.Read();
        }
        private static void RedBlackTreeDemo()
        {
            RedBlackTree tree   = new RedBlackTree();
            int          choice = 1;

            do
            {
                Console.Clear();
                Console.WriteLine("1.Display the Tree\n2.Insert a node\n3.Delete a node\n4.Search a node\n5.Find Number of elements\n6.Find Height of the tree\n7.InOrderTraverse" +
                                  "\n8.PreOrderTraverse\n9.PostOrderTraverse\n10.LevelOrderTraverse\n11.Exit\nPlease enter a choice");
                choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    tree.display(tree.Root, 1);
                    Console.ReadKey();
                    break;

                case 2:
                    Console.WriteLine("Enter the element to add\n");
                    int  value  = Convert.ToInt32(Console.ReadLine());
                    bool result = tree.Insert(ref tree.Root, value);
                    if (result)
                    {
                        Console.WriteLine("Added successfully");
                    }
                    else
                    {
                        Console.WriteLine("Could not add the element");
                    }
                    Console.ReadKey();
                    break;

                case 3:
                    Console.WriteLine("Enter the element to delete\n");
                    int  valueToDelete = Convert.ToInt32(Console.ReadLine());
                    bool delResult     = tree.Delete(ref tree.Root, valueToDelete);
                    if (delResult)
                    {
                        Console.WriteLine("Deleted successfully");
                    }
                    else
                    {
                        Console.WriteLine("Could not delete the element");
                    }
                    Console.ReadKey();
                    break;

                case 4:
                    Console.WriteLine("Enter the element to search\n");
                    int  valueToSearch = Convert.ToInt32(Console.ReadLine());
                    bool searchResult  = tree.Search(ref tree.Root, valueToSearch);
                    if (searchResult)
                    {
                        Console.WriteLine("The element exists");
                    }
                    else
                    {
                        Console.WriteLine("The element doesn't exists");
                    }
                    Console.ReadKey();
                    break;

                case 5:
                    int numberOfElements = tree.FindNumberOfElements(ref tree.Root);
                    Console.WriteLine("The total number of elements in the tree is " + numberOfElements);
                    Console.ReadKey();
                    break;

                case 6:
                    int heightOfTree = tree.FindHeightOfTree(ref tree.Root);
                    Console.WriteLine("The total height of the tree is " + heightOfTree);
                    Console.ReadKey();
                    break;

                case 7:
                    tree.InOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 8:
                    tree.PreOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 9:
                    tree.PostOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 10:
                    tree.LevelOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 11:
                    break;

                default:
                    Console.WriteLine("Please enter a valid choice");
                    Console.ReadKey();
                    break;
                }
            } while (choice != 11);
        }