コード例 #1
0
 public void InsertionRightLeftTest()
 {
     var d = new AVLTree<int, int>();
     d.Add(1, 0);
     d.Add(10, 0);
     d.Add(5, 0);
     CheckTree(d, "<5<1><10>>");
 }
コード例 #2
0
 public void InversionTest()
 {
     // add elements
     var tree = new AVLTree<int>(Comparer<int>.Create((i, j) => j - i));
     tree.Add(1);
     tree.Add(2);
     tree.Add(3);
     Assert.AreEqual(tree.Select(i => i.ToString()).JoinString(","), "3,2,1");
 }
コード例 #3
0
 public void AddDistinctTest()
 {
     // add elements
     var tree = new AVLTree<int>();
     tree.Add(1);
     tree.Add(1);
     tree.Add(1);
     Assert.AreEqual(tree.Select(i => i.ToString()).JoinString(","), "1");
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: D-Eijkelenboom/ALG2
        static void Main(string[] args)
        {
            AVLTree<int> tree = new AVLTree<int>();
            tree.Add(50);
            tree.Add(2);
            tree.Add(7);
            tree.Add(94);
            tree.Add(24);
            tree.Add(24);
            tree.Add(71);
            tree.Add(30);
            tree.Add(49);
            Console.WriteLine("Count: " + tree.count()); // Should be 9
            Console.WriteLine("Min: " + tree.min()); // Should be 2
            Console.WriteLine("Max: " + tree.max()); // Should be 94
            Console.WriteLine("Depth: " + tree.depth()); // Should be 7
            tree.print(); // Prints the values in order

            tree.Remove(49); // test for value not in tree
            tree.Remove(51); // test for value not in tree
            tree.Remove(50);
            tree.Remove(2);
            tree.Remove(7);
            tree.Remove(94);
            tree.Remove(24);
            tree.Remove(24);
            tree.Remove(71);
            tree.Remove(30);
            tree.Remove(49);
            Console.WriteLine("Count: " + tree.count()); // Should be 0
            Console.WriteLine("Min: " + tree.min()); // Should be -1
            Console.WriteLine("Max: " + tree.max()); // Should be -1
            Console.WriteLine("Depth: " + tree.depth()); // Should be 0
            tree.print(); // Prints the values in order
        }
コード例 #5
0
 public void RemoveTest()
 {
     // add elements
     var tree = new AVLTree<int>();
     tree.Add(1);
     tree.Add(2);
     tree.Add(3);
     tree.Remove(3);
     tree.Remove(2);
     Assert.AreEqual(tree.Select(i => i.ToString()).JoinString(","), "1");
 }
コード例 #6
0
 public void InsertionDeepRightLeftTest()
 {
     var d = new AVLTree<int, int>();
     d.Add(10, 0);
     d.Add(5, 0);
     d.Add(11, 0);
     CheckTree(d, "<10<5><11>>");
     d.Add(20, 0);
     CheckTree(d, "<10<5><11<><20>>>");
     d.Add(15, 0);
     CheckTree(d, "<10<5><15<11><20>>>");
 }
コード例 #7
0
ファイル: AVLTreeTests.cs プロジェクト: helios2k6/DSA
      public void BasicAVLTreeTest()
      {
         var tree = new AVLTree<int>();
         Assert.AreEqual(0, tree.Height);

         for (int i = 0; i < 10; i++)
         {
            tree.Add(i);
         }

         Assert.AreEqual(4, tree.Height);
         Assert.AreEqual(10, tree.Size);

         for (int i = 0; i < 10; i++)
         {
            Assert.IsTrue(tree.Contains(i));
         }

         Assert.IsFalse(tree.Contains(11));

         for (int i = 0; i < 10; i++)
         {
            Assert.IsTrue(tree.Remove(i));
         }

         Assert.AreEqual(0, tree.Height);
         Assert.AreEqual(0, tree.Size);
      }
コード例 #8
0
ファイル: AdditionTests.cs プロジェクト: osst/University
        public void PreOrderBalancingTest()
        {
            AVLTree<int> tree = new AVLTree<int>();

            tree.Add(4);
            tree.Add(5);
            tree.Add(2);
            tree.Add(7);
            tree.Add(3);
            tree.Add(6);
            tree.Add(1);
            tree.Add(8);

            int[] expected = new[] { 4, 2, 1, 3, 6, 5, 7, 8 };

            int index = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: yfann/CSharp_Algorithm
        private static void AVLTreeTest()
        {
            AVLTree<int, int> avl = new AVLTree<int, int>();

            for (int i = 1; i < 11; i++)
            {
                avl.Add(i, i);
            }
            Console.WriteLine("Root:{0}", avl.Root.Key.ToString());
            Console.WriteLine("Height:{0}", avl.Root.Height);
            Console.WriteLine("Inorder:{0}", avl.Print());
            Queue<AVLNode<int, int>> que = new Queue<AVLNode<int, int>>();

            ViewAVLLayer(avl.Root, que);
        }
コード例 #10
0
ファイル: AdditionTests.cs プロジェクト: osst/University
        public void InOrderBalancingTest()
        {
            AVLTree<int> tree = new AVLTree<int>();
            //No matter whatr the input is, output is sorted data

            tree.Add(4);
            tree.Add(5);
            tree.Add(2);
            tree.Add(7);
            tree.Add(3);
            tree.Add(6);
            tree.Add(1);
            tree.Add(8);

            int[] expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            int index = 0;

            tree.InOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
        }
コード例 #11
0
ファイル: AVLTreeTests.cs プロジェクト: dkblock/Personal
        public void CountDecreasesAfterRemoving()
        {
            var tree = new AVLTree <int, int>();
            var n    = 10;
            var m    = 2;

            for (int i = 0; i < n; i++)
            {
                tree.Add(i, i);
            }

            for (int i = 0; i < m; i++)
            {
                tree.Remove(i);
            }

            var expected = n - m;

            Assert.AreEqual(tree.Count, expected);
        }
コード例 #12
0
        public void Test(Dictionary <int, int> dict, string expected)
        {
            // Arrange
            var tree = new AVLTree <int, int>();

            foreach (var item in dict)
            {
                tree.Add(item.Key, item.Value);
            }


            var v = new AVLTreeVisitor <int, int>();

            // Act
            tree.InOrder(v);
            var result = v.String;

            // Assert
            Assert.AreEqual(expected, result);
        }
コード例 #13
0
        public void Remove_Missing_From_Tree()
        {
            var tree = new AVLTree <int>();

            //         4
            //       /   \
            //      2     8
            //     / \    /
            //    1   3  6
            //          / \
            //         5   7

            int[] values = { 4, 2, 1, 3, 8, 6, 7, 5 };

            foreach (var i in values)
            {
                Assert.IsFalse(tree.Contains(10), "Tree should not contain 10");
                tree.Add(i);
            }
        }
コード例 #14
0
        public IEnumerator GetEnumerator()
        {
            List <int> parameters = new List <int> {
                10, 3, 2, 4, 12, 15, 11
            };

            foreach (int i in parameters)
            {
                actualTree.Add(i);
            }

            List <int> expected = new List <int> {
                2, 3, 4, 10, 12, 15
            };
            List <int> expected2 = new List <int> {
                3, 4, 10, 12, 15
            };
            List <int> expected3 = new List <int> {
                3, 4, 10, 12
            };

            List <int> parameters2 = new List <int> {
                10, 3, 2, 4, 12, 15, 11, 25, 14
            };

            foreach (int i in parameters2)
            {
                balanceTree.Add(i);
            }
            List <int> expected4 = new List <int> {
                2, 3, 4, 10, 12, 14, 15, 25
            };

            yield return(new object[] { actualTree, 11, expected });

            yield return(new object[] { actualTree, 2, expected2 });

            yield return(new object[] { actualTree, 15, expected3 });

            yield return(new object[] { balanceTree, 11, expected4 });
        }
コード例 #15
0
        public void Remove_Head()
        {
            var tree = new AVLTree <int>();

            //     4
            //   /   \
            //  2     6
            // / \   / \
            //1   3 5   7
            //           \
            //            8


            tree.Add(4);
            tree.Add(5);
            tree.Add(2);
            tree.Add(7);
            tree.Add(3);
            tree.Add(6);
            tree.Add(1);
            tree.Add(8);

            tree.Remove(4);

            //     5
            //   /   \
            //  2     6
            // / \     \
            //1   3     7
            //           \
            //            8

            int[] expected = { 1, 3, 2, 8, 7, 6, 5 };

            var index = 0;

            tree.PostOrderTraversal(item => Assert.AreEqual(expected[index++], item));
        }
コード例 #16
0
 protected override IObservable <TwitterStatus> ReceiveSink(long?maxId)
 {
     System.Diagnostics.Debug.WriteLine("RECEIVESINK SEARCH QUERY: " + _query);
     return(Observable.Start(() => Setting.Accounts.GetRandomOne())
            .Where(a => a != null)
            .SelectMany(a => a.SearchAsync(_query, maxId: maxId,
                                           lang: String.IsNullOrWhiteSpace(Setting.SearchLanguage.Value)
                              ? null
                              : Setting.SearchLanguage.Value,
                                           locale: String.IsNullOrWhiteSpace(Setting.SearchLocale.Value)
                              ? null
                              : Setting.SearchLocale.Value)
                        .ToObservable())
            .Do(s =>
     {
         lock (_acceptIds)
         {
             _acceptIds.Add(s.Id);
         }
     }));
 }
コード例 #17
0
        public IEnumerator GetEnumerator()
        {
            List <int> parameters = new List <int> {
                10, 3, 2, 4, 12, 15, 11
            };

            foreach (int i in parameters)
            {
                actualTree.Add(i);
            }

            yield return(new object[] { actualTree, 11, true });

            yield return(new object[] { actualTree, 10, true });

            yield return(new object[] { actualTree, 2, true });

            yield return(new object[] { actualTree, 6, false });

            yield return(new object[] { actualTree, 8, false });
        }
コード例 #18
0
    static public void NumberOfValuesInsertedEqualsNumberOfValuesDeleted()
    {
        WriteLine("TEST: AVLTreeTest.NumberOfValuesInsertedEqualsNumberOfValuesDeleted");
        Indent();
        Random        rand     = new Random();
        AVLTree <int> t        = new AVLTree <int>();
        List <int>    l        = new List <int>();
        int           inserted = 0;

        for (int i = 0; i < 100; ++i)
        {
            int r = rand.Next(1000);
            inserted += t.Add(r) ? 1 : 0;
            l.Add(r);
        }

        Assert(inserted == t.Count, $"Number of values inserted ({inserted}) does not equal internal Count ({t.Count}).");

        int count = 0;

        foreach (int i in l)
        {
            count += t.Contains(i) ? 1 : 0;
        }

        Assert(count == l.Count, $"Contains method found {count} values in a list of {l.Count} (non-unique) values, which were all inserted.");

        int deleted = 0;

        foreach (int i in l)
        {
            deleted += t.Remove(i) ? 1 : 0;
        }

        Assert(inserted == deleted, $"Number of values inserted ({inserted}) does not equal number of values deleted ({deleted}).");

        // this test may report false positives in the case that the (generic type T) values stored in the tree are null or whitespace.
        Assert(string.IsNullOrWhiteSpace(t.ToString()), "Tree not empty after deleting all values.");
        Unindent();
    }
コード例 #19
0
        public void Remove_Node_Left_Leaf()
        {
            var tree = new AVLTree <int>();

            //         4
            //       /   \
            //      2     6
            //     / \   / \
            //    1   3 5   7
            //               \
            //               8

            tree.Add(4);
            tree.Add(5);
            tree.Add(2);
            tree.Add(7);
            tree.Add(3);
            tree.Add(6);
            tree.Add(1);
            tree.Add(8);

            Assert.IsTrue(tree.Remove(1), "Remove should return true for found node");

            //        4
            //      /   \
            //    2      6
            //     \    / \
            //      3  5   7
            //              \
            //               8

            int[] expected = { 3, 2, 5, 8, 7, 6, 4 };

            var index = 0;

            tree.PostOrderTraversal(item => Assert.AreEqual(expected[index++], item));
        }
コード例 #20
0
ファイル: AVLTreeTests.cs プロジェクト: zack7891/DSA
        public void SortedElementsAfterAdding()
        {
            var tree = new AVLTree <int>();

            int elementsCount = 100000;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                    }
                    el += i;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item)
                {
                    elementsAreSorted = false;
                }
                last = item;
                count++;
            }

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
コード例 #21
0
        public void IndexingNodesInBinaryTree1()
        {
            Graph        g         = new Graph();
            ILiteralNode canonical = (1).ToLiteral(g);
            ILiteralNode alternate = g.CreateLiteralNode("01", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeInteger));

            AVLTree <INode, int> tree = new AVLTree <INode, int>();

            tree.Add(canonical, 1);
            Assert.Equal(1, tree[canonical]);
            tree[alternate] = 2;

            //Since the default comparer considers the keys to be equal
            //lookup via either key should now give the value 2 rather than the originally
            //set value since the 2nd Add() just changes the existing value for the key
            //rather than adding a new key value pair
            Assert.Equal(2, tree[alternate]);
            Assert.Equal(2, tree[canonical]);

            //With the default comparer we expect to see 1 here rather than 2 because
            //the keys are considered equal
            Assert.Single(tree.Keys);
        }
