Exemple #1
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);
        }
Exemple #2
0
        public void Remove_EmptyTree_ReturnsTrue()
        {
            var myTree = new NaryTree <int>();

            //tree should be empty
            Assert.IsTrue(myTree.IsEmpty());

            var removed = myTree.Remove(10);

            Assert.IsTrue(removed);

            // tree should still be empty
            Assert.IsTrue(myTree.IsEmpty());
        }
Exemple #3
0
        public void Remove_ItemNotFound_ReturnsFalse()
        {
            var myLetters = new NaryTree <char>('b');
            var found     = myLetters.Remove('x');

            Assert.IsFalse(found);

            //find the existing item to ensure that it was not delete
            var foundItem = myLetters.Find('b');

            Assert.IsNotNull(foundItem);

            if (foundItem != null)
            {
                Assert.IsTrue(foundItem.Data == 'b');
                Assert.IsTrue(foundItem.ChildrenList.Count == 0);
            }
        }
Exemple #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());
        }