Ejemplo n.º 1
0
        public void AvlTree_AddRotateLeft_Test()
        {
            Func <int, int, bool> Smaller = (a, b) => { return(a < b); };
            AvlTree <int>         Tree    = new AvlTree <int>(Smaller);

            AvlTree <int> .NodeCheck
                _2 = AvlTree <int> .NodeCheck.ConvirmValue(2),
                _1 = AvlTree <int> .NodeCheck.ConvirmValue(1),
                _4 = AvlTree <int> .NodeCheck.ConvirmValue(4),
                _3 = AvlTree <int> .NodeCheck.ConvirmValue(3),
                _5 = AvlTree <int> .NodeCheck.ConvirmValue(5);

            _2.NextCheck = _1;
            _1.NextCheck = _4;
            _4.NextCheck = _3;
            _3.NextCheck = _5;

            Tree.Add(1);
            Tree.Add(2);
            Tree.Add(3);
            Tree.Add(4);
            Tree.Add(5);

            //Tree.Dump();
            Tree.HierarchyCheck(_2);
        }
        public void Add_LeftRotationOnLeftChild()
        {
            var tree = new AvlTree <int>();

            tree.Add(50);
            tree.Add(30);
            tree.Add(60);
            tree.Add(20);
            tree.Add(40);
            tree.Add(55);
            tree.Add(65);
            tree.Add(10);
            tree.Add(25);
            tree.Add(5);

            Assert.Equal(50, tree.Root.Value);
            Assert.Equal(20, tree.Root.Left.Value);
            Assert.Equal(10, tree.Root.Left.Left.Value);
            Assert.Equal(5, tree.Root.Left.Left.Left.Value);
            Assert.Null(tree.Root.Left.Left.Right);
            Assert.Equal(30, tree.Root.Left.Right.Value);
            Assert.Equal(25, tree.Root.Left.Right.Left.Value);
            Assert.Equal(40, tree.Root.Left.Right.Right.Value);
            Assert.Equal(60, tree.Root.Right.Value);
            Assert.Equal(55, tree.Root.Right.Left.Value);
            Assert.Equal(65, tree.Root.Right.Right.Value);
            AssertBalanced(tree);
            AssertOrdered(tree);
        }
        public void Add_RightRotation()
        {
            var tree = new AvlTree <int>();

            tree.Add(2);
            tree.Add(3);
            tree.Add(4);

            Assert.Equal(3, tree.Root.Value);
            Assert.Equal(2, tree.Root.Left.Value);
            Assert.Equal(4, tree.Root.Right.Value);

            tree.Add(5);

            Assert.Equal(3, tree.Root.Value);
            Assert.Equal(2, tree.Root.Left.Value);
            Assert.Equal(4, tree.Root.Right.Value);
            Assert.Equal(5, tree.Root.Right.Right.Value);

            tree.Add(6);

            Assert.Equal(3, tree.Root.Value);
            Assert.Equal(2, tree.Root.Left.Value);
            Assert.Equal(5, tree.Root.Right.Value);
            Assert.Equal(4, tree.Root.Right.Left.Value);
            Assert.Equal(6, tree.Root.Right.Right.Value);
            AssertBalanced(tree);
            AssertOrdered(tree);
        }
Ejemplo n.º 4
0
        public void AddTwoElementSecondIsGreaterFirst()
        {
            var tree = new AvlTree <int, int>();

            tree.Add(0, 0);
            tree.Add(1, 0);
            Assert.AreEqual("0 0\n1 0\n", tree.Print());
        }
Ejemplo n.º 5
0
        public void AddTwoElementSecondIsLessFirst()
        {
            var tree = new AvlTree <int, int>();

            tree.Add(0, 0);
            tree.Add(-1, 0);
            Assert.AreEqual("-1 0\n0 0\n", tree.Print());
        }
Ejemplo n.º 6
0
        public void CountAfterAdding()
        {
            var tree = new AvlTree <int, int>();

            tree.Add(0, 0);
            tree.Add(1, 0);
            Assert.AreEqual(2, tree.Count);
        }
Ejemplo n.º 7
0
 public void TestAddInOrder()
 {
     var tree = new AvlTree<TestNode> ();
     tree.Add (new TestNode (1));
     tree.Add (new TestNode (2));
     tree.Add (new TestNode (3));
     Assert.AreEqual (3, tree.Count);
     Assert.AreEqual ("1,2,3,", GetContent (tree));
 }
Ejemplo n.º 8
0
        public void TestAddOutOfOrder()
        {
            var tree = new AvlTree <TestNode> ();

            tree.Add(new TestNode(3));
            tree.Add(new TestNode(1));
            tree.Add(new TestNode(2));
            Assert.AreEqual(3, tree.Count);
            Assert.AreEqual("1,2,3,", GetContent(tree));
        }
Ejemplo n.º 9
0
        public void InsertAndRebalance3ItemsTest()
        {
            AvlTree <int> newTree = new AvlTree <int>();

            newTree.Add(25);
            newTree.Add(12);
            newTree.Add(14);

            Assert.AreEqual(14, newTree.Root.Data);
        }