コード例 #22
0
        public void RemovalTest(Dictionary <int, int> dict, int[] remove, string[] expected)
        {
            // Arrange
            var tree = new AVLTree <int, int>();

            foreach (var item in dict)
            {
                tree.Add(item.Key, item.Value);
            }

            for (int i = 0; i < remove.Length; i++)
            {
                // Arrange
                var v = new AVLTreeVisitor <int, int>();

                // Act
                tree.Remove(remove[i]);
                tree.InOrder(v);

                // Assert
                Assert.AreEqual(expected[i], v.String);
            }
        }
コード例 #23
0
        public void AddingMultipleItems_InBalancedWay_ShouldForeachInOrder()
        {
            var numbers = TestUtils.ToIntArrayUnique("20 10 30 0 15 25 40");
            var tree    = new AVLTree <int>();

            foreach (int number in numbers)
            {
                tree.Add(number);
            }

            var sortedNumbers    = numbers.OrderBy(n => n).ToArray();
            var expectedSequence = new Queue <int>(sortedNumbers);

            int count = 0;

            tree.ForeachDfs((depth, num) =>
            {
                Assert.AreEqual(expectedSequence.Dequeue(), num);
                count++;
            });

            Assert.AreEqual(count, tree.Count);
        }
コード例 #24
0
        /// <summary>
        /// Metodo que se va a encargar de encontrar un conjunto de
        /// PRGMTYPETB dados por sus identificadores.
        /// </summary>
        /// <param name="idPrgTypeTbList"></param>
        /// <returns></returns>
        private AVLTree <decimal, PRGMTYPETB> GetPRGMTYPETBFromDB(List <decimal?> idPrgTypeTbList)
        {
            var avl = new AVLTree <decimal, PRGMTYPETB>();

            using (var conn =
                       new OracleConnection(_iConfig.GetConnectionString("OracleConnection")))
            {
                var cmd = conn.CreateCommand();
                cmd.CommandText = $"SELECT PRGMTYPEID,PRGMTYPENAME,PRGMTYPEDESCP FROM PRGMTYPETB WHERE  {CreateOrStatements(idPrgTypeTbList.Select(id => id.ToString()), "PRGMTYPEID")} ";
                conn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var prgTypeTb = CreatePRGMTYPETBFromReader(reader);
                        avl.Add(prgTypeTb.PRGMTYPEID, prgTypeTb);
                    }
                }
            }

            return(avl);
        }
