Exemple #1
0
        public void FindAfterDelete()
        {
            var root  = new SimpleTreeNode <int>(0, null);
            var node1 = new SimpleTreeNode <int>(1, null);
            var node2 = new SimpleTreeNode <int>(2, null);
            var node3 = new SimpleTreeNode <int>(3, null);
            var node4 = new SimpleTreeNode <int>(4, null);
            var node5 = new SimpleTreeNode <int>(5, null);

            var tree = new SimpleTree <int>(root);

            tree.AddChild(root, node1);
            tree.AddChild(root, node2);
            tree.AddChild(root, node3);
            tree.AddChild(node3, node4);
            tree.AddChild(node3, node5);

            Assert.True(tree.FindNodesByValue(5).Count == 1);

            tree.DeleteNode(node5);
            tree.DeleteNode(node2);

            Assert.True(tree.Count() == 4);
            Assert.True(tree.FindNodesByValue(5).Count == 0);
            Assert.True(tree.GetAllNodes().Count == 4);
            Assert.True(tree.LeafCount() == 2);
        }
Exemple #2
0
        public static void TestDeleteLeafFromTree()
        {
            var root  = new SimpleTreeNode <int>(1, null);
            var tree  = new SimpleTree <int>(root);
            var node2 = new SimpleTreeNode <int>(2, root);

            tree.AddChild(root, node2);
            var node3 = new SimpleTreeNode <int>(3, root);

            tree.AddChild(root, node3);
            var node4 = new SimpleTreeNode <int>(4, node3);

            tree.AddChild(node3, node4);
            var count     = tree.Count();
            var leafCount = tree.LeafCount();

            Assert.AreEqual(4, count);
            Assert.AreEqual(2, leafCount);

            tree.DeleteNode(node4);

            count     = tree.Count();
            leafCount = tree.LeafCount();
            Assert.AreEqual(3, count);
            Assert.AreEqual(2, leafCount);

            Assert.AreEqual(true, root.Children.Contains(node2));
            Assert.AreEqual(true, root.Children.Contains(node3));
            Assert.IsNull(node3.Children);
        }
Exemple #3
0
        public void TestGetAllNodes_3()
        {
            SimpleTreeNode <int> root  = new SimpleTreeNode <int>(1, null);
            SimpleTreeNode <int> node1 = new SimpleTreeNode <int>(11, null);
            SimpleTreeNode <int> node2 = new SimpleTreeNode <int>(12, null);
            SimpleTreeNode <int> node3 = new SimpleTreeNode <int>(13, null);

            SimpleTreeNode <int>[] nodes = new SimpleTreeNode <int>[]
            {
                root, node1, node2, node3
            };

            SimpleTree <int> tree = new SimpleTree <int>(root);

            tree.AddChild(root, node1);
            tree.AddChild(root, node2);
            tree.AddChild(root, node3);

            List <SimpleTreeNode <int> > list = tree.GetAllNodes();

            int index = 0;

            foreach (SimpleTreeNode <int> node in list)
            {
                Assert.AreEqual(nodes[index], node);
                index++;
            }
        }
        public void forest()
        {
            SimpleTreeNode <int> a1   = new SimpleTreeNode <int>(1, null);
            SimpleTreeNode <int> b1   = new SimpleTreeNode <int>(2, a1);
            SimpleTreeNode <int> b2   = new SimpleTreeNode <int>(3, a1);
            SimpleTreeNode <int> b3   = new SimpleTreeNode <int>(6, a1);
            SimpleTreeNode <int> c1   = new SimpleTreeNode <int>(5, b1);
            SimpleTreeNode <int> c2   = new SimpleTreeNode <int>(7, b1);
            SimpleTreeNode <int> c3   = new SimpleTreeNode <int>(4, b2);
            SimpleTreeNode <int> c4   = new SimpleTreeNode <int>(8, b3);
            SimpleTreeNode <int> d1   = new SimpleTreeNode <int>(9, c4);
            SimpleTreeNode <int> d2   = new SimpleTreeNode <int>(10, c4);
            SimpleTree <int>     tree = new SimpleTree <int>(a1);

            tree.AddChild(a1, b1);
            tree.AddChild(a1, b2);
            tree.AddChild(a1, b3);
            tree.AddChild(b1, c1);
            tree.AddChild(b1, c2);
            tree.AddChild(b2, c3);
            tree.AddChild(b3, c4);
            tree.AddChild(c4, d1);
            tree.AddChild(c4, d2);
            List <int> list = tree.EvenTrees();

            if (list[0] != 1 && list[1] != 3 && list[2] != 1 && list[3] != 6)
            {
                Assert.Fail();
            }
            if (tree.LeafCount() != 2)
            {
                Assert.Fail();
            }
        }
        // Возвращает дерево на основе массива узлов.
        //
        // Структура дерева:
        //      0:1-2, 2:3
        //
        private SimpleTree <int> GetTreeD()
        {
            SimpleTreeNode <int>[] nodes = GetNodesArray_4();

            SimpleTree <int> tree = new SimpleTree <int>(nodes[0]);

            tree.AddChild(tree.Root, nodes[1]);
            tree.AddChild(tree.Root, nodes[2]);

            tree.AddChild(nodes[2], nodes[3]);
            return(tree);
        }