Ejemplo n.º 10
0
        public void GeeksForGeeks_TreeConstructionAndRemoval()
        {
            //Arrange
            var avl = new AvlTree <int, string>();

            avl.Add(9, "9");
            avl.Add(5, "5");
            avl.Add(10, "10");
            avl.Add(0, "0");
            avl.Add(6, "6");
            avl.Add(11, "11");
            avl.Add(-1, "-1");
            avl.Add(1, "1");
            avl.Add(2, "2");
            //Act
            var result = avl.Remove(10);
            var tree   = avl._root;

            //Assert
            Assert.True(result);
            Assert.Equal(1, tree.Key);
            Assert.Equal(9, tree.Right.Key);
            Assert.Equal(0, tree.Left.Key);
            Assert.Equal(11, tree.Right.Right.Key);
            Assert.Equal(5, tree.Right.Left.Key);
            Assert.Equal(6, tree.Right.Left.Right.Key);
            Assert.Equal(2, tree.Right.Left.Left.Key);
            Assert.Equal(-1, tree.Left.Left.Key);
        }
        public void RemoveTest()
        {
            var tree = new AvlTree <int>();

            tree.Add(30);
            tree.Add(20);
            tree.Add(40);
            tree.Add(10);
            tree.Add(25);
            tree.Add(35);
            tree.Add(50);
            tree.Add(24);
            tree.Add(26);

            tree.Remove(20);

            Assert.Equal(30, tree.Root.Value);
            Assert.Equal(40, tree.Root.Right.Value);
            Assert.Equal(35, tree.Root.Right.Left.Value);
            Assert.Equal(50, tree.Root.Right.Right.Value);
            Assert.Equal(24, tree.Root.Left.Value);
            Assert.Equal(10, tree.Root.Left.Left.Value);
            Assert.Equal(25, tree.Root.Left.Right.Value);
            Assert.Null(tree.Root.Left.Right.Left);
            Assert.Equal(26, tree.Root.Left.Right.Right.Value);
        }
Ejemplo n.º 12
0
        public void TreeConstruction()
        {
            //Arrange
            var avl = new AvlTree <int, string>();

            avl.Add(9, "9");
            avl.Add(5, "5");
            avl.Add(10, "10");
            avl.Add(0, "0");
            avl.Add(6, "6");
            avl.Add(11, "11");
            avl.Add(-1, "-1");
            avl.Add(1, "1");
            avl.Add(2, "2");
            //Act
            var result = avl._root;

            //Assert
            Assert.Equal(9, result.Key);
            Assert.Equal(10, result.Right.Key);
            Assert.Equal(1, result.Left.Key);
            Assert.Equal(11, result.Right.Right.Key);
            Assert.Equal(5, result.Left.Right.Key);
            Assert.Equal(6, result.Left.Right.Right.Key);
            Assert.Equal(2, result.Left.Right.Left.Key);
            Assert.Equal(0, result.Left.Left.Key);
            Assert.Equal(-1, result.Left.Left.Left.Key);
        }
        public void ShouldBalanceTreeWhenItIsLeftRightOverweight()
        {
            var avlTree = new AvlTree<int>(40);

            avlTree.Add(15);
            avlTree.Add(25);

            Assert.AreEqual(25, avlTree.Root.Value);
            Assert.AreEqual(15, avlTree.Root.LeftChild.Value);
            Assert.AreEqual(40, avlTree.Root.RightChild.Value);
        }
Ejemplo n.º 14
0
        public void ShouldBalanceTreeWhenItIsLeftRightOverweight()
        {
            var avlTree = new AvlTree <int>(40);

            avlTree.Add(15);
            avlTree.Add(25);

            Assert.AreEqual(25, avlTree.Root.Value);
            Assert.AreEqual(15, avlTree.Root.LeftChild.Value);
            Assert.AreEqual(40, avlTree.Root.RightChild.Value);
        }
Ejemplo n.º 15
0
        public void LeftRightRotationNoChildrenRootChangeTest()
        {
            AvlTree <int> avl = new AvlTree <int>(null);

            avl.Add(30);
            avl.Add(10);
            avl.Add(20);

            Assert.AreEqual(20, avl.Root.Data);
            Assert.AreEqual(30, avl.Root.Right.Data);
            Assert.AreEqual(10, avl.Root.Left.Data);
        }
Ejemplo n.º 16
0
        public void RemoveWithAncestorNoRotationTest()
        {
            AvlTree <int> avl = new AvlTree <int>(null);

            avl.Add(5);
            avl.Add(6);
            avl.Add(2);
            avl.Add(1);

            avl.RemoveNode(2);
            Assert.AreEqual(2, avl.Root.Left.Data);
        }
Ejemplo n.º 17
0
        public void RemoveTest()
        {
            const int rootKey   = 4;
            const int removeKey = 3;

            var tree = new AvlTree();

            tree.Add(rootKey);
            tree.Add(removeKey);
            tree.Remove(removeKey);

            Assert.IsTrue(tree.Root == rootKey, "Deleted node with wrong key");
        }
Ejemplo n.º 18
0
        public void RemoveWithoutRebalanceTest()
        {
            AvlTree <int> avl = new AvlTree <int>(null);

            avl.Add(40);
            avl.Add(50);
            avl.Add(30);

            avl.RemoveNode(50);
            avl.RemoveNode(30);
            Assert.AreEqual(40, avl.Root.Data);
            Assert.IsNull(avl.Root.Left);
            Assert.IsNull(avl.Root.Right);
        }