コード例 #25
0
        public void Add_And_Remove_1k_Unique_Items()
        {
            AVLTree <int> tree  = new AVLTree <int>();
            List <int>    items = new List <int>();

            // add random unique items to the tree
            Random rng = new Random();

            while (items.Count < 1000)
            {
                int next = rng.Next();
                if (!items.Contains(next))
                {
                    items.Add(next);
                    tree.Add(next);

                    Assert.AreEqual(items.Count, tree.Count, "items and tree collection should have the same count");
                }
            }

            // make sure they all exist in the tree
            foreach (int value in items)
            {
                Assert.IsTrue(tree.Contains(value), "The tree does not contain the expected value " + value.ToString());
            }

            // remove the item from the tree and make sure it's gone
            foreach (int value in items)
            {
                Assert.IsTrue(tree.Remove(value), "The tree does not contain the expected value " + value.ToString());
                Assert.IsFalse(tree.Contains(value), "The tree should not have contained the value " + value.ToString());
                Assert.IsFalse(tree.Remove(value), "The tree should not have contained the value " + value.ToString());
            }

            // now make sure the tree is empty
            Assert.AreEqual(0, tree.Count, "The tree should be empty");
        }
コード例 #26
0
        static void Main()
        {
            string[] input = Console.ReadLine().Split(' ');
            int      n     = int.Parse(input[0]);

            AVLTree tree = new AVLTree();

            for (int i = 0; i < n; i++)
            {
                input = Console.ReadLine().Split(' ');
                long value = long.Parse(input[1]);

                switch (input[0])
                {
                case "?":
                    Console.WriteLine(tree.Find(tree.GetModifiedValue(value)) != null ? "Found" : "Not found");
                    //Console.WriteLine(tree.Find(value) != null ? "Found" : "Not found");
                    break;

                case "+":
                    tree.Add(tree.GetModifiedValue(value));
                    //tree.Add(value);
                    break;

                case "-":
                    tree.Remove(tree.GetModifiedValue(value));
                    //tree.Remove(value);
                    break;

                case "s":
                    tree.Sum(tree.GetModifiedValue(value), tree.GetModifiedValue(long.Parse(input[2])));
                    break;
                }
            }

            Console.Read();
        }