Exemple #6
0
        public void TestCountLeaf_4()
        {
            SimpleTreeNode <int> root  = new SimpleTreeNode <int>(0, null);
            SimpleTreeNode <int> node1 = new SimpleTreeNode <int>(0, null);
            SimpleTreeNode <int> node2 = new SimpleTreeNode <int>(0, null);
            SimpleTreeNode <int> node3 = new SimpleTreeNode <int>(0, null);
            SimpleTree <int>     tree  = new SimpleTree <int>(root);

            tree.AddChild(root, node1);
            tree.AddChild(root, node2);
            tree.AddChild(root, node3);

            Assert.AreEqual(3, tree.LeafCount());
        }
        public void TestTree_1Count_I()
        {
            SimpleTreeNode <int> node_1 = new SimpleTreeNode <int>(1, null);
            SimpleTreeNode <int> node_2 = new SimpleTreeNode <int>(11, null);
            SimpleTreeNode <int> node_3 = new SimpleTreeNode <int>(12, null);

            SimpleTree <int> tree = new SimpleTree <int>(node_1);

            tree.AddChild(node_1, node_2);
            tree.AddChild(node_1, node_3);

            List <int> forest = tree.EvenTrees();

            Assert.IsTrue(forest.Count == 0);
        }
Exemple #8
0
        public void TestFindNodesByValue_2()
        {
            SimpleTreeNode <int>[] nodes = GetNodesArray_2();
            SimpleTree <int>       tree  = new SimpleTree <int>(nodes[0]);

            tree.AddChild(nodes[0], nodes[1]);
            tree.AddChild(nodes[1], nodes[2]);
            tree.AddChild(nodes[2], nodes[3]);

            List <SimpleTreeNode <int> > list = tree.FindNodesByValue(11);

            foreach (SimpleTreeNode <int> node in list)
            {
                Assert.AreEqual(11, node.NodeValue);
            }
            Assert.AreEqual(2, list.Count);
        }
Exemple #9
0
        public static void TestGetAllNodes()
        {
            var root = new SimpleTreeNode <int>(1, null);
            var tree = new SimpleTree <int>(root);
            var node = new SimpleTreeNode <int>(2, root);

            tree.AddChild(root, node);
            var node3 = new SimpleTreeNode <int>(3, root);

            tree.AddChild(root, node3);
            var node4 = new SimpleTreeNode <int>(4, node3);

            tree.AddChild(node3, node4);
            var result = tree.GetAllNodes();

            Assert.AreEqual(4, result.Count);
        }
