Exemplo n.º 1
0
        static void Main(string[] args)
        {
            MyTree<int> testTree = new MyTree<int>();
            testTree.AddChildOn(2, 4);
            testTree.AddChildOn(3, 2);
            testTree.AddChildOn(5, 0);
            testTree.AddChildOn(3, 5);
            testTree.AddChildOn(5, 6);
            testTree.AddChildOn(5, 1);
            testTree.AddChildOn(1, 7);

            //            testTree.AddChildOn(2, 5); // try to move part of tree - and IT'S WORK!

            if (!testTree.CheckTreeConsistency())
            {
                Console.WriteLine("Tree is not solid! Check connections again!");
                Environment.Exit(0);
            }

            // a) Find root node
            Console.WriteLine("Root element in tree is: {0}", testTree.root);

            //b) return all leafs
            Console.WriteLine("List of all leafs: {0}", String.Join(", ", testTree.leafs));

            //c) return all middles
            Console.WriteLine("List of all middles: {0}", String.Join(", ", testTree.middles));

            //d) return the longest path
            Console.WriteLine("Longest path: {0}", String.Join(", ", testTree.FindLongestPath));
        }
Exemplo n.º 2
0
        public void Insert_InsertFirstElement_TreeContainsValue()
        {
            //arrenge
            var tree = new MyTree <int>();

            //act
            tree.Insert(5);
            var result = tree.Find(5);

            //assert
            Assert.NotNull(result);
        }
Exemplo n.º 3
0
        public void GivenATree_WhenHaveSomeItems_ShouldReturnTheHeight()
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 10, 30, 6, 21, 4, 2, 8 });

            // Act
            var height = tree.GetHeight();

            // Assert
            height.Should().Be(4);
        }
        public void GivenATree_WhenIsBalanced_ShouldReturnTrue()
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 19, 21, 6, 3, 8, 4 });

            // Act
            var isBST = tree.IsBinarySearchTree();

            // Assert
            isBST.Should().BeTrue();
        }
Exemplo n.º 5
0
        protected void LoadDirectory()
        {
            MyTree.Nodes.Clear();
            MyTree.Dispose();
            filePath = System.Web.Configuration.WebConfigurationManager.AppSettings["EmpFilePath"];
            //filePath = "D:\\BASESOFT\\MSBHR\\Documents\\PolicyFiles\\";
            System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath(filePath));
            //System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(filePath);
            TreeNode RootNode = OutputDirectory(RootDir, null);

            MyTree.Nodes.Add(RootNode);
            MyTree.ExpandDepth = 1;
        }
Exemplo n.º 6
0
        public void Sort_ShoulReturnSortedList(int[] inputValues)
        {
            var output = new List <int>();

            foreach (var value in inputValues)
            {
                myTree.Insert(value);
            }

            MyTree.Sort(myTree.Root, output);

            output.Should().BeInAscendingOrder();
        }
Exemplo n.º 7
0
        public void GivenATree_WhenNodeHaveValueOnHisTree_ShouldReturnValueOfThisNode()
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 11, 30 });

            // Act
            var value = tree.GetAncestor(11);

            // Assert
            value.Should().Be(20);
        }
Exemplo n.º 8
0
        public void GivenATree_WhenHaveItemsThatAreSiblings_ShouldReturnTrue()
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 11, 30 });

            // Act
            var areSibling = tree.AreSibling(11, 30);

            // Assert
            areSibling.Should().BeTrue();
        }
Exemplo n.º 9
0
        public void AreEquivalent()
        {
            MyTree treeTest = new MyTree();

            treeTest.AddItem(8);
            treeTest.AddItem(25);
            treeTest.AddItem(4);
            treeTest.AddItem(15);

            var arrayTreeTest = TreeHelper.GetTreeInLine(tree);

            CollectionAssert.AreEquivalent(arrayTree, arrayTreeTest);
        }