コード例 #27
0
ファイル: Form1.cs プロジェクト: GerenkoOlga/printqueue
 private void ShowPrintQueue(int highlightedOne)
 {
     //Отобразить состояние очереди
     richTextBox3.Text = "";
     AVLTree<Client> reservedQueue = new AVLTree<Client>();
     Client[] clientArr = new Client[highlightedOne];
     m_printerQueue.CopyTo(clientArr);//скопруем очередь в массив клиентов
     foreach (Client element in clientArr)
     {
         // делаем копию очереди reservedQueue, ее и распечатываем
         reservedQueue.Add(element);
     }
     int count = reservedQueue.Count;
     for (int i = 0; i < count; i++)
     {
         Client peekedClient = reservedQueue.MaxValue;//метод, возвращающий клиента с максимальным приоритетом
         bool d = reservedQueue.Remove(peekedClient);
         richTextBox3.Text += (i + 1).ToString() + ". " + peekedClient.GetClientName() + " ("
         + peekedClient.GetPriority() + ")" + "    [" + peekedClient.GetLeftTime() + "]";
         if (i == highlightedOne || peekedClient.GetTimeInQueue() < 3)
             richTextBox3.Text += "  <<<";
         richTextBox3.Text += "\n";
     }
 }
コード例 #28
0
        static void Main(string[] args)
        {
            var a = new AVLTree <string, int>();
            //var a = new SortedDictionary<string, int>();
            //string input_text = "a b  l d e  g  h h k c m ";
            string str = "";

            string[] input_check = System.IO.File.ReadAllText(@"big.txt");
            // string[] input_check = System.IO.File.ReadAllLines(@"check.txt");
            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
            long elapsedMs;

            foreach (var i in input_text)
            {
                if (i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z' || i == '\'')
                {
                    str += i;
                }
                else if (str.Length > 0)
                {
                    if (a.ContainsKey(str))
                    {
                        ++a[str];
                    }
                    else
                    {
                        a.Add(str, 1);
                    }
                    str = "";
                }
            }
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            System.Console.WriteLine("time:  " + elapsedMs);
            //a.Print();
        }
コード例 #29
0
        public void AddingMultipleItems_RandomOrder_ShouldForeachInOrder()
        {
            var nums = TestUtils.ToIntArrayUnique("1 5 3 20 6 13 40 70 100 200 -50");

            var tree = new AVLTree <int>();

            foreach (var num in nums)
            {
                tree.Add(num);
            }

            var sortedNumbers    = nums.OrderBy(n => n).ToArray();
            var expectedSequence = new Queue <int>(sortedNumbers);

            int count = 0;

            tree.ForeachDfs((depth, num) =>
            {
                Assert.AreEqual(expectedSequence.Dequeue(), num);
                count++;
            });

            Assert.AreEqual(count, tree.Count);
        }
コード例 #30
0
        public void Enumerator_Of_Single()
        {
            var tree = new AVLTree <int>();

            foreach (var item in tree)
            {
                Assert.IsTrue(false, "An empty tree should not enumerate any values");
            }

            Assert.IsFalse(tree.Contains(10), "Tree should not contain 10 yet");

            tree.Add(10);

            Assert.IsTrue(tree.Contains(10), "Tree should contain 10");

            var count = 0;

            foreach (var item in tree)
            {
                count++;
                Assert.AreEqual(1, count);
                Assert.AreEqual(10, item);
            }
        }
コード例 #31
0
ファイル: AdditionTests.cs プロジェクト: osst/University
        public void SingleElementsAddsUpToTree()
        {
            AVLTree<int> tree = new AVLTree<int>();

            foreach (int item in tree)
            {
                Assert.Fail("An empty tree should not enumerate any values");
            }

            Assert.IsFalse(tree.Contains(10), "Tree should not contain 10 yet");

            tree.Add(10);

            Assert.IsTrue(tree.Contains(10), "Tree should contain 10");

            int count = 0;
            foreach (int item in tree)
            {
                count++;
                Assert.AreEqual(1, count, "There should be exactly one item");
                Assert.AreEqual(item, 10, "The item value should be 10");
            }
        }
コード例 #32
0
        public void TestSearchMin()
        {
            AVLTree <int> avletree = new AVLTree <int>(Comparer <int> .Default);

            avletree.Add(1);
            avletree.Add(2);
            avletree.Add(3);
            avletree.Add(4);
            avletree.Add(5);
            avletree.Add(4);
            avletree.Add(3);
            avletree.Delete(3);
            avletree.Delete(3);
            avletree.Add(1);
            avletree.Add(6);
            avletree.Add(5);
            avletree.Add(2);

            int min = avletree.SearchMin();

            Assert.AreEqual(1, min);
        }
コード例 #33
0
        public void TestFindNotExistElement()
        {
            AVLTree <int> avletree = new AVLTree <int>(Comparer <int> .Default);

            avletree.Add(1);
            avletree.Add(2);
            avletree.Add(3);
            avletree.Add(4);
            avletree.Add(5);
            avletree.Add(4);
            avletree.Add(3);
            avletree.Delete(3);
            avletree.Delete(3);
            avletree.Add(1);
            avletree.Add(6);
            avletree.Add(5);
            avletree.Add(2);

            bool exist = avletree.Search(3);

            Assert.AreEqual(false, exist);
        }
