Ejemplo n.º 1
0
        public void Find_ItemFound_ReturnsFoundItem()
        {
            var myOrganizationTree = new NaryTree <string>();
            var added = false;
            NaryTreeNode <string> foundNode = null;

            //root of the tree should be null
            Assert.IsTrue(myOrganizationTree.IsEmpty());

            //create a children list
            var Children = new List <NaryTreeNode <string> >()
            {
                new NaryTreeNode <string>("Kim"),
                new NaryTreeNode <string>("Kate"),
                new NaryTreeNode <string>("Mikesh")
            };

            //add root node Jimmy with children
            added = myOrganizationTree.InsertTopLevelWithChildren("Jimmy", Children);

            //should return true as it was added
            Assert.IsTrue(added);

            //Find Mikesh node it's children list should be 0 before inserting Timmy
            foundNode = myOrganizationTree.Find("Mikesh");

            Assert.IsNotNull(foundNode);

            if (foundNode != null)
            {
                Assert.IsTrue(foundNode.Data == "Mikesh");
                Assert.IsTrue(foundNode.ChildrenList.Count == 0);
            }
        }
Ejemplo n.º 2
0
        public void Remove_ItemNotRoot_ReturnsTrue()
        {
            var myOrganizationTree = new NaryTree <string>();
            var added = false;
            NaryTreeNode <string> foundNode = null;

            //root of the tree should be null
            Assert.IsTrue(myOrganizationTree.IsEmpty());

            //create a children list
            var Children = new List <NaryTreeNode <string> >()
            {
                new NaryTreeNode <string>("Kim"),
                new NaryTreeNode <string>("Kate"),
                new NaryTreeNode <string>("Mikesh")
            };

            //add some children to Kate
            Children[1].ChildrenList.Add(new NaryTreeNode <string>("Jack"));
            Children[1].ChildrenList.Add(new NaryTreeNode <string>("Peter"));
            Children[1].ChildrenList[1].ChildrenList.Add(new NaryTreeNode <string>("Mike"));


            //add root node Jimmy with children
            added = myOrganizationTree.InsertTopLevelWithChildren("Jimmy", Children);

            //should return true as it was added
            Assert.IsTrue(added);

            //Find Kate
            foundNode = myOrganizationTree.Find("Kate");

            Assert.IsNotNull(foundNode);

            if (foundNode != null)
            {
                Assert.IsTrue(foundNode.Data == "Kate");
                Assert.IsTrue(foundNode.ChildrenList.Count == 2);
                Assert.IsTrue(foundNode.ChildrenList[0].Data == "Jack");
                Assert.IsTrue(foundNode.ChildrenList[1].Data == "Peter");
            }

            //Remove Kate
            var removed = myOrganizationTree.Remove("Kate");

            Assert.IsTrue(removed);

            foundNode = myOrganizationTree.Find("Kate");

            //Kate should not be found
            Assert.IsNull(foundNode);
        }
Ejemplo n.º 3
0
        public void InsertTopLevelWithChildren_NonEmptyTree_ReturnsTrue()
        {
            var myOrganizationTree = new NaryTree <string>("Jimmy");
            var added = false;
            NaryTreeNode <string> foundNode = null;

            //root of the tree should be not null
            Assert.IsFalse(myOrganizationTree.IsEmpty());

            //find the root which is Jimmy and should have no children
            foundNode = myOrganizationTree.Find("Jimmy");

            Assert.IsNotNull(foundNode);

            if (foundNode != null)
            {
                Assert.IsTrue(foundNode.Data == "Jimmy");
                Assert.IsTrue(foundNode.ChildrenList.Count == 0);
            }


            //create a children list
            var Children = new List <NaryTreeNode <string> >()
            {
                new NaryTreeNode <string>("Kim"),
                new NaryTreeNode <string>("Kate"),
                new NaryTreeNode <string>("Mikesh")
            };



            //add root node Jimmy with children
            added = myOrganizationTree.InsertTopLevelWithChildren("Timmy", Children);

            //should return true as it was added
            Assert.IsTrue(added);

            foundNode = myOrganizationTree.Find("Timmy");

            //should find it
            Assert.IsNotNull(foundNode);

            if (foundNode != null)
            {
                Assert.IsTrue(foundNode.Data == "Timmy");
                Assert.IsTrue(foundNode.ChildrenList.Count == 3);
                Assert.IsTrue(foundNode.ChildrenList[0].Data == "Kim");
                Assert.IsTrue(foundNode.ChildrenList[1].Data == "Kate");
                Assert.IsTrue(foundNode.ChildrenList[2].Data == "Mikesh");
            }
        }
Ejemplo n.º 4
0
        public void Remove_Root_ReturnsTrue()
        {
            var myOrganizationTree = new NaryTree <string>();
            var added = false;
            NaryTreeNode <string> foundNode = null;

            //root of the tree should be null
            Assert.IsTrue(myOrganizationTree.IsEmpty());

            //create a children list
            var Children = new List <NaryTreeNode <string> >()
            {
                new NaryTreeNode <string>("Kim"),
                new NaryTreeNode <string>("Kate"),
                new NaryTreeNode <string>("Mikesh")
            };

            //add root node Jimmy with children
            added = myOrganizationTree.InsertTopLevelWithChildren("Jimmy", Children);

            //should return true as it was added
            Assert.IsTrue(added);

            //Find Root Jimmy
            foundNode = myOrganizationTree.Find("Jimmy");

            Assert.IsNotNull(foundNode);

            if (foundNode != null)
            {
                Assert.IsTrue(foundNode.Data == "Jimmy");
                Assert.IsTrue(foundNode.ChildrenList.Count == 3);
                Assert.IsTrue(foundNode.ChildrenList[0].Data == "Kim");
                Assert.IsTrue(foundNode.ChildrenList[1].Data == "Kate");
                Assert.IsTrue(foundNode.ChildrenList[2].Data == "Mikesh");
            }

            //Remove the root
            myOrganizationTree.Remove("Jimmy");

            //root of the tree should be null after removing root
            Assert.IsTrue(myOrganizationTree.IsEmpty());
        }