Exemplo n.º 10
0
        public void GivenATree_WhenHaveSomeItems_ShouldReturnListOfIntInTheDistanceLevel(int distance, int[] expectedValues)
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 10, 30, 6, 14, 24, 3, 8, 26 });

            // Act
            var values = tree.GetValuesAtDistance(distance);

            // Assert
            values.Should().BeEquivalentTo(expectedValues);
        }
Exemplo n.º 11
0
        public void Costructor_CreateTreeWithIntRoot3_RootMustBeInt3()
        {
            //arrange
            int          expected = 3;
            MyTree <int> tree;
            int          actual;

            //act
            tree   = new MyTree <int>(expected);
            actual = tree.RootNode.Value;
            //assert
            Assert.Equal(expected, actual);
        }
Exemplo n.º 12
0
        public void Insert_InsertedObjectCallEvent_ReturnValueOfObjectThatCallEvent()
        {
            //arrenge
            var tree   = new MyTree <string>();
            var actual = new MyTreeEventArgs <string>("");

            //act
            tree.OnInsert += (o, s) => { actual = s; };
            tree.Insert("");

            //assert
            Assert.Equal("", actual.Element.ToString());
        }
Exemplo n.º 13
0
        public void AddTest3()
        {
            // Arrange
            Node   node = new Node();
            MyTree tree = new MyTree(node);

            // Act

            tree.AddNode(node, "Y");

            // Assert
            Assert.Equal("Y", tree.BreadthFirst(node));
        }
Exemplo n.º 14
0
        public void GivenATree_WhenHaveSomeItems_ShouldReturnMaxValue()
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 10, 30, 6, 14, 24, 3, 8, 26 });

            // Act
            var max = tree.GetMaxValue();

            // Assert
            max.Should().Be(30);
        }
Exemplo n.º 15
0
        public void GivenATree_WhenHaveSomeItems_ShouldReturnCountOfItems()
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 10, 30, 6, 14, 24, 3, 8, 26 });

            // Act
            var count = tree.Count();

            // Assert
            count.Should().Be(9);
        }
Exemplo n.º 16
0
        public void GivenATree_WhenDoNotHaveItemsThatAreSibling_ShouldReturnFalse()
        {
            // Arrange
            var tree = new MyTree();

            tree.Insert(new int[] { 20, 11, 30 });

            // Act
            var areSibling = tree.AreSibling(20, 11);

            // Assert
            areSibling.Should().BeFalse();
        }
Exemplo n.º 17
0
        protected void BindNode()
        {
            DataTable dt = sfNodeBll.SelBy(SiteID);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["ParentID"] = 0;
            }
            MyTree.liAllTlp     = "<a href='List.aspx?SiteID=" + SiteID + "'>全部内容</a>";
            MyTree.LiContentTlp = "<a href='List.aspx?NodeID=@NodeID&SiteID=" + SiteID + "'>@NodeName</a>";
            MyTree.SelectedNode = NodeID;//选中节点
            MyTree.DataSource   = dt;
            MyTree.DataBind();
        }
Exemplo n.º 18
0
        public void AddNode_AddNodeInt3LessThenRoot4_CreateLeftChildInt3ToRootNodeInt4()
        {
            //arrange
            MyTree <int> tree;
            int          expected = 3;
            int          actual;

            //act
            tree = new MyTree <int>(4);
            tree.AddNode(expected);
            actual = tree.RootNode.LeftNode.Value;
            //assert
            Assert.Equal(expected, actual);
        }
Exemplo n.º 19
0
        public void GivenATree_WhenHaveSomeItems_ShouldReturnTheMinValue()
        {
            // Arrange
            var tree   = new MyTree();
            var values = GetRandomValues(100);

            tree.Insert(values);

            // Act
            var min = tree.GetMinValue();

            // Assert
            min.Should().Be(values.Min());
        }