コード例 #34
0
        void Tick_timerTick(object sender, EventArgs e)
        {
            for (int i = 0; i < this.speed; i++)
            {
                if (solveState == SolveState.FIND)
                {
                    Cell current;
                    if (!queue.GetMin(out current))
                    {
                        this.time_record.Stop();
                        this.tick_timer.Stop();
                        this.Refresh();
                        return;
                    }
                    current.Open = State.CLOSED;
                    queue.DeleteMin();

                    getNeighbours(current.i, current.k).ForEach((n) => {
                        checkedTiles++;
                        float newG = current.gScore + Cell.d(current, n);
                        if (newG < n.gScore)
                        {
                            n.parent = current;
                            n.gScore = newG;
                        }

                        if (n == target)
                        {
                            solveState = SolveState.TRACE;
                            trace      = n;
                            pathTiles++;
                            trace.Open = State.PATH;
                            return;
                        }
                        if (n.Open == State.UNDEFINED)
                        {
                            n.Open = State.OPENED;
                            queue.Add(n);
                        }
                    });
                }
                else
                {
                    if (trace.parent != null)
                    {
                        pathTiles++;
                        trace      = trace.parent;
                        trace.Open = State.PATH;
                    }
                    else
                    {
                        time_record.Stop();
                        tick_timer.Stop();
                        this.Refresh();
                        this.checkedLabel.Text = checkedTiles.ToString();
                        this.pathLabel.Text    = pathTiles.ToString();
                        return;
                    }
                }
            }

            this.checkedLabel.Text = checkedTiles.ToString();
            this.pathLabel.Text    = pathTiles.ToString();
            this.Refresh();
        }
コード例 #35
0
        public void Rotation_Complexish()
        {
            AVLTree <int> tree = new AVLTree <int>();

            //      3
            //     /
            //    2
            //   /
            //  1
            tree.Add(3);
            tree.Add(2);
            tree.Add(1);

            //   2
            //  / \
            // 1   3

            int[] expected = new[] { 2, 1, 3 };
            int   index    = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
            Assert.AreEqual(index, expected.Length, "The wrong number of items were enumerated?");

            //   2
            //  / \
            // 1   3
            //      \
            //       4

            tree.Add(4);

            expected = new[] { 2, 1, 3, 4 };
            index    = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
            Assert.AreEqual(index, expected.Length, "The wrong number of items were enumerated?");

            //   2
            //  / \
            // 1   3
            //      \
            //       4
            //        \
            //         5

            tree.Add(5);

            //   2
            //  / \
            // 1   4
            //    /  \
            //   3    5

            expected = new[] { 2, 1, 4, 3, 5 };
            index    = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
            Assert.AreEqual(index, expected.Length, "The wrong number of items were enumerated?");

            //   2
            //  / \
            // 1   4
            //    /  \
            //   3    5
            //         \
            //          6

            tree.Add(6);

            //     4
            //    / \
            //   2   5
            //  / \   \
            // 1   3   6

            expected = new[] { 4, 2, 1, 3, 5, 6 };
            index    = 0;

            tree.PreOrderTraversal(item => Debug.WriteLine(item));

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
            Assert.AreEqual(index, expected.Length, "The wrong number of items were enumerated?");
        }
コード例 #36
0
ファイル: AVLTest.cs プロジェクト: pocketgems/Theraot
        public void AddRemoveAndBalanceDuplicates()
        {
            var values = new[]
            {
                9,
                77,
                50,
                48,
                24,
                60,
                72,
                95,
                66,
                27,
                28,
                33,
                50,
                65,
                37,
                75,
                58,
                36,
                74,
                60
            };
            var avl        = new AVLTree <int, int>();
            var expected   = new List <int>();
            var duplicates = new List <int>();

            foreach (var item in values)
            {
                bool duplicate = expected.Contains(item);
                if (duplicate)
                {
                    duplicates.Add(item);
                }
                else
                {
                    expected.Add(item);
                }
                avl.Add(item, item);
            }
            foreach (var duplicate in duplicates)
            {
                Assert.IsTrue(avl.Remove(duplicate));
            }
            Assert.AreEqual(expected.Count, avl.Count);
            expected.Sort();
            var index = 0;

            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            foreach (var duplicate in duplicates)
            {
                Assert.IsTrue(avl.Remove(duplicate));
                expected.Remove(duplicate);
            }
            Assert.AreEqual(expected.Count, avl.Count);
            expected.Sort();
            index = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
        }
コード例 #37
0
        public void EvenMoreTest()
        {
            var d = new AVLTree<int, int>();
            var sequence =
                "95,91,33,48,74,53,21,47,1,58,75,98,32,10,46,26,3,96,31,61,13,85,51,93,90,92,64,39,66,34,22,81,57,7,45,88,99,94,84,11,0,29,41,17,18,2,44,56,82,6,73,50,89,62,27,16,60,54,30,5,65,80,69,19,43,79,40,8,14,78,52,97,4,28,36,67,20,87,76,15,24,86,55,70,71,42,35,25,77,83,23,12,59,68,37,49,63,9,72,38"
                    .Split(',').Select(int.Parse).ToArray();
            
            int size = 0;
            foreach (var i in sequence)
            {
                size++;
                d.Add(i, i * 100);
                Assert.AreEqual(size, d.Root.Size);
                CheckBalance(d);
            }

            var removeSequence = 
                "64,70,55,54,46,21,87,53,96,51,23,24,9,40,12,39,99,27,75,11,50,31,2,18,7,34,95,83,86,20,42,92,48,63,22,89,47,68,36,84,1,19,28,25,93,15,81,43,71,65,78,8,10,76,17,91,98,69,37,79,33,13,45,73,35,0,66,77,58,41,29,72,59,56,62,32,60,49,82,97,3,88,4,80,74,57,52,5,94,26,16,30,67,14,85,61,44,38,6,90"
                    .Split(',').Select(int.Parse).ToArray();
            foreach (var i in removeSequence)
            {
                size--;
                d.Remove(i);
                Assert.AreEqual(size, d.Root == null ? 0 : d.Root.Size);
                CheckBalance(d);
            }
        }
