Пример #1
0
        static void Main(string[] args)
        {
            //Trees and Graph Algorithms
            BinarySearchTree.Test(10);
            GraphSearch.Test();

            //Test data
            int[] data     = Helper.Generate(10, 1000);
            int[] heapData = new int[] { 12, 11, 13, 5, 6, 7 };

            //Sorting Algorithms
            int[] bubbleSort    = Sort.BubbleSort(data);
            int[] selectionSort = Sort.SelectionSort(data);
            int[] insertionSort = Sort.InsertionSort(data);
            int[] mergeSort     = Sort.MergeSort(data);
            int[] quickSort     = Sort.QuickSort(data, 0, data.Length / 2);
            int[] heapSort      = Sort.HeapSort(heapData);

            //Searching Algorithms
            int value                 = mergeSort[mergeSort.Length / 2];
            int binarySearch          = Search.BinarySearch(mergeSort, value);
            int binarySearchRecursive = Search.BinarySearchRecursive(mergeSort, value, 0, mergeSort.Length - 1);

            int[,] dGraph =
            {
                { 0,  6, 0,  0,  0,  0, 0,  9, 0 },
                { 6,  0, 9,  0,  0,  0, 0, 11, 0 },
                { 0,  9, 0,  5,  0,  6, 0,  0, 2 },
                { 0,  0, 5,  0,  9, 16, 0,  0, 0 },
                { 0,  0, 0,  9,  0, 10, 0,  0, 0 },
                { 0,  0, 6,  0, 10,  0, 2,  0, 0 },
                { 0,  0, 0, 16,  0,  2, 0,  1, 6 },
                { 9, 11, 0,  0,  0,  0, 1,  0, 5 },
                { 0,  0, 2,  0,  0,  0, 6,  5, 0 }
            };

            GraphTraversal.Dijkstra(dGraph, 0, 9);


            int[, ] pGraph = new int[, ] {
                { 0, 2, 0, 6, 0 },
                { 2, 0, 3, 8, 5 },
                { 0, 3, 0, 0, 7 },
                { 6, 8, 0, 0, 9 },
                { 0, 5, 7, 9, 0 }
            };

            GraphTraversal.PrimMST(pGraph, 5);


            //Word Sorter
            BinaryTree <string> tree = new BinaryTree <string>();
            string input             = string.Empty;

            while (!input.Equals("quit", StringComparison.CurrentCultureIgnoreCase))
            {
                //read the line from the user
                Console.Write("> ");
                input = Console.ReadLine();
                //split the line into words (on space)
                string[] words = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                //add each word to the tree

                foreach (string word in words)
                {
                    tree.Add(word);
                }

                //print the number of words
                Console.WriteLine("{0} words", tree.Count);

                //add print each word using the default (in-order enumerator)
                foreach (string word in tree)
                {
                    Console.Write("{0} ", word);
                }

                Console.WriteLine();

                tree.Clear();
            }
        }
Пример #2
0
        public void TestBinaryTree()
        {
            int MAX = 1000;

            for (int cases = 0; cases < 100; cases++)
            {
                BinaryTree <int> tree = CreateTree(MAX);

                // Test tree traversal, node by node
                BinaryTree <int> current = tree.Find(1);
                for (int i = 1; i <= MAX; i++)
                {
                    Assert.AreEqual(i, current.Value);
                    Assert.AreEqual(i - 1, current.Index());
                    current = current.Next();
                }
                Assert.IsTrue(current.IsRoot());
                Assert.AreEqual(MAX, current.Index());

                // Test tree traversal, random jumps
                for (int i = 0; i < MAX; i++)
                {
                    int start  = _random.Next(1, MAX + 2);
                    int jumpTo = _random.Next(1, MAX + 2);

                    BinaryTree <int> x = start == MAX + 1 ? tree : tree.Find(start);
                    BinaryTree <int> y = x.Step(jumpTo - start);
                    if (jumpTo == MAX + 1)
                    {
                        Assert.IsTrue(y.IsRoot());
                    }
                    else
                    {
                        Assert.AreEqual(jumpTo, y.Value);
                    }
                }

                // Find indexes
                for (int i = 0; i < MAX; i++)
                {
                    BinaryTree <int> bt = tree.FindIndex(i);
                    Assert.AreEqual(i + 1, bt.Value);
                }
                BinaryTree <int> findRoot = tree.FindIndex(MAX);
                Assert.IsTrue(findRoot.IsRoot());

                if (cases % 2 == 0)
                {
                    // remove in order
                    current = tree.Find(1);
                    for (int i = 1; i <= MAX; i++)
                    {
                        Assert.AreEqual(i, current.Value);
                        current = current.Remove();
                        tree.Validate();
                    }
                    Assert.IsTrue(current.IsRoot());
                }
                else
                {
                    // remove in random order
                    for (int i = 0; i < MAX; i++)
                    {
                        int pos = _random.Next(0, MAX - i);
                        current = tree.FindIndex(pos);
                        Assert.AreEqual(pos, current.Index());
                        int oldValue = current.Value;
                        current = current.Remove();
                        Assert.AreEqual(pos, current.Index());
                        if (!current.IsRoot())
                        {
                            int newValue = current.Value;
                            Assert.IsTrue(oldValue < newValue);
                        }
                        tree.Validate();
                    }
                    Assert.IsTrue(current.IsRoot());
                }
            }
        }