Exemplo n.º 20
0
        public void Test1()
        {
            // Arrange
            Node node = new Node();

            // Declare and populate the nodes
            Node node1 = new Node()
            {
                Value = 1.ToString()
            };
            Node node2 = new Node()
            {
                Value = 3.ToString()
            };
            Node node3 = new Node()
            {
                Value = 5.ToString()
            };
            Node node4 = new Node()
            {
                Value = 16.ToString()
            };
            Node node5 = new Node()
            {
                Value = 21.ToString()
            };
            Node node6 = new Node()
            {
                Value = 30.ToString()
            };
            Node node7 = new Node()
            {
                Value = 45.ToString()
            };

            // Instantiate the tree
            MyTree tree = new MyTree(node);

            // Link the tree.
            node1.LeftChild  = node2;
            node1.RightChild = node3;

            node2.LeftChild  = node4;
            node2.RightChild = node5;

            node3.LeftChild  = node6;
            node3.RightChild = node7;

            Assert.Equal(" 16 Fizz 16 Fizz Fizz 1 16 Fizz 16 Fizz Fizz 1 16 Fizz 16 Fizz Fizz 1 FizzBuzz Buzz 16 Fizz 16 Fizz Fizz 1 16 Fizz 16 Fizz Fizz 1 FizzBuzz Buzz FizzBuzz", MyTree.FizzBuzz(node1, ""));
        }
        public void Test1()
        {
            //Arrange
            Node node = new Node();

            // Declare and populate the nodes
            Node node1 = new Node()
            {
                Value = 1
            };
            Node node2 = new Node()
            {
                Value = 2
            };
            Node node3 = new Node()
            {
                Value = 3
            };
            Node node4 = new Node()
            {
                Value = 4
            };
            Node node5 = new Node()
            {
                Value = 5
            };
            Node node6 = new Node()
            {
                Value = 6
            };
            Node node7 = new Node()
            {
                Value = 7
            };

            // Instantiate the tree
            MyTree tree = new MyTree(node);

            // Link the tree.
            node1.LeftChild  = node2;
            node1.RightChild = node3;

            node2.LeftChild  = node4;
            node2.RightChild = node5;

            node3.LeftChild  = node6;
            node3.RightChild = node7;

            Assert.Equal(7, Program.FindMaximumValue(tree, node1));
        }
Exemplo n.º 22
0
        /// <summary>
        /// 元素名称变更
        /// </summary>
        /// <param name="map"></param>
        /// <param name="tree"></param>
        /// <param name="cmd"></param>
        private void ChangeObjectName(MapImage map, MyTree tree, string[] cmd)
        {
            if (cmd.Length != 5)
            {
                return;
            }
            TreeNode node = FindObject(tree.Nodes[0], GetId(cmd[2]), GetId(cmd[3]));

            if (node == null)
            {
                return;
            }
            node.Text = cmd[4];
        }
Exemplo n.º 23
0
        public static void PrintMyTree(MyTree myTree, int topMargin = 0, int leftMargin = 2)
        {
            BNode firstNode = new BNode(0);

            // Console.WriteLine($"Top = {myTree.topIdx}");
            // for (int i = 0; i <= myTree.treeSize; i++)
            // {
            //     Console.WriteLine($"{i,2}) {myTree.table[i, 0],2} {myTree.table[i, 1],2} {myTree.table[i, 2],2}");
            // }

            Add(firstNode, myTree.TopIdx);

            TreePrinter.Print(firstNode, topMargin, leftMargin);

            void Add(BNode node, int index)
            {
                if (myTree.Table[index, 1] != 0)
                {
                    node.Item = myTree.Table[index, 1];
                    // если есть сын
                    if (myTree.Table[index, 0] != 0)
                    {
                        int childIdx = myTree.Table[index, 0];
                        // если сын меньше ключа
                        if (myTree.Table[childIdx, 1] < node.Item)
                        {
                            node.Left = new BNode(0);
                            Add(node.Left, childIdx);
                            int broIdx = myTree.Table[childIdx, 2];
                            // если у сына есть брат
                            if (broIdx != 0)
                            {
                                node.Right = new BNode(0);
                                Add(node.Right, broIdx);
                            }
                        }
                        // если сын больше ключа (значит сын у нас один)
                        else
                        {
                            node.Right = new BNode(0);
                            Add(node.Right, childIdx);
                        }
                    }
                }
                // else
                // {
                //     firstNode = null;
                // }
            }
        }