コード例 #38
0
ファイル: AdditionTests.cs プロジェクト: osst/University
        public void SimpleTestIfBalancingWorks()
        {
            AVLTree<int> tree = new AVLTree<int>();
            tree.Add(1);
            tree.Add(2);
            tree.Add(3);
            tree.Add(4);
            tree.Add(5);
            int[] expected = new[] { 2, 1, 4, 3, 5 };

            int index = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item, "The item enumerated in the wrong order"));
        }
コード例 #39
0
        public void ExecuteTestTree()
        {
            List <string> hashes = new List <string>();
            List <string> values = new List <string>();
            const int     size   = 1000;

            Console.WriteLine("Small Tests...");
            Assert.IsTrue(tree.Count == 0);
            tree.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "value");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            tree.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "value2");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            tree.Add(new KeyValuePair <string, string>("key", "avalue"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "avalue");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            Assert.IsFalse(tree.Remove("value"));
            Assert.IsTrue(tree.Remove("key"));
            tree.Validate();
            Assert.IsTrue(tree.Count == 0);
            tree.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(tree.Count == 1);
            tree.Clear();
            Assert.IsTrue(tree.Count == 0);
            tree.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "value");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            tree.Clear();
            Assert.IsFalse(tree.Remove(""));
            Assert.IsFalse(tree.Remove(new KeyValuePair <string, string>("", "")));

            Console.WriteLine("Adding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree.Count == i);
                hashes.Add(Hash.GetHash());
                values.Add(Hash.GetHash());
                tree[hashes[i]] = values[i];
                Assert.IsTrue(tree[hashes[i]] == values[i]);
                Assert.IsTrue(tree.Keys.Contains(hashes[i]));
                Assert.IsTrue(tree.Values.Contains(values[i]));
                tree.Validate();
            }

            Console.WriteLine("Overriding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree[hashes[i]] == values[i]);
                values[i]       = Hash.GetHash();
                tree[hashes[i]] = values[i];
                Assert.IsTrue(tree.Count == size);
            }

            Console.WriteLine("Checking...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree[hashes[i]] == values[i]);
                Assert.IsTrue(tree.Keys.Contains(hashes[i]));
                Assert.IsTrue(tree.Values.Contains(values[i]));
            }

            Console.WriteLine("Validating...");

            tree.Validate();

            Console.WriteLine("Deleting...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree.Count == size - i);
                Assert.IsTrue(tree.ContainsKey(hashes[i]));
                Assert.IsTrue(tree[hashes[i]] != default(string));
                Assert.IsTrue(tree.Remove(hashes[i]));
                Assert.IsFalse(tree.Keys.Contains(hashes[i]));
                Assert.IsFalse(tree.Values.Contains(values[i]));

                if (true)
                {
                    for (int j = i + 1; j < size; j++)
                    {
                        Assert.IsFalse(tree[hashes[j]].Contains(hashes[j]));
                    }

                    for (int j = 0; j < i; j++)
                    {
                        Assert.IsFalse(tree.Remove(hashes[j]));
                    }
                }

                Assert.IsTrue(tree[hashes[i]] == default(string));
                tree.Validate();
            }

            Serializer.WriteXmlData(tree, nameof(tree));
            tree = Serializer.ReadXmlData <AVLTree <string, string> >(nameof(tree));

            tree.Validate();
        }
コード例 #40
0
        public void Rotation_Complexish()
        {
            var tree = new AVLTree <int>();

            //      3
            //     /
            //    2
            //   /
            //  1
            tree.Add(3);
            tree.Add(2);
            tree.Add(1);

            //   2
            //  / \
            // 1   3

            int[] expected = { 2, 1, 3 };
            var   index    = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item));
            Assert.AreEqual(index, expected.Length);

            //   2
            //  / \
            // 1   3
            //      \
            //       4

            tree.Add(4);

            expected = new[] { 2, 1, 3, 4 };
            index    = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item));
            Assert.AreEqual(index, expected.Length);

            //   2
            //  / \
            // 1   3
            //      \
            //       4
            //        \
            //         5

            tree.Add(5);

            //   2
            //  / \
            // 1   4
            //    /  \
            //   3    5

            expected = new[] { 2, 1, 4, 3, 5 };
            index    = 0;

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item));
            Assert.AreEqual(index, expected.Length);

            //   2
            //  / \
            // 1   4
            //    /  \
            //   3    5
            //         \
            //          6

            tree.Add(6);

            //     4
            //    / \
            //   2   5
            //  / \   \
            // 1   3   6

            expected = new[] { 4, 2, 1, 3, 5, 6 };
            index    = 0;

            tree.PreOrderTraversal(item => Debug.WriteLine(item));

            tree.PreOrderTraversal(item => Assert.AreEqual(expected[index++], item));
            Assert.AreEqual(index, expected.Length);
        }
コード例 #41
0
ファイル: AVLTest.cs プロジェクト: mesheets/Theraot-CF
 private static void AddAndIterate(int[] data)
 {
     var avl = new AVLTree<int, int>();
     foreach (var item in data)
     {
         avl.Add(item, item);
     }
     Assert.AreEqual(avl.Count, data.Length);
     Array.Sort(data);
     var index = 0;
     foreach (var item in avl)
     {
         Assert.AreEqual(item.Key, data[index]);
         index++;
     }
 }
コード例 #42
0
        public void MoreTest()
        {
            var d = new AVLTree<int, int>();
            d.Add(25, 0);
            CheckTree(d, "<25<><>>");
            d.Add(42, 0);
            CheckTree(d, "<25<><42>>");
            d.Add(55, 0);
            CheckTree(d, "<42<25><55>>");
            d.Add(72, 0);
            CheckTree(d, "<42<25><55<><72>>>");
            d.Add(24, 0);
            CheckTree(d, "<42<25<24><>><55<><72>>>");
            d.Add(96, 0);
            CheckTree(d, "<42<25<24><>><72<55><96>>>");
            d.Add(28, 0);
            CheckTree(d, "<42<25<24><28>><72<55><96>>>");

            d.Remove(28);
            CheckTree(d, "<42<25<24><>><72<55><96>>>");
            d.Remove(24);
            CheckTree(d, "<42<25><72<55><96>>>");
            d.Remove(42);
            CheckTree(d, "<55<25><72<><96>>>");
            d.Remove(55);
            CheckTree(d, "<72<25><96>>");
        }