Ejemplo n.º 19
0
        public static void Main()
        {
            AvlTree <int> avl = new AvlTree <int>(null);

            avl.Add(20);
            avl.Add(10);
            avl.Add(30);
            avl.Add(40);
            avl.Add(50);


            Console.WriteLine("Asi sa nic nepokazilo");
            Console.ReadKey();
        }
        public void CreateBalancedTree1()
        {
            var tree = new AvlTree <int>();

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

            Assert.Equal(2, tree.Root.Value);

            tree.Add(6);

            Assert.Equal(2, tree.Root.Value);

            tree.Add(15);

            Assert.Equal(2, tree.Root.Value);

            tree.Add(-2);

            Assert.Equal(2, tree.Root.Value);

            tree.Add(-5);

            Assert.Equal(2, tree.Root.Value);

            tree.Add(-8);

            Assert.Equal(2, tree.Root.Value);
            AssertBalanced(tree);
            AssertOrdered(tree);
        }
        public void RightLeftRotation1()
        {
            var tree = new AvlTree <int>();

            tree.Add(30);
            tree.Add(40);
            tree.Add(35);

            Assert.Equal(35, tree.Root.Value);
            Assert.Equal(30, tree.Root.Left.Value);
            Assert.Equal(40, tree.Root.Right.Value);
            AssertBalanced(tree);
            AssertOrdered(tree);
        }
Ejemplo n.º 22
0
        public void InOrderTraversTest()
        {
            var tree = new AvlTree();

            tree.Add(4);
            tree.Add(3);
            tree.Add(5);
            tree.Add(7);

            var result         = tree.InOrderTravers();
            var expectedResult = new[] { 3, 4, 5, 7 };

            Assert.IsTrue(result.SequenceEqual(expectedResult), "The tree traver is wrong");
        }
Ejemplo n.º 23
0
        public void Add_KeyAlreadyInTree_ThrowsException()
        {
            var tree = new AvlTree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5 });
            Assert.Throws <ArgumentException>(() => tree.Add(1));
        }
Ejemplo n.º 24
0
        /// <summary>Loads a shader file, compiles it on the GPU, and adds it to the database.</summary>
        /// <param name="shaderId">The name you want to associate with this specific shader so you can use it later.</param>
        /// <param name="filePath">The file location of the shader file.</param>
        /// <returns>If the load was successful. (true if successful; false if unsuccessful)</returns>
        public static bool LoadExtendedGeometryShader(string shaderId, string filePath)
        {
            // Store the entire file in a string to be compiled on the GPU.
            string shaderSource;

            using (StreamReader reader = new StreamReader(filePath)) { shaderSource = reader.ReadToEnd(); }

            // Attempt to load the shader on the GPU and compile it
            int shaderHandle;

            shaderHandle = GL.CreateShader(ShaderType.GeometryShaderExt);
            GL.ShaderSource(shaderHandle, shaderSource);
            GL.CompileShader(shaderHandle);

            // Check for compiler errors
            string[] filePathSplit    = filePath.Split('\\');
            string   compilationError = GL.GetShaderInfoLog(shaderHandle);

            if (compilationError == "" || compilationError == "No errors\n")
            {
                Output.WriteLine("ERROR loading shader \"" + filePathSplit[filePathSplit.Length - 1] + "\";");
                Output.WriteLine(compilationError);
                return(false);
            }

            // The load and comilation was successful, we can add it to the database.
            _extendedGeometryShaderDatabase.Add(new ExtendedGeometryShader(shaderId, filePath, shaderHandle));
            Output.WriteLine("Shader file compiled \"" + filePathSplit[filePathSplit.Length - 1] + "\";");
            return(true);
        }
Ejemplo n.º 25
0
        public void AddOneElement()
        {
            var tree = new AvlTree <int, int>();

            tree.Add(0, 0);
            Assert.AreEqual("0 0\n", tree.Print());
        }
Ejemplo n.º 26
0
        public void InsertionAndDeletionPUT([PexAssumeUnderTest] List <int> values, int start, int end)
        {
            PexAssume.IsFalse(start < 0 || end < 0);
            PexAssume.IsTrue(start < values.Count);
            PexAssume.IsTrue(end >= start && end < values.Count);
            PexAssume.AreDistinctValues <int>(values.ToArray());
            AvlTree <int> avlTree = new AvlTree <int>();

            foreach (int i in values)
            {
                avlTree.Add(i);
            }
            PexAssert.AreEqual(values.Count, avlTree.Count);
            PexObserve.ValueForViewing <int>("Root", avlTree.Root.Value);
            int         toRemoveCount = (end - start) == 0?1:end - start;
            IList <int> toRemove      = values.GetRange(start, toRemoveCount);

            foreach (int i in toRemove)
            {
                avlTree.Remove(i);
            }

            PexAssert.AreEqual(values.Count - toRemoveCount, avlTree.Count);
            IEnumerable enumerator = avlTree.GetInorderEnumerator();

            foreach (int i in enumerator)
            {
                PexObserve.ValueForViewing <int>("tree nodes", i);
            }
        }