Exemplo n.º 24
0
        public void Constructor_CreateTreeWithPreDefindeRootAndCreateEmptyTreeAndThenAddRoot_BothRootMustBeEqual()
        {
            //arrange
            MyTree <int> tree1;
            MyTree <int> tree2;
            int          value = 5;

            //act
            tree1 = new MyTree <int>(value);
            tree2 = new MyTree <int>();
            tree2.AddNode(value);
            //assert
            Assert.Equal(tree1.RootNode, tree2.RootNode);
        }
Exemplo n.º 25
0
        /// <summary>
        /// 图层名称变更
        /// </summary>
        /// <param name="map"></param>
        /// <param name="tree"></param>
        /// <param name="cmd"></param>
        private void ChangeLayerName(MapImage map, MyTree tree, string[] cmd)
        {
            if (cmd.Length != 4)
            {
                return;
            }
            TreeNode node = FindLayer(tree.Nodes[0], GetId(cmd[2]));

            if (node == null)
            {
                return;
            }
            node.Text = cmd[3];
        }
Exemplo n.º 26
0
        public void AddNode_AddNodeInt5BigerThenRoot4_CreateRightChildInt5ToRootNodeInt4()
        {
            //arrange
            MyTree <int> tree;
            int          expected = 5;
            int          actual;

            //act
            tree = new MyTree <int>(4);
            tree.AddNode(expected);
            actual = tree.RootNode.RightNode.Value;
            //assert
            Assert.Equal(expected, actual);
        }
Exemplo n.º 27
0
        public void Remove_RemoveRootValue_IfOneChildIsNullChooseAnother()
        {
            //arrenge
            var tree = new MyTree <int>();

            tree.Insert(5);
            tree.Insert(2);
            var remove = 5;

            //act
            tree.Remove(remove);

            //assert
            Assert.Equal(remove.ToString(), tree.Root.Data.ToString());
        }
Exemplo n.º 28
0
        public void RemoveVoid_RemoveNullNode_ArgumentNullException()
        {
            //arrenge
            var tree = new MyTree <string>();

            tree.Insert("one");
            tree.Insert("two");
            tree.Insert("three");

            //act
            Action action = () => tree.Remove(null);

            //assert
            Assert.Throws <ArgumentNullException>(action);
        }
Exemplo n.º 29
0
        public void Find_SearchNullValue_ArgumentNullException()
        {
            //arrenge
            var tree = new MyTree <string>();

            tree.Insert("one");
            tree.Insert("two");
            tree.Insert("three");

            //act
            Action action = () => tree.Find(null);

            //assert
            Assert.Throws <ArgumentNullException>(action);
        }
Exemplo n.º 30
0
        public void GetEnumerator_GetEnumeratorForTree_EnumeratorIsNotNull()
        {
            //arrange
            MyTree <int> tree = new MyTree <int>();

            tree.Insert(10);
            tree.Insert(15);
            tree.Insert(1);

            //act
            IEnumerator result = ((IEnumerable)tree).GetEnumerator();

            //assert
            Assert.NotNull(result);
        }
Exemplo n.º 31
0
        public void GetEnumerator_GetEnumeratorForeach_NotCheckElementsInEmptyTree()
        {
            //arrenge
            var  tree  = new MyTree <int>();
            bool check = true;

            //act
            foreach (var el in tree)
            {
                check = false;
            }

            //assert
            Assert.True(check);
        }
Exemplo n.º 32
0
        private static void TestMyTree()
        {
            var foo = new MyTree<int>();
            foo.Insert(3);
            foo.Insert(1);
            foo.Insert(5);
            Console.WriteLine("PreOrder");
            foreach(var item in foo.PreOrder())
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine("----------");

            Console.WriteLine("PostOrder");
            foreach (var item in foo.PostOrder())
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine("----------");

            Console.WriteLine("InOrder");
            foreach (var item in foo.InOrder())
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine("----------");
        }