Exemplo n.º 1
0
        public void InRootNode_outNewTree()
        {
            //arrange
            Tree.BinaryTree tree = new Tree.BinaryTree();
            tree.AddItem(5); //!
            tree.AddItem(3); //L
            tree.AddItem(7); //R
            tree.AddItem(4); //L-R
            tree.AddItem(2); //L-L
            tree.AddItem(6); //R-L
            tree.AddItem(8); //R-R
            tree.RemoveItem(5);

            int expectedRoot  = 6;
            int expectedLeft  = 3;
            int expectedRight = 7;

            //act
            int actualRoot  = tree.GetRoot().Value;
            int actualLeft  = tree.GetRoot().Left.Value;
            int actualRight = tree.GetRoot().Right.Value;

            //assert
            Assert.AreEqual(expectedRoot, actualRoot);
            Assert.AreEqual(expectedLeft, actualLeft);
            Assert.AreEqual(expectedRight, actualRight);
        }
Exemplo n.º 2
0
 public void EmpryTree_outException()
 {
     //arrange
     Tree.BinaryTree binaryTree = new Tree.BinaryTree();
     //act
     //assert
     Assert.Throws <ArgumentNullException>(() => binaryTree.RemoveItem(10));
 }
Exemplo n.º 3
0
        public bool ContainsMethod_CreateTreeAndCheckForContainsElement(int[] array, int value)
        {
            Tree.BinaryTree <int> binaryTree = new Tree.BinaryTree <int>();
            foreach (var element in array)
            {
                binaryTree.Add(element);
            }

            return(binaryTree.Conatins(value));
        }
Exemplo n.º 4
0
        public int CountMethod_CreateTreeAndGetCountElements(int[] array)
        {
            Tree.BinaryTree <int> binaryTree = new Tree.BinaryTree <int>();
            foreach (var element in array)
            {
                binaryTree.Add(element);
            }

            return(binaryTree.Count);
        }
        public void In3Items_outException()
        {
            //arrange
            Tree.BinaryTree tree = new Tree.BinaryTree();
            tree.AddItem(2);
            tree.AddItem(8);
            tree.AddItem(3);

            //assert
            Assert.Throws <ArgumentOutOfRangeException>(() => { TreeNode node = tree.GetNodeByValue(4); });
        }
Exemplo n.º 6
0
        public void DelRootNode_outException()
        {
            //arrange
            Tree.BinaryTree tree = new Tree.BinaryTree();
            tree.AddItem(3);
            tree.RemoveItem(3);

            //act

            //assert
            Assert.Throws <ArgumentNullException>(() => tree.GetRoot());
        }
Exemplo n.º 7
0
        public void printBFSOfTree()
        {
            Tree.BinaryTree tree = new Tree.BinaryTree
            {
                root = new Node(1)
            };
            tree.root.left       = new Node(2);
            tree.root.right      = new Node(3);
            tree.root.left.left  = new Node(4);
            tree.root.left.right = new Node(5);

            Console.WriteLine("Level order traversal " + "of binary tree is ");
            printLevelOrderQueue(tree);
        }
Exemplo n.º 8
0
        public int[] PostoredBypassMethod_CreateTreeAndGetItPostoredBypass(int[] array)
        {
            Tree.BinaryTree <int> binaryTree = new Tree.BinaryTree <int>(array);

            int[] result = new int[array.Length];
            int   i      = 0;

            foreach (var value in binaryTree.PostorderBypass())
            {
                result[i++] = value;
            }

            return(result);
        }
        public void In2Items_outValue4()
        {
            //arrange
            int expected = 4;

            Tree.BinaryTree tree = new Tree.BinaryTree();
            tree.AddItem(2);
            tree.AddItem(4);

            TreeNode node = tree.GetNodeByValue(4);

            //act
            int actual = node.Value;

            //assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 10
0
        public int[] InoredBypassMethod_CreateTreeAndGetItInoredBypass(int[] array)
        {
            Tree.BinaryTree <int> binaryTree = new Tree.BinaryTree <int>();
            foreach (var element in array)
            {
                binaryTree.Add(element);
            }

            int[] result = new int[array.Length];
            int   i      = 0;

            foreach (var value in binaryTree.InorderBypass())
            {
                result[i++] = value;
            }

            return(result);
        }
Exemplo n.º 11
0
        public int[] GetEnumeratorMethods_CreateTreeAndGetItEnumerator(int[] array)
        {
            Tree.BinaryTree <int> binaryTree = new Tree.BinaryTree <int>();
            foreach (var element in array)
            {
                binaryTree.Add(element);
            }

            int[] result = new int[array.Length];
            int   i      = 0;

            foreach (var value in binaryTree)
            {
                result[i++] = value;
            }

            return(result);
        }
Exemplo n.º 12
0
        public void In3Node_outRoot3()
        {
            //arrange
            int expected = 3;

            Tree.BinaryTree tree = new Tree.BinaryTree();
            tree.AddItem(3);
            tree.AddItem(2);
            tree.AddItem(4);

            //act
            int actualRight = tree.GetRoot().Right.Parent.Value;
            int actualLeft  = tree.GetRoot().Left.Parent.Value;

            //assert
            Assert.AreEqual(expected, actualRight);
            Assert.AreEqual(expected, actualLeft);
        }
Exemplo n.º 13
0
        public void InRootNode_outRootAndLeftNode()
        {
            //arrange
            Tree.BinaryTree tree = new Tree.BinaryTree();
            tree.AddItem(3);
            tree.AddItem(5);
            tree.AddItem(2);
            tree.RemoveItem(3);

            int expectedRoot = 5;
            int expectedLeft = 2;

            //act
            int actualRoot = tree.GetRoot().Value;
            int actualLeft = tree.GetRoot().Left.Value;

            //assert
            Assert.AreEqual(expectedRoot, actualRoot);
            Assert.AreEqual(expectedLeft, actualLeft);
        }
Exemplo n.º 14
0
        public void InoredBypassMethod_CreateTreeWithStringAndGetItInoredBypass()
        {
            string[] elements       = new string[] { "fffff", "kkkkkkkkk", "aaaa", "c" };
            string[] expectedResult = new string[] { "c", "aaaa", "fffff", "kkkkkkkkk" };

            Tree.BinaryTree <string> binaryTree = new Tree.BinaryTree <string>((left, right) => left.Length.CompareTo(right.Length));
            foreach (var element in elements)
            {
                binaryTree.Add(element);
            }

            string[] result = new string[elements.Length];
            int      i      = 0;

            foreach (var value in binaryTree.InorderBypass())
            {
                result[i++] = value;
            }

            Assert.AreEqual(expectedResult, result);
        }
Exemplo n.º 15
0
        public void In6Node_outRootLeft3()
        {
            //arrange
            int expected = 3;

            Tree.BinaryTree tree = new Tree.BinaryTree();
            tree.AddItem(5);
            tree.AddItem(3);//L
            tree.AddItem(4);
            tree.AddItem(2);
            tree.AddItem(8);//R
            tree.AddItem(6);
            tree.AddItem(7);

            TreeNode leftNode = tree.GetRoot().Left;
            //act
            int actualRight = leftNode.Right.Parent.Value;
            int actualLeft  = leftNode.Left.Parent.Value;

            //assert
            Assert.AreEqual(expected, actualRight);
            Assert.AreEqual(expected, actualLeft);
        }