Ejemplo n.º 27
0
        public void CompareRemoveTime()
        {
            var itemsCount = 10000;

            var list = new List <int>();

            for (int i = 0; i < itemsCount; i++)
            {
                list.Add(i);
            }

            var startTime = DateTime.Now;

            list.Remove(5000);

            var listRemoveTime = DateTime.Now - startTime;

            var avlTree = new AvlTree <int>();

            for (int i = 0; i < itemsCount; i++)
            {
                avlTree.Add(i);
            }

            startTime = DateTime.Now;

            avlTree.Remove(5000);

            var avlRemoveTime = DateTime.Now - startTime;
        }
Ejemplo n.º 28
0
        public void CompareSearchTime()
        {
            var itemsCount = 10000;

            var list = new List <int>();

            for (int i = 0; i < itemsCount; i++)
            {
                list.Add(i);
            }

            var startTime = DateTime.Now;

            list.Contains(5000);

            var listContainsTime = DateTime.Now - startTime;

            var avlTree = new AvlTree <int>();

            for (int i = 0; i < itemsCount; i++)
            {
                avlTree.Add(i);
            }

            startTime = DateTime.Now;

            avlTree.Contains(5000);

            var avlContainsTime = DateTime.Now - startTime;
        }
Ejemplo n.º 29
0
        public void CompareAddTime()
        {
            var itemsCount = 10000;

            var startTime = DateTime.Now;

            var list = new List <int>();

            for (int i = 0; i < itemsCount; i++)
            {
                list.Add(i);
            }

            var listAddingTime = DateTime.Now - startTime;

            startTime = DateTime.Now;
            var avlTree = new AvlTree <int>();

            for (int i = 0; i < itemsCount; i++)
            {
                avlTree.Add(i);
            }

            var avlAddTree = DateTime.Now - startTime;
        }
        public void CompareSearchTime()
        {
            var itemsCount = 10000;
            
            var list = new List<int>();
            for (int i = 0; i < itemsCount; i++)
            {
                list.Add(i);
            }

            var startTime = DateTime.Now;

            list.Contains(5000);

            var listContainsTime = DateTime.Now - startTime;

            var avlTree = new AvlTree<int>();

            for (int i = 0; i < itemsCount; i++)
            {
                avlTree.Add(i);
            }

            startTime = DateTime.Now;

            avlTree.Contains(5000);

            var avlContainsTime = DateTime.Now - startTime;
        }
Ejemplo n.º 31
0
        static void Main(string[] args)
        {
            var tree = new AvlTree<int>();
            //tree.Add(50);
            //tree.Add(25);
            //tree.Add(75);
            //tree.Add(65);
            //tree.Add(70);
            //tree.Add(234);
            //tree.Add(116);
            //tree.Add(6);
            //tree.Add(18);
            //tree.Add(71);
            //tree.Add(90);
            //tree.Add(120);
            //tree.Add(300);

            int[] nodes = Console.ReadLine().Split().Select(int.Parse).ToArray();
            foreach (var node in nodes)
            {
                tree.Add(node);
            }

            int[] rangeParams = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int start = rangeParams[0];
            int end = rangeParams[1];

            Console.WriteLine(string.Join(" ", tree.Range(start, end)));
            Console.WriteLine(tree.getRangeCounter);
            // tree.Add(65);
            //tree.Add(70);
            Console.WriteLine();
        }
Ejemplo n.º 32
0
        public static AvlTree<CharacterData> Parse(string filePath)
        {
            AvlTree<CharacterData> charDictionary = new AvlTree<CharacterData>();

              string[] lines = File.ReadAllLines(filePath);

              for (int i = HeaderSize; i < lines.Length; i += 1)
              {
            string firstLine = lines[i];
            string[] typesAndValues = firstLine.Split(" ".ToCharArray(),
            StringSplitOptions.RemoveEmptyEntries);

            // All the data comes in a certain order,
            // used to make the parser shorter
            CharacterData charData = new CharacterData
            {
              Id = GetValue(typesAndValues[1]),
              X = GetValue(typesAndValues[2]),
              Y = GetValue(typesAndValues[3]),
              Width = GetValue(typesAndValues[4]),
              Height = GetValue(typesAndValues[5]),
              XOffset = GetValue(typesAndValues[6]),
              YOffset = GetValue(typesAndValues[7]),
              XAdvance = GetValue(typesAndValues[8])
            };
            charDictionary.Add(((char)charData.Id).ToString(), charData);
              }
              return charDictionary;
        }
Ejemplo n.º 33
0
        public void Foreach_AddedManyRandomElements_ShouldReturnSortedAscending()
        {
            const int NumCount = 10000;
            var       tree     = new AvlTree <int>();
            var       nums     = new HashSet <int>();
            var       random   = new Random();

            for (int i = 0; i < NumCount; i++)
            {
                var num = random.Next(0, NumCount);
                nums.Add(num);
                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);
        }