コード例 #43
0
ファイル: AVLTest.cs プロジェクト: mesheets/Theraot-CF
 public void AddRemoveAndBalanceDuplicates()
 {
     var values = new[]
     {
         9,
         77,
         50,
         48,
         24,
         60,
         72,
         95,
         66,
         27,
         28,
         33,
         50,
         65,
         37,
         75,
         58,
         36,
         74,
         60
     };
     var avl = new AVLTree<int, int>();
     var expected = new List<int>();
     var duplicates = new List<int>();
     foreach (var item in values)
     {
         bool duplicate = expected.Contains(item);
         if (duplicate)
         {
             duplicates.Add(item);
         }
         else
         {
             expected.Add(item);
         }
         avl.Add(item, item);
     }
     foreach (var duplicate in duplicates)
     {
         Assert.IsTrue(avl.Remove(duplicate));
     }
     Assert.AreEqual(avl.Count, expected.Count);
     expected.Sort();
     var index = 0;
     foreach (var item in avl)
     {
         Assert.AreEqual(item.Key, expected[index]);
         index++;
     }
     foreach (var duplicate in duplicates)
     {
         Assert.IsTrue(avl.Remove(duplicate));
         expected.Remove(duplicate);
     }
     Assert.AreEqual(avl.Count, expected.Count);
     expected.Sort();
     index = 0;
     foreach (var item in avl)
     {
         Assert.AreEqual(item.Key, expected[index]);
         index++;
     }
 }