Exemple #10
0
        public static void TestEvenTreesMoreComplexTree()
        {
            var root  = new SimpleTreeNode <int>(9, null);
            var tree  = new SimpleTree <int>(root);
            var node4 = new SimpleTreeNode <int>(4, root);

            tree.AddChild(root, node4);
            var node17 = new SimpleTreeNode <int>(17, root);

            tree.AddChild(root, node17);
            var node3 = new SimpleTreeNode <int>(3, node4);

            tree.AddChild(node4, node3);
            var node6 = new SimpleTreeNode <int>(6, node4);

            tree.AddChild(node4, node6);
            var node5 = new SimpleTreeNode <int>(5, node6);

            tree.AddChild(node6, node5);
            var node7 = new SimpleTreeNode <int>(7, node6);

            tree.AddChild(node6, node7);
            var node8 = new SimpleTreeNode <int>(8, node7);

            tree.AddChild(node7, node8);
            var node22 = new SimpleTreeNode <int>(22, node17);

            tree.AddChild(node17, node22);
            var node20 = new SimpleTreeNode <int>(20, node22);

            tree.AddChild(node22, node20);
            var node23 = new SimpleTreeNode <int>(23, node22);

            tree.AddChild(node22, node23);
            var list    = tree.EvenTrees();
            var ethalon = new List <int> {
                9, 4, 9, 17, 4, 6, 6, 7
            };

            Assert.AreEqual(ethalon.Count, list.Count);
            for (int i = 0; i < ethalon.Count; i++)
            {
                Assert.AreEqual(ethalon[i], list[i]);
            }
        }
        public void TestTree_1Count_G()
        {
            SimpleTreeNode <int> node_1 = new SimpleTreeNode <int>(1, null);
            SimpleTreeNode <int> node_2 = new SimpleTreeNode <int>(11, null);
            SimpleTreeNode <int> node_3 = new SimpleTreeNode <int>(111, null);

            SimpleTree <int> tree = new SimpleTree <int>(node_1);

            tree.AddChild(node_1, node_2);
            tree.AddChild(node_2, node_3);

            List <int> forest = tree.EvenTrees();

            foreach (int value in forest)
            {
                Assert.IsTrue(value == 1 || value == 11);
            }
        }
Exemple #12
0
 private static void Mapping(SimpleTree _simpleTree)
 {
     foreach (Transform t in _simpleTree.root)
     {
         SimpleTree st = new SimpleTree(t);
         _simpleTree.AddChild(st);
         Mapping(st);
     }
 }
Exemple #13
0
        public void TestCountLeaf_2()
        {
            SimpleTreeNode <int>[] nodes = GetNodesArray_1();
            SimpleTree <int>       tree  = GetTree(nodes);

            tree.AddChild(nodes[4], new SimpleTreeNode <int>(1111, null));
            tree.AddChild(nodes[5], new SimpleTreeNode <int>(1121, null));
            tree.AddChild(nodes[6], new SimpleTreeNode <int>(1211, null));
            tree.AddChild(nodes[7], new SimpleTreeNode <int>(1221, null));
            tree.AddChild(nodes[8], new SimpleTreeNode <int>(1231, null));
            tree.AddChild(nodes[9], new SimpleTreeNode <int>(1311, null));
            tree.AddChild(nodes[10], new SimpleTreeNode <int>(1321, null));
            tree.AddChild(nodes[11], new SimpleTreeNode <int>(1331, null));
            tree.AddChild(nodes[12], new SimpleTreeNode <int>(1341, null));
            tree.AddChild(nodes[13], new SimpleTreeNode <int>(1351, null));

            Assert.AreEqual(10, tree.LeafCount());
        }
Exemple #14
0
        public static void TestLeafCount()
        {
            var root = new SimpleTreeNode <int>(1, null);
            var tree = new SimpleTree <int>(root);
            var node = new SimpleTreeNode <int>(2, root);

            tree.AddChild(root, node);
            var node3 = new SimpleTreeNode <int>(3, root);

            tree.AddChild(root, node3);
            var node4 = new SimpleTreeNode <int>(4, node3);

            tree.AddChild(node3, node4);

            var count = tree.LeafCount();

            Assert.AreEqual(2, count);
        }
Exemple #15
0
        public static void TestAddNodeToTree()
        {
            var root    = new SimpleTreeNode <int>(1, null);
            var newNode = new SimpleTreeNode <int>(2, null);
            var tree    = new SimpleTree <int>(root);

            tree.AddChild(root, newNode);
            Assert.AreEqual(root, newNode.Parent);
            Assert.AreEqual(true, root.Children.Contains(newNode));
        }
