Пример #1
0
        public void TestMethod3()
        {
            Node root = new Node(node_type.inner, new List<int>() { 16 });
            Node child1 = new Node(node_type.inner, new List<int>() { 7, 13 });
            Node child2 = new Node(node_type.inner, new List<int>() { 20, 24 });
            Node child3 = new Node(node_type.leaf, new List<int>() { 1, 2, 3, 4, 5 });
            Node child4 = new Node(node_type.leaf, new List<int>() { 10, 11, 12 });
            Node child5 = new Node(node_type.leaf, new List<int>() { 14, 15 });
            Node child6 = new Node(node_type.leaf, new List<int>() { 17, 18, 19 });
            Node child7 = new Node(node_type.leaf, new List<int>() { 21, 22 });
            Node child8 = new Node(node_type.leaf, new List<int>() { 25, 26 });

            root.AddChild(child1);
            root.AddChild(child2);
            child1.AddChild(child3);
            child1.AddChild(child4);
            child1.AddChild(child5);
            child2.AddChild(child6);
            child2.AddChild(child7);
            child2.AddChild(child8);

            BTree tree = new BTree(root, 3);

            bool flag = BTree.Check(tree);
            Assert.AreEqual(true, flag);
        }
Пример #2
0
        public void TestMethod2()
        {
            Node root = new Node(node_type.inner, new List<int>() { 7, 13, 20, 30 });
            Node child1 = new Node(node_type.leaf, new List<int>() { 1, 3, 4, 5 });
            Node child2 = new Node(node_type.leaf, new List<int>() { 10, 11 });
            Node child3 = new Node(node_type.leaf, new List<int>() { 14, 15 });
            Node child4 = new Node(node_type.leaf, new List<int>() { 19, 20, 21 });
            Node child5 = new Node(node_type.leaf, new List<int>() { 31, 32 });

            root.AddChild(child1);
            root.AddChild(child2);
            root.AddChild(child3);
            root.AddChild(child4);
            root.AddChild(child5);

            BTree tree = new BTree(root, 3);

            bool flag = BTree.Check(tree);
            Assert.AreEqual(false, flag);
        }
Пример #3
0
        public void TestMethod4()
        {
            Node root = new Node(node_type.inner, new List<int>() { 20, 22, 35 });
            Node child1 = new Node(node_type.leaf, new List<int>() { 5, 7, 10, 15 });
            Node child2 = new Node(node_type.leaf, new List<int>() { 23, 24 });
            Node child3 = new Node(node_type.leaf, new List<int>() { 27, 29, 30 });
            Node child4 = new Node(node_type.leaf, new List<int>() { 36, 38 });

            root.AddChild(child1);
            root.AddChild(child2);
            root.AddChild(child3);
            root.AddChild(child4);

            BTree tree = new BTree(root, 3);

            bool flag = BTree.Check(tree);
            Assert.AreEqual(false, flag);
        }
Пример #4
0
        public void TestMethod6()
        {
            Node root = new Node(node_type.inner, new List<int>() { 25 });
            Node child1 = new Node(node_type.inner, new List<int>() { 10, 16 });
            Node child2 = new Node(node_type.inner, new List<int>() { 26 });
            Node child3 = new Node(node_type.leaf, new List<int>() { 4, 6, 8 });
            Node child4 = new Node(node_type.leaf, new List<int>() { 12, 14 });
            Node child5 = new Node(node_type.leaf, new List<int>() { 18, 20 });
            Node child6 = new Node(node_type.leaf, new List<int>() { 24 });
            Node child7 = new Node(node_type.leaf, new List<int>() { 28, 30 });

            root.AddChild(child1);
            root.AddChild(child2);
            child1.AddChild(child3);
            child1.AddChild(child4);
            child1.AddChild(child5);
            child2.AddChild(child6);
            child2.AddChild(child7);

            BTree tree = new BTree(root, 2);

            bool flag = BTree.Check(tree);
            Assert.AreEqual(false, flag);
        }