Ejemplo n.º 34
0
        private void Execute(int[] points)
        {
            _testee = new AvlTree <int>(points.Length);

            var sw = new Stopwatch();

            sw.Start();
            foreach (var p in points)
            {
                _testee.Add(p);
            }
            sw.Stop();

            Console.WriteLine("Add {0} values to tree was {1}ms", points.Length, sw.ElapsedMilliseconds);

            Assert.AreEqual(points.Length, _testee.Count);
            var list = new List <int>();

            sw.Reset();

            sw.Start();
            foreach (var p in points)
            {
                var index = list.BinarySearch(p);
                if (index < 0)
                {
                    index = ~index;
                }

                list.Insert(index, p);
            }
            sw.Stop();

            Console.WriteLine("Add {0} values to list was {1}ms", points.Length, sw.ElapsedMilliseconds);
        }
        public void GlobalSetup()
        {
            var random = new Random(Seed);
            var set    = new HashSet <int>(Count);

            _data        = new int[Count];
            _tree        = new AvlTree <int>();
            _compactTree = new CompactAvlTree <int>();
            _list        = new List <int>();
            _set         = new SortedSet <int>();
            for (int i = 0; i < Count; i++)
            {
                int number;
                do
                {
                    number = random.Next(MaxNumber) | 1;
                } while (!set.Add(number));
                _data[i] = number;
                var num1 = number + 1;
                _tree.Add(num1);
                _compactTree.Add(num1);
                _set.Add(num1);
                _list.Add(num1);
            }
            _list.Sort();
        }
Ejemplo n.º 36
0
        public static void Main()
        {
            var treeElements = Console.ReadLine().Split().Select(int.Parse);
            var tree = new AvlTree<int>();

            foreach (int element in treeElements)
            {
                tree.Add(element);
            }

            // Problem 2 - Range
            var range = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var elementsInRange = tree.Range(range[0], range[1]);
            Console.WriteLine(string.Join(" ", elementsInRange));

            // Problem 3 - Indexing
            //int index;
            //bool isIndex = int.TryParse(Console.ReadLine(), out index);
            //while (isIndex)
            //{
            //    try
            //    {
            //        Console.WriteLine(tree[index]);
            //    }
            //    catch (IndexOutOfRangeException)
            //    {
            //        Console.WriteLine("Invalid index");
            //    }

            //    isIndex = int.TryParse(Console.ReadLine(), out index);
            //}
        }
Ejemplo n.º 37
0
        static void Index()
        {
            var nums = new int[] { -2, 1, 0, 4, 5, 6, 10, 13, 14, 15, 17, 18, 20};
            var tree = new AvlTree<int>();
            foreach (var num in nums)
            {
                tree.Add(num);
            }

            string index;

            Console.WriteLine("Which index? 'q' for end:");
            while(true)
            {
                index = Console.ReadLine();
                if("q" == index)
                {
                    break;
                }
                try {
                    Console.WriteLine(tree[int.Parse(index)]);
                } catch (IndexOutOfRangeException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Ejemplo n.º 38
0
        static void Main()
        {
            var elements = Console.ReadLine()
                .Split(' ')
                .Select(int.Parse);
            var avlTree = new AvlTree<int>();

            foreach (var element in elements)
            {
                avlTree.Add(element);
            }

            var rangeTokens = Console.ReadLine()
                .Split(' ')
                .Select(int.Parse)
                .ToList();
            var from = rangeTokens[0];
            var to = rangeTokens[1];
            var range = avlTree.Range(from, to);

            if (range.Count == 0)
            {
                Console.WriteLine("(empty)");
            }
            else
            {
                foreach (var element in range)
                {
                    Console.WriteLine(element);
                }
            }
        }
        public void AddSeveralElements_EmptyTree_ShouldIncreaseCount()
        {
            var nums = TestUtils.ToIntArrayUnique("1 2 3");

            var tree = new AvlTree<int>();
            foreach (int num in nums)
            {
                tree.Add(num);
            }

            Assert.AreEqual(nums.Length, tree.Count);
        }
        public void Add_RepeatingElements_ShouldNotAddDuplicates()
        {
            var nums = TestUtils.ToIntArrayUnique("1 1 1");

            var tree = new AvlTree<int>();
            foreach (int num in nums)
            {
                tree.Add(num);
            }

            Assert.AreEqual(1, tree.Count);
        }
Ejemplo n.º 41
0
 /// <summary>Creates a static model from the ids provided.</summary>
 /// <param name="staticModelId">The id to represent this model as.</param>
 /// <param name="textures">An array of the texture ids for each sub-mesh of this model.</param>
 /// <param name="meshes">An array of each mesh id for this model.</param>
 /// <param name="meshNames">An array of mesh names for this specific instanc3e of a static model.</param>
 internal StaticModel(string staticModelId, string[] meshNames, string[] meshes, string[] textures)
 {
     if (textures.Length != meshes.Length && textures.Length != meshNames.Length)
     throw new Exception("Attempting to create a static model with non-matching number of components.");
       _id = staticModelId;
       _meshes = new AvlTreeLinked<StaticMesh, string>(StaticMesh.CompareTo, StaticMesh.CompareTo);
       for (int i = 0; i < textures.Length; i++)
     _meshes.Add(new StaticMesh(meshNames[i], TextureManager.Get(textures[i]), StaticModelManager.GetMesh(meshes[i]).StaticMeshInstance));
       _shaderOverride = null;
       _position = new Vector(0, 0, 0);
       _scale = new Vector(1, 1, 1);
       _orientation = Quaternion.FactoryIdentity;
 }
Ejemplo n.º 42
0
        public void AvlTree_AddDoubleRotateLeft_Test()
        {
            Func<int, int, bool> Smaller = (a, b) => { return a < b; };
            AvlTree<int> Tree = new AvlTree<int>(Smaller);
            AvlTree<int>.NodeCheck
                _3 = AvlTree<int>.NodeCheck.ConvirmValue(3),
                _1 = AvlTree<int>.NodeCheck.ConvirmValue(1),
                _2 = AvlTree<int>.NodeCheck.ConvirmValue(2),
                _4 = AvlTree<int>.NodeCheck.ConvirmValue(4),
                _5 = AvlTree<int>.NodeCheck.ConvirmValue(5);
            _3.NextCheck = _1;
            _1.NextCheck = _2;
            _2.NextCheck = _4;
            _4.NextCheck = _5;

            Tree.Add(1);
            Tree.Add(4);
            Tree.Add(3);
            Tree.Add(5);
            Tree.Add(2);

            //Tree.Dump();
            Tree.HierarchyCheck(_3);
        }
Ejemplo n.º 43
0
        static void Range()
        {
            Console.WriteLine("Add numbers separated by space");
            var nums = Console.ReadLine().Split(' ').Select(Int32.Parse).ToList();
            Console.WriteLine("Find range - two numbers separated by space.");
            var range = Console.ReadLine().Split(' ').Select(Int32.Parse).ToList();

            var tree = new AvlTree<int>();
            foreach (var num in nums)
            {
                tree.Add(num);
            }

            tree.Range(range[0], range[1]);
            Console.WriteLine();
        }
Ejemplo n.º 44
0
        static void Main(string[] args)
        {
            var tree = new AvlTree<int>();
            var random = new Random();

            for (int i = 0; i < 20; i++)
            {
                var num = random.Next(0, 1000);

                tree.Add(num);
            }

            tree.ForeachDfs((depth, num) =>
            {
                Console.WriteLine(num);
            });
        }
Ejemplo n.º 45
0
        private static void Main()
        {
            var tree = new AvlTree<int>();
            tree.Add(11);
            tree.Add(10);
            tree.Add(1);
            tree.Add(15);
            tree.Add(13);
            tree.Add(12);

            var vals = tree.ToArray();

            Console.ReadKey();
        }
        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);
        }
        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);
        }
        public void CompareAddTime()
        {
            var itemsCount = 10000;

            var startTime = DateTime.Now;

            var list = new List<int>();
            for (int i = 0; i < itemsCount; i++)
            {
                list.Add(i);
            }

            var listAddingTime = DateTime.Now - startTime;

            startTime = DateTime.Now;
            var avlTree = new AvlTree<int>();

            for (int i = 0; i < itemsCount; i++)
            {
                avlTree.Add(i);
            }

            var avlAddTree = DateTime.Now - startTime;
        }