Exemple #16
0
        public static void TestDeleteNodeFromTree()
        {
            var root = new SimpleTreeNode <int>(1, null);
            var tree = new SimpleTree <int>(root);
            var node = new SimpleTreeNode <int>(2, root);

            tree.AddChild(root, node);
            var node3 = new SimpleTreeNode <int>(3, null);
            var node4 = new SimpleTreeNode <int>(4, null);

            tree.AddChild(node, node3);
            tree.AddChild(node, node4);

            tree.DeleteNode(node);

            Assert.AreEqual(false, root.Children.Contains(node));
            Assert.AreEqual(true, root.Children.Contains(node3));
            Assert.AreEqual(true, root.Children.Contains(node4));
        }
Exemple #17
0
        public void TestAdd_A_1()
        {
            SimpleTreeNode <Int32> root = new SimpleTreeNode <int>(100, null);
            SimpleTree <Int32>     tree = new SimpleTree <Int32>(root);

            tree.AddChild(root, new SimpleTreeNode <int>(101, root));
            tree.AddChild(root, new SimpleTreeNode <int>(102, root));

            Assert.AreEqual(100, tree.Root.NodeValue);
            Assert.AreEqual(2, tree.Root.Children.Count);

            int value = 100;

            foreach (SimpleTreeNode <int> node in tree.Root.Children)
            {
                value++;
                Assert.AreEqual(value, node.NodeValue);
            }
        }
        public void TestTree_1Count_A()
        {
            SimpleTreeNode <int> node_root = new SimpleTreeNode <int>(0, null);
            SimpleTreeNode <int> node_1    = new SimpleTreeNode <int>(1, node_root);
            SimpleTreeNode <int> node_2    = new SimpleTreeNode <int>(2, node_1);
            SimpleTreeNode <int> node_3    = new SimpleTreeNode <int>(3, node_2);

            SimpleTree <int> tree = new SimpleTree <int>(node_root);

            tree.AddChild(node_root, node_1);
            tree.AddChild(node_1, node_2);
            tree.AddChild(node_2, node_3);

            List <int> forest = tree.EvenTrees();

            foreach (Int32 value in forest)
            {
                Assert.IsTrue(value == 1 || value == 2);
            }
        }
Exemple #19
0
        public static void TestCalcLevelAllNodes()
        {
            var root = new SimpleTreeNode <int>(1, null);
            var tree = new SimpleTree <int>(root);
            var node = new SimpleTreeNode <int>(2, root);

            tree.AddChild(root, node);
            var node3 = new SimpleTreeNode <int>(3, root);

            tree.AddChild(root, node3);
            var node4 = new SimpleTreeNode <int>(4, node3);

            tree.AddChild(node3, node4);

            tree.CalcLevelAllNodes();
            Assert.AreEqual(1, root.Level);
            Assert.AreEqual(2, node.Level);
            Assert.AreEqual(2, node3.Level);
            Assert.AreEqual(3, node4.Level);
        }
        // Возвращает дерево на основе массива узлов.
        //
        // Структура дерева:
        //      0:1-2-3, 1:4, 2:7-8, 4:5-6, 7:9
        //
        private SimpleTree <int> GetTreeB()
        {
            SimpleTreeNode <int>[] nodes = GetNodesArray_2();

            SimpleTree <int> tree = new SimpleTree <int>(nodes[0]);

            tree.AddChild(tree.Root, nodes[1]);
            tree.AddChild(tree.Root, nodes[2]);
            tree.AddChild(tree.Root, nodes[3]);

            tree.AddChild(nodes[1], nodes[4]);
            tree.AddChild(nodes[2], nodes[7]);
            tree.AddChild(nodes[2], nodes[8]);
            tree.AddChild(nodes[4], nodes[5]);
            tree.AddChild(nodes[4], nodes[6]);
            tree.AddChild(nodes[7], nodes[9]);
            return(tree);
        }
Exemple #21
0
        public void TestDelete_A_1()
        {
            SimpleTreeNode <Int32> root  = new SimpleTreeNode <Int32>(100, null);
            SimpleTreeNode <Int32> node1 = new SimpleTreeNode <Int32>(101, root);
            SimpleTreeNode <Int32> node2 = new SimpleTreeNode <Int32>(102, root);

            SimpleTree <Int32> tree = new SimpleTree <Int32>(root);

            tree.AddChild(root, node1);
            tree.AddChild(root, node2);

            tree.DeleteNode(node1);
            Assert.AreEqual(root, tree.Root);
            foreach (SimpleTreeNode <Int32> node in tree.Root.Children)
            {
                Assert.AreEqual(node2, node);
            }
            Assert.AreEqual(1, tree.Root.Children.Count);

            tree.DeleteNode(root);
            Assert.AreEqual(null, tree.Root);
        }
