Beispiel #1
0
    static public void Main()
    {
        // build a binary search tree
        IBSTree aBSTree = new BSTree();

        aBSTree.Insert('M');
        aBSTree.Insert('D');
        aBSTree.Insert('G');
        aBSTree.Insert('A');
        aBSTree.Insert('W');
        aBSTree.Insert('P');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // delete a leaf A
        aBSTree.Delete('A');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // put A back aBStree
        aBSTree.Insert('A');
        // delete a node W, which has only one child
        aBSTree.Delete('W');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // clear the binary tree
        aBSTree.Clear();

        // pre-order traversal
        aBSTree.PreOrderTraverse();
    }
    private void Start()
    {
        BSTree tree = new BSTree();

        int[] data = { 62, 58, 88, 47, 73, 99, 35, 51, 93, 37 };
        for (int i = 0; i < data.Length; i++)
        {
            tree.Add(data[i]);
        }
        tree.MiddleTraversal();
        //Debug.Log(tree.Find(99));
        //Debug.Log(tree.Find(100));
        tree.Delete(73);
        tree.MiddleTraversal();
        tree.Delete(99);
        tree.MiddleTraversal();
    }
 public void BSTreeDelete()
 {
     Assert.IsTrue(BSTCountry.Contains("UK"));
     Assert.AreEqual(5, BSTCountry.GetNodeCount());
     BSTCountry.Delete("UK");
     Assert.IsFalse(BSTCountry.Contains("UK"));
     Assert.AreEqual(4, BSTCountry.GetNodeCount());
 }
        public void InsertTest()
        {
            var values = new int[] { 8, 2, 1, 9, 2, 5 };
            var bstree = new BSTree <int>();

            bstree.BuildBSTree(values);

            bstree.Delete(5);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            //var bsTree = new BSTree(new[] { 12, 8, 3, 15, 12, 7, 2, 19, 11, 3 });

            //var a = bsTree.NumberOfOccurence(12);
            //var b = bsTree.NumberOfOccurence(0);
            //var c = bsTree.NumberOfOccurence(8);
            //var d = bsTree.NumberOfOccurence(3);

            ////bsTree.Delete(bsTree.Root, 12);
            ////bsTree.Delete(bsTree.Root, 12);
            ////bsTree.Delete(bsTree.Root, 2);
            ////bsTree.Delete(bsTree.Root, 0);
            //bsTree.Delete(bsTree.Root, 19);

            Console.WriteLine();


            GenerateMockData();

            var reader = new Reader();
            var writer = new Writer();

            //var quickSort = new DualPivotQuickSort();

            Console.WriteLine("Zadejte kolik cisel se ma generovat");
            var range = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Rozsah od");
            var from = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Rozsah do");
            var to = Int32.Parse(Console.ReadLine());

            var mockGenerator = new MockNumberGenerator();

            var mockSpecification = new MockDataSpecification()
            {
                ValueFrom = from,
                ValueTo   = to,
                Count     = range
            };

            mockGenerator.WriteRandomNumbersInFile(mockSpecification);

            var data = reader.Read();

            Console.WriteLine("Tvorim strom");

            var bsTree = new BSTree(data);

            Console.WriteLine("Strom vytvoren");

            var runUntil = true;

            while (runUntil)
            {
                Console.WriteLine("Press  a to add value.");
                Console.WriteLine("Press  b  search value");
                Console.WriteLine("Press  c  remove value");
                Console.WriteLine("Press  d  to end");

                var pressedKey = Console.ReadLine();

                if (pressedKey == "a")
                {
                    Console.WriteLine("Zadejte cislo k pridani");
                    var number = Int32.Parse(Console.ReadLine());

                    bsTree.Add(number);
                    Console.WriteLine("Cislo pridano: " + number);
                }
                else if (pressedKey == "b")
                {
                    Console.WriteLine("Zadejte cislo k vypoctu vyskytu");
                    var number = Int32.Parse(Console.ReadLine());

                    var value = bsTree.NumberOfOccurence(number);

                    Console.WriteLine("Pocet vyskytu: " + value);
                }
                else if (pressedKey == "c")
                {
                    Console.WriteLine("Zadejte cislo k odstraneni ze stromu");
                    var number = Int32.Parse(Console.ReadLine());

                    bsTree.Delete(bsTree.Root, number);

                    Console.WriteLine("Odstraneni dokonceno");
                }

                else if (pressedKey == "d")
                {
                    runUntil = false;
                }
            }

            //Console.WriteLine("Konec, diiiky");

            //Console.WriteLine("Data se sortuji");

            //var sortedData = quickSort.Sort(data);

            //Console.WriteLine("Data se zapisuji");

            //writer.WriteDataInFile(data, "quicksortOutput.txt");

            //Console.WriteLine("Operace dokoncena");

            //var binarySerach = new BinarySearchDll.BinarySearch();

            //Console.WriteLine("Zadejte hledane cislo");
            //var searchedNumber = Int32.Parse(Console.ReadLine());

            //Console.WriteLine("Hledam pocetnost cisla: " + searchedNumber);

            //var count = binarySerach.GetNumberCountInNumbers(searchedNumber, sortedData);

            //Console.WriteLine("Operace dokoncena");
            //Console.WriteLine("Cislo: " + searchedNumber + " se vyskytuje: " + count);

            //var runUntil = true;

            //while (runUntil)
            //{

            //    Console.WriteLine("Press  a  to continue.");
            //    Console.WriteLine("Press  b  end");

            //    var pressedKey = Console.ReadLine();

            //    if (pressedKey == "a")
            //    {
            //        Console.WriteLine("Zadejte hledane cislo");
            //        searchedNumber = Int32.Parse(Console.ReadLine());
            //        Console.WriteLine("Hledam pocetnost cisla: " + searchedNumber);
            //        count = binarySerach.GetNumberCountInNumbers(searchedNumber, sortedData);
            //        Console.WriteLine("Operace dokoncena");
            //        Console.WriteLine("Cislo: " + searchedNumber + " se vyskytuje: " + count);
            //    }
            //    else if (pressedKey == "b")
            //    {
            //        runUntil = false;
            //    }
            //}

            //Console.WriteLine("Konec, diiiky");
        }
    static public void Main()
    {
        // build a binary search tree
        Console.WriteLine("Initially, an empty BST is created");
        IBSTree  aBSTree    = new BSTree();
        Customer customer01 = new Customer("Huanyi", "Qian", 17701449052);
        Customer customer02 = new Customer("Dick", "Pussy", 12345678910);
        Customer customer03 = new Customer("Stop", "it", 17701449052);
        Customer customer04 = new Customer("Ass", "F**k", 12345678910);
        Customer customer05 = new Customer("Idiot", "Freeman", 17701449052);
        Customer customer06 = new Customer("Saul", "Goodman", 12345678910);
        Customer customer07 = new Customer("Shabi", "ZhuXinhu", 12345678910);


        aBSTree.Insert(customer01);
        aBSTree.Insert(customer02);
        aBSTree.Insert(customer03);
        aBSTree.Insert(customer04);
        aBSTree.Insert(customer05);
        aBSTree.Insert(customer06);
        aBSTree.Insert(customer07);
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();

        // delete a leaf A

        aBSTree.Delete(customer07);
        Console.WriteLine("Zhuxinhu has been deleted from the BST");
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal

        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();

        // put A back aBStree
        aBSTree.Insert(customer07);
        Console.WriteLine("Zhuxinhu has been added back to the BST");
        // delete a node W, which has only one child

        aBSTree.Delete(customer06);
        Console.WriteLine("Goodman has been deleted from the BST");
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal

        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();

        // clear the binary tree
        aBSTree.Clear();
        Console.WriteLine("The BST has been cleard");
        Console.WriteLine();
        Console.WriteLine();

        // pre-order traversal

        aBSTree.PreOrderTraverse();
        // in-order traversal

        aBSTree.InOrderTraverse();
        // post-order traversal

        aBSTree.PostOrderTraverse();

        Console.WriteLine();
        Console.WriteLine();
        Console.ReadKey();
    }