Ejemplo n.º 49
0
 public void TestDetachNodesAtLeftChildAfterDeletingRoot()
 {
     var tree = new AvlTree<TestNode> ();
     int[] keys = { 110, 122, 2, 134, 86, 14, 26, 182 };
     foreach (var key in keys) {
         tree.Add (new TestNode (key));
     }
     tree.Remove (tree.First (t => t.val == 110));
     Assert.AreEqual (26, ((TestNode)tree.First (t => t.val == 14).Right).val);
 }
Ejemplo n.º 50
0
        public void AvlTree_String_ExtendedInsertionRebalance_Test()
        {
            Func<string, string, bool> Smaller = (text1, text2) =>
            {
                return string.Compare(text1, text2, StringComparison.OrdinalIgnoreCase) < 0;
            };
            AvlTree<string> StrTree = new AvlTree<string>(Smaller);

            StrTree.Add("ORY");
            StrTree.Add("JFK");
            StrTree.Add("BRU");
            StrTree.Add("DUS");
            StrTree.Add("ZRH");
            StrTree.Add("MEX");
            StrTree.Add("ORD");
            StrTree.Add("NRT");
            StrTree.Add("ARN");
            StrTree.Add("GLA");
            StrTree.Add("GCM");

            AvlTree<string>.NodeCheck
                _JFK = AvlTree<string>.NodeCheck.ConvirmValue("JFK"),
                _BRU = AvlTree<string>.NodeCheck.ConvirmValue("BRU"),
                _ARN = AvlTree<string>.NodeCheck.ConvirmValue("ARN"),
                _GCM = AvlTree<string>.NodeCheck.ConvirmValue("GCM"),
                _DUS = AvlTree<string>.NodeCheck.ConvirmValue("DUS"),
                _GLA = AvlTree<string>.NodeCheck.ConvirmValue("GLA"),
                _ORY = AvlTree<string>.NodeCheck.ConvirmValue("ORY"),
                _NRT = AvlTree<string>.NodeCheck.ConvirmValue("NRT"),
                _MEX = AvlTree<string>.NodeCheck.ConvirmValue("MEX"),
                _ORD = AvlTree<string>.NodeCheck.ConvirmValue("ORD"),
                _ZRH = AvlTree<string>.NodeCheck.ConvirmValue("ZRH");

            _JFK.NextCheck = _BRU;
            _BRU.NextCheck = _ARN;
            _ARN.NextCheck = _GCM;
            _GCM.NextCheck = _DUS;
            _DUS.NextCheck = _GLA;
            _GLA.NextCheck = _ORY;
            _ORY.NextCheck = _NRT;
            _NRT.NextCheck = _MEX;
            _MEX.NextCheck = _ORD;
            _ORD.NextCheck = _ZRH;

            StrTree.HierarchyCheck(_JFK);
        }