Exemple #22
0
        public void MoveBranch()
        {
            var root    = new SimpleTreeNode <int>(0, null);
            var node1   = new SimpleTreeNode <int>(1, null);
            var node2   = new SimpleTreeNode <int>(2, null);
            var node3   = new SimpleTreeNode <int>(3, null);
            var node4   = new SimpleTreeNode <int>(4, null);
            var node5   = new SimpleTreeNode <int>(5, null);
            var node5_2 = new SimpleTreeNode <int>(5, null);

            var tree = new SimpleTree <int>(root);

            tree.AddChild(root, node1);
            tree.AddChild(root, node2);
            tree.AddChild(node2, node3);
            tree.AddChild(node2, node4);

            tree.MoveNode(node2, node1);

            Assert.True(tree.Count() == 5);
            Assert.True(tree.GetAllNodes().Count == 5);
            Assert.True(tree.LeafCount() == 2);
        }
Exemple #23
0
        public static void TestEvenTrees()
        {
            var root  = new SimpleTreeNode <int>(1, null);
            var tree  = new SimpleTree <int>(root);
            var node2 = new SimpleTreeNode <int>(2, root);

            tree.AddChild(root, node2);
            var node3 = new SimpleTreeNode <int>(3, root);

            tree.AddChild(root, node3);
            var node6 = new SimpleTreeNode <int>(6, root);

            tree.AddChild(root, node6);
            var node5 = new SimpleTreeNode <int>(5, node2);

            tree.AddChild(node2, node5);
            var node7 = new SimpleTreeNode <int>(7, node2);

            tree.AddChild(node2, node7);
            var node4 = new SimpleTreeNode <int>(4, node3);

            tree.AddChild(node3, node4);
            var node8 = new SimpleTreeNode <int>(8, node6);

            tree.AddChild(node6, node8);
            var node9 = new SimpleTreeNode <int>(9, node8);

            tree.AddChild(node8, node9);
            var node10 = new SimpleTreeNode <int>(10, node8);

            tree.AddChild(node8, node10);
            var list    = tree.EvenTrees();
            var ethalon = new List <int> {
                1, 3, 1, 6
            };

            Assert.AreEqual(ethalon.Count, list.Count);
            for (int i = 0; i < ethalon.Count; i++)
            {
                Assert.AreEqual(ethalon[i], list[i]);
            }
        }
Exemple #24
0
        public static void TestFindNodesByValue()
        {
            var root = new SimpleTreeNode <int>(1, null);
            var tree = new SimpleTree <int>(root);
            var node = new SimpleTreeNode <int>(2, root);

            tree.AddChild(root, node);
            var node3 = new SimpleTreeNode <int>(3, root);

            tree.AddChild(root, node3);
            var node4 = new SimpleTreeNode <int>(2, node3);

            tree.AddChild(node3, node4);

            var testList = new List <SimpleTreeNode <int> >();

            testList.Add(node);
            testList.Add(node4);

            var result = tree.FindNodesByValue(2);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(testList, result);
        }
        public void DeleteNodeTest()
        {
            SimpleTreeNode <string> a1   = new SimpleTreeNode <string>("a", null);
            SimpleTreeNode <string> b1   = new SimpleTreeNode <string>("BA", a1);
            SimpleTreeNode <string> b2   = new SimpleTreeNode <string>("BB", a1);
            SimpleTreeNode <string> b3   = new SimpleTreeNode <string>("BC", a1);
            SimpleTreeNode <string> b4   = new SimpleTreeNode <string>("BC", a1);
            SimpleTreeNode <string> c2   = new SimpleTreeNode <string>("CA", null);
            SimpleTree <string>     tree = new SimpleTree <string>(a1);

            tree.AddChild(a1, b1);
            tree.AddChild(a1, b2);
            tree.AddChild(a1, b3);
            tree.AddChild(a1, b4);
            tree.AddChild(b2, c2);
            int A0 = tree.LeafCount();

            if (A0 != 4)
            {
                Assert.Fail();
            }
            int a0 = tree.Count();

            if (a0 != 6)
            {
                Assert.Fail();
            }
            tree.MoveNode(b2, b1);
            List <SimpleTreeNode <string> > aa = tree.GetAllNodes();
            int aaa = tree.Count();

            if (aaa != 6)
            {
                Assert.Fail();
            }
            int AA = tree.LeafCount();

            if (AA != 3)
            {
                Assert.Fail();
            }
            tree.DeleteNode(b2);
            int BB = tree.LeafCount();

            if (BB != 3)
            {
                Assert.Fail();
            }
            int bbb = tree.Count();

            if (bbb != 4)
            {
                Assert.Fail();
            }
            tree.DeleteNode(b3);
            int cc = tree.LeafCount();

            if (cc != 2)
            {
                Assert.Fail();
            }
            int CCC = tree.Count();

            if (CCC != 3)
            {
                Assert.Fail();
            }

            List <SimpleTreeNode <string> > aaaaaa = tree.FindNodesByValue("a");
            List <SimpleTreeNode <string> > ab     = tree.GetAllNodes();
        }
