Ejemplo n.º 1
0
        public void AddWithBlackUncleAndRedParent()
        {
            //должен быть совершен поворот относительно родителя
            //родитель должен стать черным,а новый ,брат - красным
            //для сохранения свойства "Оба потомка каждого красного узла — чёрные."
            //и свойства "Все пути от любого данного узла до листовых узлов содержат одинаковое число чёрных узлов."
            TreeAlgorithm tree    = new TreeAlgorithm();
            var           newNode = 30;

            //изначально имеет касного родителя и черного дядю
            tree.Add(56); //root black
            tree.Add(12); //black
            tree.Add(65); //parent red
            tree.Add(16); //red

            //добавление нового узла
            tree.Add(newNode);
            var result = tree.Root;

            //проверка
            Assert.AreEqual(16, result.Left.Value);
            Assert.AreEqual(Color.B, result.Left.Colour);       //цвет родителя
            Assert.AreEqual(12, result.Left.Left.Value);
            Assert.AreEqual(Color.R, result.Left.Left.Colour);  //цвет брата
            Assert.AreEqual(30, result.Left.Right.Value);
            Assert.AreEqual(Color.R, result.Left.Right.Colour); //цвет узла
        }
Ejemplo n.º 2
0
 public void TreeComplite(TreeAlgorithm tree, int[] values)
 {
     for (int i = 0; i < values.Length; i++)
     {
         tree.Add(values[i]);
     }
 }
Ejemplo n.º 3
0
 public void TreeComplite()
 {
     int[] values = { 22, 4, 61, 25, 66, 27, 10, 9, 7, 43 };
     for (int i = 0; i < values.Length; i++)
     {
         tree.Add(values[i]);
     }
 }
Ejemplo n.º 4
0
        public void MinTest()
        {
            if (tree.Root == null)
            {
                TreeComplite();
            }
            TreeAlgorithm tree1 = new TreeAlgorithm();

            tree1.Add(-69);
            tree1.Add(-15);
            tree1.Add(-36);
            tree1.Add(-17);
            TreeAlgorithm tree2 = new TreeAlgorithm();

            Assert.AreEqual(4, tree.Min().Value);
            Assert.AreEqual(-69, tree1.Min().Value);
            Assert.IsNull(tree2.Max());
        }
Ejemplo n.º 5
0
        public void MaxTest()
        {
            if (tree.Root == null)
            {
                TreeComplite();
            }
            TreeAlgorithm tree1 = new TreeAlgorithm();

            tree1.Add(-23);
            tree1.Add(-36);
            tree1.Add(-43);
            tree1.Add(-2);
            TreeAlgorithm tree2 = new TreeAlgorithm();

            Assert.AreEqual(66, tree.Max().Value);
            Assert.AreEqual(-2, tree1.Max().Value);
            Assert.IsNull(tree2.Max());
        }
Ejemplo n.º 6
0
        public void AddRoot()
        {
            TreeAlgorithm tree = new TreeAlgorithm();

            tree.Add(56);
            var expectedColor = Color.B;
            var resultColor   = tree.FindKey(56).Colour;

            Assert.AreEqual(expectedColor, resultColor);
        }
Ejemplo n.º 7
0
        public void AddWithRedUncleAndRedParent()
        {
            //должен помен¤тьс¤ цвет у родител¤  и д¤ди на черный
            //дл¤ сохранени¤ свойства "ќба потомка каждого красного узла Ч чЄрные."
            TreeAlgorithm tree = new TreeAlgorithm();

            tree.Add(56); //root black
            tree.Add(12); //red
            tree.Add(65); //red
            tree.Add(16);
            var result = tree.Root;

            Assert.AreEqual(56, result.Value);
            Assert.AreEqual(12, result.Left.Value);
            Assert.AreEqual(Color.B, result.Left.Colour); //цвет родител¤
            Assert.AreEqual(65, result.Right.Value);
            Assert.AreEqual(Color.B, result.Left.Colour); //цвет д¤ди
            Assert.AreEqual(16, result.Left.Right.Value);
        }
Ejemplo n.º 8
0
        public void AddWithRedParrent()
        {
            //Добавление узла к черному родителю
            TreeAlgorithm tree     = new TreeAlgorithm();
            var           newNode1 = 12;
            var           newNode2 = 65;

            //mainTree
            tree.Add(56);//root black Test1

            //добавление новых узлов
            tree.Add(newNode1);
            tree.Add(newNode2);
            var result = tree.Root;

            Assert.AreEqual(12, result.Left.Value);
            Assert.AreEqual(Color.R, result.Left.Colour); //цвет первого узла
            Assert.AreEqual(65, result.Right.Value);
            Assert.AreEqual(Color.R, result.Left.Colour); //цвет второго узла
        }
Ejemplo n.º 9
0
        public void AddWithRedUncleAndRedParent()
        {
            //должен поменяться цвет у родителя  и дяди на черный
            //для сохранения свойства "Оба потомка каждого красного узла — чёрные."
            TreeAlgorithm tree    = new TreeAlgorithm();
            var           newNode = 16;

            //mainTree
            tree.Add(56); //root black Test1
            tree.Add(12); //red Test2
            tree.Add(65); //red Test2

            //Добавление нового узла
            tree.Add(newNode);

            var result = tree.Root;

            Assert.AreEqual(56, result.Value);
            Assert.AreEqual(12, result.Left.Value);
            Assert.AreEqual(Color.B, result.Left.Colour); //цвет родителя
            Assert.AreEqual(65, result.Right.Value);
            Assert.AreEqual(Color.B, result.Left.Colour); //цвет дяди
            Assert.AreEqual(16, result.Left.Right.Value);
        }