Ejemplo n.º 51
0
        public void AvlTree_Balance_Test()
        {
            AvlTree<int> Tree = new AvlTree<int>((a, b) => { return a < b; });

            Tree.Add(5);
            Tree.Add(4);
            Tree.Add(3);
            Tree.Add(2);
            Tree.Add(1);
            Tree.Add(0);
            Tree.Dump();

            AvlTree<int>.NodeCheck
                RootCheck = new AvlTree<int>.NodeCheck(),
                Check_Left1 = new AvlTree<int>.NodeCheck(),
                Check_Right1 = new AvlTree<int>.NodeCheck(),
                Check_Left1_Left2 = new AvlTree<int>.NodeCheck(),
                Check_Right1_Left2 = new AvlTree<int>.NodeCheck(),
                Check_Right1_Right2 = new AvlTree<int>.NodeCheck()
                ;
            // This is an explicit structural test
            RootCheck.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(2, nodeCheck.Value);

                Assert.AreEqual(0, nodeCheck.ParentId);
                Assert.AreNotEqual(0, nodeCheck.LeftId);
                Assert.AreNotEqual(0, nodeCheck.RightId);
                return true;
            };
            RootCheck.NextCheck = Check_Left1;

            Check_Left1.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(1, nodeCheck.Value);

                Assert.AreEqual(RootCheck.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(RootCheck.LeftId, nodeCheck.ThisId);
                Assert.Greater(RootCheck.Value, nodeCheck.Value);
                Assert.AreNotEqual(0, nodeCheck.LeftId); // should be left node
                Assert.AreEqual(0, nodeCheck.RightId); // but no right node.
                return true;
            };
            Check_Left1.NextCheck = Check_Left1_Left2;

            Check_Left1_Left2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(0, nodeCheck.Value);

                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.AreEqual(Check_Left1.ThisId, nodeCheck.ParentId);
                Assert.Greater(Check_Left1.Value, nodeCheck.Value);
                return true;
            };
            Check_Left1_Left2.NextCheck = Check_Right1;

            Check_Right1.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(4, nodeCheck.Value);

                Assert.AreEqual(RootCheck.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(RootCheck.RightId, nodeCheck.ThisId);
                Assert.AreNotEqual(0, nodeCheck.LeftId);
                Assert.AreNotEqual(0, nodeCheck.RightId);
                Assert.Less(RootCheck.Value, nodeCheck.Value);
                return true;
            };
            Check_Right1.NextCheck = Check_Right1_Left2;

            Check_Right1_Left2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(3, nodeCheck.Value);

                Assert.AreEqual(Check_Right1.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(Check_Right1.LeftId, nodeCheck.ThisId);
                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.Greater(Check_Right1.Value, nodeCheck.Value);
                return true;
            };
            Check_Right1_Left2.NextCheck = Check_Right1_Right2;

            Check_Right1_Right2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(5, nodeCheck.Value);

                Assert.AreEqual(Check_Right1.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(Check_Right1.RightId, nodeCheck.ThisId);
                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.Less(Check_Right1.Value, nodeCheck.Value);
                return false; // end of tree
            };

            Tree.HierarchyCheck(RootCheck);
            Console.WriteLine("\r\n: Tree Looks Like:");
            Tree.InOrder((num) => { Console.WriteLine("> {0}", num); });
        }
Ejemplo n.º 52
0
        public void TestRemove()
        {
            var tree = new AvlTree<TestNode> ();
            var t1 = new TestNode (1);
            var t2 = new TestNode (2);
            var t3 = new TestNode (3);

            tree.Add (t2);
            tree.Add (t1);
            tree.Add (t3);
            Assert.AreEqual (3, tree.Count);

            Assert.IsTrue (tree.Remove (t2));

            Assert.AreEqual (2, tree.Count);
            Assert.IsTrue (tree.Contains (t1));
            Assert.IsFalse (tree.Contains (t2));
            Assert.IsTrue (tree.Contains (t3));
        }
Ejemplo n.º 53
0
 public void InsertionRotateRightLeftTest()
 {
     AvlTree<int> avlTree = new AvlTree<int>(){6,5,15,7,16};
     avlTree.Add(14);
     Assert.AreEqual(7, avlTree.Root.Value);
     Assert.AreEqual(6, avlTree.Root.Left.Value);
     Assert.AreEqual(15, avlTree.Root.Right.Value);
     Assert.AreEqual(5, avlTree.Root.Left.Left.Value);
     Assert.AreEqual(14, avlTree.Root.Right.Left.Value);
     Assert.AreEqual(16, avlTree.Root.Right.Right.Value);
 }
Ejemplo n.º 54
0
        public void TestRemoveInLeftSubtree()
        {
            var tree = new AvlTree<TestNode> ();
            int[] keys = { 8, 4, 12, 6, 7, 16, 10, 5, 11, 9, 17, 5, 14, 2, 13, 1, 3 };
            foreach (var key in keys) {
                tree.Add (new TestNode (key));
            }

            tree.Remove (tree.First (t => t.val == 16));

            Assert.AreEqual( 8, tree.Root.val );
            Assert.AreEqual( 12, ((TestNode)tree.Root.Right).val );
            Assert.AreEqual( 14, ((TestNode)tree.Root.Right.Right).val );

            Assert.AreEqual (13, ((TestNode)tree.First (t => t.val == 14).Left).val);
        }