Exemple #26
0
        // Возвращает дерево на основе массива узлов.
        //
        // Структура дерева:
        //      0:1-2-3, 1:1-2, 2:1-2-3, 3:1-2-3-4-5.
        //
        private SimpleTree <int> GetTree(SimpleTreeNode <int>[] nodes)
        {
            SimpleTree <int> tree = new SimpleTree <int>(nodes[0]);

            tree.AddChild(tree.Root, nodes[1]);
            tree.AddChild(tree.Root, nodes[2]);
            tree.AddChild(tree.Root, nodes[3]);

            tree.AddChild(nodes[1], nodes[4]);
            tree.AddChild(nodes[1], nodes[5]);
            tree.AddChild(nodes[2], nodes[6]);
            tree.AddChild(nodes[2], nodes[7]);
            tree.AddChild(nodes[2], nodes[8]);
            tree.AddChild(nodes[3], nodes[9]);
            tree.AddChild(nodes[3], nodes[10]);
            tree.AddChild(nodes[3], nodes[11]);
            tree.AddChild(nodes[3], nodes[12]);
            tree.AddChild(nodes[3], nodes[13]);
            return(tree);
        }
Exemple #27
0
        public void TestAdd_A_2()
        {
            SimpleTreeNode <Int32> root   = new SimpleTreeNode <int>(1, null);
            SimpleTreeNode <Int32> node1  = new SimpleTreeNode <int>(11, root);
            SimpleTreeNode <Int32> node2  = new SimpleTreeNode <int>(12, root);
            SimpleTreeNode <Int32> node11 = new SimpleTreeNode <int>(111, node1);
            SimpleTreeNode <Int32> node21 = new SimpleTreeNode <int>(121, node2);
            SimpleTreeNode <Int32> node22 = new SimpleTreeNode <int>(122, node2);
            SimpleTreeNode <Int32> node23 = new SimpleTreeNode <int>(123, node2);

            SimpleTree <Int32> tree = new SimpleTree <Int32>(root);

            tree.AddChild(root, node1);
            tree.AddChild(root, node2);

            Assert.AreEqual(1, tree.Root.NodeValue);

            int value = 10;

            foreach (SimpleTreeNode <int> node in tree.Root.Children)
            {
                value++;
                Assert.AreEqual(value, node.NodeValue);
                Assert.AreEqual(true, node.Parent != null);
            }

            tree.AddChild(node1, node11);

            value = 100;
            foreach (SimpleTreeNode <int> node in tree.Root.Children)
            {
                value += 10;
                if (node.Children != null)
                {
                    foreach (SimpleTreeNode <int> childNode in node.Children)
                    {
                        value++;
                        Assert.AreEqual(value, childNode.NodeValue);
                        Assert.AreEqual(true, childNode.Parent != null);
                    }
                    value -= node.Children.Count;
                }
                Assert.AreEqual(true, node.Parent != null);
            }

            tree.AddChild(node2, node21);
            tree.AddChild(node2, node22);
            tree.AddChild(node2, node23);

            value = 100;
            foreach (SimpleTreeNode <int> node in tree.Root.Children)
            {
                if (root.Children != null)
                {
                    value += 10;
                    foreach (SimpleTreeNode <int> childNode in node.Children)
                    {
                        value++;
                        Assert.AreEqual(value, childNode.NodeValue);
                    }
                    value -= node.Children.Count;
                }
            }
        }