Пример #5
0
        public void TestMethod9()
        {
            Node root = new Node(node_type.inner, new List<int>() { 22 });
            Node child1 = new Node(node_type.leaf, new List<int>() { 10 });

            root.AddChild(child1);

            BTree tree = new BTree(root, 2);

            bool flag = BTree.Check(tree);
            Assert.AreEqual(false, flag);
        }
Пример #6
0
        /// <summary>
        /// Проверка узла дерева на:
        /// 1. Упорядоченность элементов по возрастанию
        /// 2. Для каждого элемента узла: левый сосед меньше, правый - больше
        /// </summary>
        /// <param name="node"> узел для проверки </param>
        /// <param name="kTree"> коэффициент дерева </param>
        /// <returns> результат проверки </returns>
        public static bool NodeTest(Node node, int kTree)
        {
            if (node.type == 0)
            {
                if (!(node.values.Count >= kTree - 1 && node.children.Count <= 2 * kTree - 1))
                    return false;
                for (int i = 0; i < node.values.Count - 1; i++)
                    if (node.values[i] > node.values[i + 1])
                        return false;
            }
            for (int i = 0; i < node.children.Count; i++)
            {
                int nodeCount = node.children[i].values.Count;
                int childrenCount = node.children[i].children.Count;
                if (!(nodeCount >= kTree - 1 && nodeCount <= 2 * kTree - 1))
                    return false;

                if (childrenCount != 0)
                {
                    if (!(nodeCount + 1 == childrenCount))
                        return false;
                }

                for (int j = 0; j < nodeCount - 1; j++)
                {
                    if (node.children[i].values[j] > node.children[i].values[j + 1])
                        return false;
                    if (childrenCount != 0)
                    {
                        if (node.children[i].values[j] < node.children[i].children[j].values[0])
                            return false;
                        if (node.children[i].values[j] > node.children[i].children[j + 1].values[0])
                            return false;
                    }
                }
                int nodeValue = node.children[i].values[nodeCount - 1];
                if (childrenCount != 0)
                {
                    if (nodeValue < node.children[i].children[nodeCount - 1].values[0])
                        return false;
                    if (nodeValue > node.children[i].children[nodeCount].values[0])
                        return false;
                }

            }
            return true;
        }
Пример #7
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="_root"> корень дерева </param>
 /// <param name="_kTree"> коэффициент дерева </param>
 public BTree(Node _root, int _kTree)
 {
     root = _root;
     kTree = _kTree;
 }
Пример #8
0
 /// <summary>
 /// Добавление детей к узлу
 /// </summary>
 /// <param name="child"> ребенок данного узла (типа узел) </param>
 public void AddChild(Node child)
 {
     this.children.Add(child);
 }
Пример #9
0
 /// <summary>
 /// Помощь в проверке дерева на упорядоченность.
 /// Проверяет правых соседей
 /// </summary>
 /// <param name="node"> узел (сначала - корень) </param>
 /// <param name="element"> элемент узла, для которого проверка </param>
 /// <returns> результат проверки </returns>
 public static bool OrderTestHelper1(Node node, int element)
 {
     for (int i = 0; i < node.children.Count; i++)
     {
         if (node.children[i].values[0] < element)
             return false;
         for (int j = 0; j < node.children[i].children.Count; j++)
             OrderTestHelper1(node.children[i].children[j], element);
     }
     return true;
 }
Пример #10
0
        /// <summary>
        /// Проверка дерева на упорядоченность
        /// </summary>
        /// <param name="root"> корень дерева </param>
        /// <returns> результат проверки </returns>
        public static bool OrderTest(Node root)
        {
            int rootCount = root.values.Count;
            int childrenCount = root.children.Count;
            for (int i = 0; i < rootCount; i++)
            {
                int rootElement = root.values[i];
                bool flag;
                flag = OrderTestHelper(root.children[i], rootElement);
                if (!flag)
                    return false;
            }
            bool f = OrderTestHelper1(root.children[rootCount], root.values[rootCount - 1]);
            if (!f)
                return false;

            return true;
        }