Ejemplo n.º 55
0
        public void TestAddCase2()
        {
            var tree = new AvlTree<TestNode> ();
            var t3 = new TestNode (3);
            var t24 = new TestNode (24);
            var t26 = new TestNode (26);

            tree.Add (t3);

            Assert.AreEqual (1, tree.Count);
            tree.Remove (t3);

            tree.Add (new TestNode (37));
            tree.Add (new TestNode (70));
            tree.Add (new TestNode (12));

            Assert.AreEqual (3, tree.Count);

            tree.Add (new TestNode (90));
            tree.Add (new TestNode (25));
            tree.Add (new TestNode (99));
            tree.Add (new TestNode (91));
            tree.Add (t24);
            tree.Add (new TestNode (28));
            tree.Add (t26);

            // Should do a single left rotation on node with key 12
            tree.Remove (t24);
            Assert.IsTrue (tree.Root.Left == t26, "was:" + tree.Root.Left);
        }
Ejemplo n.º 56
0
        public void TestTreeRoationAtLeftChildAfterDeletingRoot()
        {
            var tree = new AvlTree<TestNode> ();
            int[] keys = { 86, 110, 122, 2, 134, 26, 14, 182 };
            int[] expectedKeys = { 2, 14, 26, 86, 122, 134, 182 };

            foreach (var key in keys) {
                tree.Add (new TestNode (key));
            }
            tree.Remove (tree.First (t => t.val == 110));

            var node = tree.Root.AvlGetOuterLeft ();
            foreach (var expected in expectedKeys) {
                Assert.AreEqual (expected, node.val);
                node = node.AvlGetNextNode ();
            }
        }
        public void ShouldBalanceTreeWhenItIsLeftLeftOverweightInTheLeftSubtree()
        {
            var avlTree = new AvlTree<int>(new int[] { 25, 40, 17, 15, 22 });

            avlTree.Add(14);

            Assert.AreEqual(17, avlTree.Root.Value);
            Assert.AreEqual(15, avlTree.Root.LeftChild.Value);
            Assert.AreEqual(14, avlTree.Root.LeftChild.LeftChild.Value);
            Assert.AreEqual(25, avlTree.Root.RightChild.Value);
            Assert.AreEqual(22, avlTree.Root.RightChild.LeftChild.Value);
            Assert.AreEqual(40, avlTree.Root.RightChild.RightChild.Value);
        }
Ejemplo n.º 58
0
 public void TestRemoveInRightSubtree()
 {
     var tree = new AvlTree<TestNode> ();
     int[] keys = { 8, 4, 13, 6, 15, 7, 10, 5, 14, 2, 11, 3, 9, 1 };
     foreach (var key in keys) {
         tree.Add (new TestNode (key));
     }
     tree.Remove (tree.First (t => t.val == 13));
     Assert.AreEqual (11, ((TestNode)tree.First (t => t.val == 8).Right).val);
 }
Ejemplo n.º 59
0
        public void TestReverseOrderRemoval()
        {
            var tree = new AvlTree<TestNode> ();
            TestNode[] nodes = new TestNode[10];
            for (int i = 0; i < 10; i++) {
                tree.Add (nodes [i] = new TestNode (i));
            }
            Assert.AreEqual (10, tree.Count);

            for (int i = 0; i < 10; i++) {
                Assert.IsTrue (tree.Contains (nodes[9 - i]), "case : " + (9 - i));
                tree.Remove (nodes[9 - i]);
                Assert.IsFalse (tree.Contains (nodes[9 - i]), "case : " + (9 - i));
            }
            Assert.AreEqual (0, tree.Count);
        }
Ejemplo n.º 60
0
 public void InsertionAndDeletionTest()
 {
     AvlTree<uint> avlTree = new AvlTree<uint>();
     avlTree.Add(28);
     avlTree.Add(22);
     avlTree.Add(3);
     avlTree.Add(7);
     avlTree.Add(14);
     avlTree.Add(9);
     avlTree.Add(19);
     avlTree.Add(55);
     avlTree.Add(23);
     avlTree.Add(4);
     avlTree.Add(1);
     avlTree.Add(6);
     avlTree.Add(15);
     Assert.AreEqual(avlTree.Root.Value, 14);
     Assert.AreEqual(avlTree.Root.Left.Value, 4);
     Assert.AreEqual(avlTree.Root.Right.Value, 22);
     avlTree.Remove(4);
     avlTree.Remove(3);
     avlTree.Remove(1);
     avlTree.Remove(9);
     avlTree.Remove(23);
     avlTree.Remove(14);
     Assert.AreEqual(7, avlTree.Count);
     Assert.AreEqual(22, avlTree.Root.Value);
     Assert.AreEqual(7, avlTree.Root.Left.Value);
     Assert.AreEqual(28, avlTree.Root.Right.Value);
     Assert.AreEqual(55, avlTree.Root.Right.Right.Value);
     Assert.AreEqual(6, avlTree.Root.Left.Left.Value);
     Assert.AreEqual(19, avlTree.Root.Left.Right.Value);
     Assert.AreEqual(15, avlTree.Root.Left.Right.Left.Value);
 }