Exemple #28
0
    // creates a tree of Slots
    // the slots don't change so this only needs to run once
    // to update, traverse to check if there is item under the slot
    private void createTree()
    {
        // building up the tree
        Node tempRef = new Node(NodeOptions.RootNode);

        treeRoot = new SimpleTree(tempRef);

        tempRef = new Node(layer1[0].gameObject, lineObjectRoot.Find("11").gameObject, NodeOptions.EndNode);
        treeRoot.AddChild(tempRef);
        tempRef = new Node(layer1[1].gameObject, lineObjectRoot.Find("12").gameObject, NodeOptions.AndNode, true);
        treeRoot.AddChild(tempRef);
        tempRef = new Node(layer1[2].gameObject, lineObjectRoot.Find("13").gameObject, NodeOptions.EndNode);
        treeRoot.AddChild(tempRef);
        tempRef = new Node(layer1[3].gameObject, lineObjectRoot.Find("14").gameObject, NodeOptions.EndNode);
        treeRoot.AddChild(tempRef);
        tempRef = new Node(layer1[4].gameObject, lineObjectRoot.Find("15").gameObject, NodeOptions.AndNode);
        // treeRoot.AddChild(tempRef, treeRoot.GetChild(2));
        treeRoot.AddChild(tempRef);
        tempRef = new Node(layer1[5].gameObject, lineObjectRoot.Find("16").gameObject, NodeOptions.EndNode);
        treeRoot.AddChild(tempRef);
        // Debug.Log(tempRef.getActionText().text);


        SimpleTree tempAndNode = treeRoot.GetChild(2);

        tempRef = new Node(layer2.Find("1").gameObject, lineObjectRoot.Find("21").gameObject, NodeOptions.OrNode);
        tempAndNode.AddChild(tempRef);
        tempRef = new Node(layer2.Find("2").gameObject, lineObjectRoot.Find("22").gameObject, NodeOptions.EndNode);
        tempAndNode.AddChild(tempRef);

        SimpleTree tempOrNode = tempAndNode.GetChild(1);

        tempRef = new Node(layer3[0].Find("1").gameObject, lineObjectRoot.Find("311").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);
        tempRef = new Node(layer3[0].Find("2").gameObject, lineObjectRoot.Find("312").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);

        tempOrNode = tempAndNode.GetChild(2);
        tempRef    = new Node(layer3[1].Find("1").gameObject, lineObjectRoot.Find("321").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);
        tempRef = new Node(layer3[1].Find("2").gameObject, lineObjectRoot.Find("322").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);

        tempAndNode = treeRoot.GetChild(5);

        tempRef = new Node(layer2.Find("1").gameObject, lineObjectRoot.Find("21").gameObject, NodeOptions.OrNode);
        tempAndNode.AddChild(tempRef);
        tempRef = new Node(layer2.Find("2").gameObject, lineObjectRoot.Find("22").gameObject, NodeOptions.EndNode);
        tempAndNode.AddChild(tempRef);

        tempOrNode = tempAndNode.GetChild(1);
        tempRef    = new Node(layer3[0].Find("1").gameObject, lineObjectRoot.Find("311").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);
        tempRef = new Node(layer3[0].Find("2").gameObject, lineObjectRoot.Find("312").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);

        tempOrNode = tempAndNode.GetChild(2);
        tempRef    = new Node(layer3[1].Find("1").gameObject, lineObjectRoot.Find("321").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);
        tempRef = new Node(layer3[1].Find("2").gameObject, lineObjectRoot.Find("322").gameObject, NodeOptions.EndNode);
        tempOrNode.AddChild(tempRef);
    }