コード例 #44
0
ファイル: AVLTest.cs プロジェクト: pocketgems/Theraot
        public void LargeTreeTest()
        {
            var avl = new AVLTree <int, int> {
                { 4, 0 }, { 8, 0 }, { 12, 0 }, { 16, 0 }, { 18, 0 }, { 19, 0 }, { 21, 0 }
            };

            Assert.AreEqual(avl.Count, 7);
            var expected = new[] { 4, 8, 12, 16, 18, 19, 21 };
            var index    = 0;

            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(2, 0);
            avl.Add(6, 0);
            avl.Add(10, 0);
            avl.Add(14, 0);
            avl.Add(17, 0);
            avl.Add(20, 0);
            avl.Add(22, 0);
            expected = new[] { 2, 4, 6, 8, 10, 12, 14, 16, 17, 18, 19, 20, 21, 22 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(1, 0);
            avl.Add(3, 0);
            avl.Add(5, 0);
            avl.Add(7, 0);
            avl.Add(9, 0);
            avl.Add(11, 0);
            avl.Add(13, 0);
            avl.Add(15, 0);
            avl.Add(23, 0);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Remove(23);
            avl.Remove(20);
            avl.Remove(17);
            avl.Remove(22);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(9, 0);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(31, 0);
            avl.Add(27, 0);
            avl.Add(26, 0);
            avl.Add(24, 0);
            avl.Add(28, 0);
            avl.Add(30, 0);
            avl.Add(32, 0);
            avl.Add(29, 0);
            avl.Add(25, 0);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Remove(31);
            avl.Remove(30);
            avl.Remove(25);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21, 24, 26, 27, 28, 29, 32 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(20, 0);
            avl.Add(10, 0);
            avl.Add(30, 0);
            avl.Add(35, 0);
            avl.Add(31, 0);
            avl.Add(33, 0);
            avl.Add(32, 0);
            avl.Add(34, 0);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 35 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(39, 0);
            avl.Add(36, 0);
            avl.Add(46, 0);
            avl.Add(42, 0);
            avl.Add(38, 0);
            avl.Add(29, 0);
            avl.Add(21, 0);
            avl.Add(48, 0);
            avl.Add(44, 0);
            avl.Add(40, 0);
            avl.Add(47, 0);
            avl.Add(49, 0);
            avl.Add(41, 0);
            avl.Add(1, 0);
            avl.Add(28, 0);
            avl.Add(7, 0);
            avl.Add(43, 0);
            avl.Add(45, 0);
            avl.Add(37, 0);
            expected = new[] { 1, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 21, 24, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Remove(1);
            avl.Remove(7);
            avl.Remove(9);
            avl.Remove(10);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 21, 24, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(17, 0);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 21, 24, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Remove(21);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(22, 0);
            avl.Add(23, 0);
            avl.Add(25, 0);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Remove(28);
            avl.Remove(29);
            avl.Remove(32);
            expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
            avl.Add(50, 0);
            avl.Add(51, 0);
            avl.Add(52, 0);
            avl.Add(53, 0);
            avl.Add(54, 0);
            avl.Add(55, 0);
            avl.Add(56, 0);
            avl.Add(57, 0);
            avl.Add(58, 0);
            avl.Add(59, 0);
            avl.Add(60, 0);
            avl.Add(61, 0);
            avl.Add(62, 0);
            avl.Add(63, 0);
            avl.Add(64, 0);
            avl.Add(65, 0);
            avl.Add(66, 0);
            avl.Add(67, 0);
            avl.Add(68, 0);
            avl.Add(69, 0);
            avl.Add(70, 0);
            avl.Add(71, 0);
            avl.Add(72, 0);
            avl.Add(73, 0);
            avl.Add(74, 0);
            avl.Add(75, 0);
            avl.Add(76, 0);
            avl.Add(77, 0);
            avl.Add(78, 0);
            avl.Add(79, 0);
            avl.Add(80, 0);
            avl.Add(81, 0);
            avl.Add(82, 0);
            avl.Add(83, 0);
            avl.Add(84, 0);
            avl.Add(85, 0);
            avl.Add(86, 0);
            avl.Add(87, 0);
            avl.Add(88, 0);
            avl.Add(89, 0);
            avl.Add(90, 0);
            avl.Add(91, 0);
            avl.Add(92, 0);
            avl.Add(93, 0);
            avl.Add(94, 0);
            avl.Add(95, 0);
            avl.Add(96, 0);
            avl.Add(97, 0);
            avl.Add(98, 0);
            avl.Add(99, 0);
            avl.Add(0, 0);
            expected = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
            index    = 0;
            foreach (var item in avl)
            {
                Assert.AreEqual(expected[index], item.Key);
                index++;
            }
        }
コード例 #45
0
        public void TestSerializeClassAvlTree()
        {
            Console.WriteLine("Building Class AVLTree...");

            AVLTree <Person, Couple> tree = new AVLTree <Person, Couple>();
            int count = 1000;

            for (int i = 0; i < count; i++)
            {
                Person a = new Person()
                {
                    age = i, name = "a" + i
                };
                Person b = new Person()
                {
                    age = i, name = "b" + i
                };
                Couple c = new Couple()
                {
                    man = a, woman = b
                };

                tree.Add(a, c);
                tree.Add(b, c);
            }

            Console.WriteLine("Serializing...");
            Serializer.WriteXmlData(tree, "tree");

            Console.WriteLine("Deserializing...");
            tree = Serializer.ReadXmlData <AVLTree <Person, Couple> >("tree");

            Console.WriteLine("Validating...");
            for (int i = 0; i < count; i++)
            {
                Assert.IsTrue(tree[new Person()
                                   {
                                       age = i, name = "a" + i
                                   }].woman.Equals(new Person()
                {
                    age = i, name = "b" + i
                }));
                Assert.IsTrue(tree[new Person()
                                   {
                                       age = i, name = "b" + i
                                   }].woman.Equals(new Person()
                {
                    age = i, name = "b" + i
                }));
                Assert.IsTrue(tree[new Person()
                                   {
                                       age = i, name = "a" + i
                                   }].man.Equals(new Person()
                {
                    age = i, name = "a" + i
                }));
                Assert.IsTrue(tree[new Person()
                                   {
                                       age = i, name = "b" + i
                                   }].man.Equals(new Person()
                {
                    age = i, name = "a" + i
                }));
            }
        }
コード例 #46
0
        private static void SingleRandomTest()
        {
            var d = new AVLTree<int, int>();
            var sequence = Enumerable.Range(0, 100).Shuffle(new Random()).ToArray();
            int size = 0;
            foreach (var i in sequence)
            {
                d.Add(i, i*100);
                CheckBalance(d);
            }
            
            var removeSequence = Enumerable.Range(0, 50).Shuffle(new Random()).ToArray();
            foreach (var i in removeSequence)
            {
                d.Remove(i);
                CheckBalance(d);
            }

            var reAddSequence = removeSequence.Shuffle(new Random()).ToArray();
            foreach (var i in reAddSequence)
            {
                d.Add(i, i * 100);
                CheckBalance(d);
            }

            foreach (var i in d.Keys.ToArray())
            {
                d.Remove(i);
                CheckBalance(d);
            }
            Assert.IsNull(d.Root);
        }
コード例 #47
0
        void Tick_timerTick(object sender, EventArgs e)
        {
            for (int i = 0; i < this.speed; i++)
            {
                if (solveState == SolveState.FIND)
                {
                    Cell current;
                    if (!OpenSet.GetMin(out current))
                    {
                        time_record.Stop();
                        tick_timer.Stop();
                        this.Refresh();
                        return;
                    }
                    current.Open = State.CLOSED;
                    OpenSet.DeleteMin();

                    List <Cell> neighbours = getNeighbours(current.i, current.k);
                    neighbours.ForEach(n => {
                        float newGScore = current.gScore + Cell.d(current, n);
                        if (newGScore < n.gScore)
                        {
                            n.gScore = newGScore;
                            n.fScore = n.hScore * heuristic + n.gScore;
                            n.parent = current;

                            this.checkedTiles++;
                            if (n.Open == State.UNDEFINED)
                            {
                                n.Open = State.OPENED;
                                OpenSet.Add(n);
                            }
                        }
                    });

                    if (current == target)
                    {
                        solveState = SolveState.TRACE;
                        trace      = target;
                        pathTiles++;
                        trace.Open = State.PATH;
                    }
                }
                else
                {
                    if (trace.parent != null)
                    {
                        pathTiles++;
                        trace.parent.Open = State.PATH;
                        trace             = trace.parent;
                    }
                    else
                    {
                        tick_timer.Stop();
                        time_record.Stop();
                    }
                }
            }

            this.checkedLabel.Text = checkedTiles.ToString();
            this.pathLabel.Text    = pathTiles.ToString();
            this.Refresh();
        }
コード例 #48
0
    public static void Main()
    {
        AVLTree <int> sortedSet = new AVLTree <int>();

        while (true)
        {
            string s = ReadLine();
            WriteLine(s);
            if (string.IsNullOrEmpty(s))
            {
                break;
            }
            char command = s[0];
            if (!"IPQDR".Contains(command.ToString()))
            {
                continue;                                  // invalid command
            }
            if (command == 'P')
            {
                WriteLine(sortedSet);
                continue;
            }
            string argString;
            try
            {
                argString = s.Substring(2);
            }
            catch (ArgumentOutOfRangeException)
            {
                continue; // invalid argument
            }
            if (!int.TryParse(argString, out int arg))
            {
                continue; // invalid argument
            }
            switch (command)
            {
            case 'I':
                if (sortedSet.Add(arg))
                {
                    WriteLine("Element inserted");
                }
                else
                {
                    WriteLine("Element was already in the tree");
                }
                break;

            case 'D':
                if (sortedSet.Remove(arg))
                {
                    WriteLine("Element deleted");
                }
                else
                {
                    WriteLine("Element was not in the tree");
                }
                break;

            case 'Q':
                if (sortedSet.Contains(arg))
                {
                    WriteLine("Present");
                }
                else
                {
                    WriteLine("Absent");
                }
                break;
            }
        }
    }