Exemple #29
0
        public void TestInterpretAndTranslateFunction()
        {
            var input   = "7+3*5-2";
            var result  = AST.Parse(input);
            var ethalon = new List <ANode>
            {
                new ANode {
                    token_type = TokenType.Bracket, token_value = "("
                },
                new ANode {
                    token_type = TokenType.Integer, token_value = "7"
                },
                new ANode {
                    token_type = TokenType.Operation, token_value = "+"
                },
                new ANode {
                    token_type = TokenType.Bracket, token_value = "("
                },
                new ANode {
                    token_type = TokenType.Bracket, token_value = "("
                },
                new ANode {
                    token_type = TokenType.Integer, token_value = "3"
                },
                new ANode {
                    token_type = TokenType.Operation, token_value = "*"
                },
                new ANode {
                    token_type = TokenType.Integer, token_value = "5"
                },
                new ANode {
                    token_type = TokenType.Bracket, token_value = ")"
                },
                new ANode {
                    token_type = TokenType.Operation, token_value = "-"
                },
                new ANode {
                    token_type = TokenType.Integer, token_value = "2"
                },
                new ANode {
                    token_type = TokenType.Bracket, token_value = ")"
                },
                new ANode {
                    token_type = TokenType.Bracket, token_value = ")"
                }
            };

            Assert.AreEqual(ethalon.Count, result.Count);
            for (var i = 0; i < result.Count; i++)
            {
                Assert.AreEqual(ethalon[i].token_type, result[i].token_type);
                Assert.AreEqual(ethalon[i].token_value, result[i].token_value);
            }

            var actualTree = AST.Build(result);

            var root = new SimpleTreeNode <ANode>(new ANode {
                token_type = TokenType.Operation, token_value = "+"
            }, null);
            var ethalonTree   = new SimpleTree <ANode>(root);
            var leftRootChild = new SimpleTreeNode <ANode>(new ANode {
                token_type = TokenType.Integer, token_value = "7"
            }, root);

            ethalonTree.AddChild(root, leftRootChild);
            var rightRootChild = new SimpleTreeNode <ANode>(new ANode {
                token_type = TokenType.Operation, token_value = "-"
            }, root);

            ethalonTree.AddChild(root, rightRootChild);
            var leftMultiplyOperation = new SimpleTreeNode <ANode>(new ANode {
                token_type = TokenType.Operation, token_value = "*"
            }, rightRootChild);

            ethalonTree.AddChild(rightRootChild, leftMultiplyOperation);
            var right2Value = new SimpleTreeNode <ANode>(new ANode {
                token_type = TokenType.Integer, token_value = "2"
            }, rightRootChild);

            ethalonTree.AddChild(rightRootChild, right2Value);
            var left3Value = new SimpleTreeNode <ANode>(new ANode {
                token_type = TokenType.Integer, token_value = "3"
            }, leftMultiplyOperation);

            ethalonTree.AddChild(leftMultiplyOperation, left3Value);
            var right5Value = new SimpleTreeNode <ANode>(new ANode {
                token_type = TokenType.Integer, token_value = "5"
            }, leftMultiplyOperation);

            ethalonTree.AddChild(leftMultiplyOperation, right5Value);

            var actualTreeList  = actualTree.GetAllNodes();
            var ethalonTreeList = ethalonTree.GetAllNodes();

            Assert.AreEqual(ethalonTreeList.Count, actualTreeList.Count);
            for (var i = 0; i < actualTreeList.Count; i++)
            {
                Assert.AreEqual(ethalonTreeList[i].NodeValue.token_type, actualTreeList[i].NodeValue.token_type);
                Assert.AreEqual(ethalonTreeList[i].NodeValue.token_value, actualTreeList[i].NodeValue.token_value);
                if (ethalonTreeList[i].Children == null)
                {
                    Assert.IsNull(actualTreeList[i].Children);
                }
                else
                {
                    Assert.AreEqual(ethalonTreeList[i].Children.Count, actualTreeList[i].Children.Count);
                    Assert.AreEqual(ethalonTreeList[i].Children.Count, 2);
                    Assert.AreEqual(actualTreeList[i].Children.Count, 2);
                }
            }

            var resultNode = AST.InterpretAndTranslate(actualTree);

            Assert.AreEqual(TokenType.Integer, resultNode.token_type);
            Assert.AreEqual("20", resultNode.token_value);
            Assert.AreEqual("(7+((3*5)-2))", resultNode.translated_result);
        }