Example #1
0
        public void BinaryTreeConstructorTest()
        {
            BinaryTreeNode <double> node0 = new BinaryTreeNode <double>(20.0);
            BinaryTreeNode <double> node1 = new BinaryTreeNode <double>(20.1);
            BinaryTreeNode <double> node2 = new BinaryTreeNode <double>(20.2);

            Assert.AreEqual(node1.CompareTo(node2), -1);
            Assert.AreEqual(node1.CompareTo(node2.Data), -1);
            Assert.AreEqual(node2.CompareTo(node2.Data), 0);
        }
Example #2
0
        public BinaryTreeNode FindWithParents(int value, out BinaryTreeNode parent)
        {
            BinaryTreeNode current = Root;

            parent = null;

            while (current != null)
            {
                int result = current.CompareTo(value);

                if (result > 0)
                {
                    parent  = current;
                    current = current.Left;
                }
                else if (result < 0)
                {
                    parent  = current;
                    current = current.Right;
                }
                else
                {
                    break;
                }
            }

            return(current);
        }
Example #3
0
        private void AddToTree(BinaryTreeNode head, BinaryTreeNode newNode)
        {
            if (newNode.CompareTo(head.Value) < 0)
            {
                Console.WriteLine("menor");

                if (head.Left == null)
                {
                    newNode.Father = head;
                    head.Left      = newNode;
                }
                else
                {
                    AddToTree(head.Left, newNode);
                }
            }
            else
            {
                if (head.Right == null)
                {
                    newNode.Father = head;
                    head.Right     = newNode;
                }
                else
                {
                    AddToTree(head.Right, newNode);
                }
            }
        }
Example #4
0
            private BinaryTreeNode <T> FindWithParent(T value, out BinaryTreeNode <T> parent)
            {
                //
                BinaryTreeNode <T> current = _head;

                parent = null;

                //
                while (current != null)
                {
                    int result = current.CompareTo(value);

                    if (result > 0)
                    {
                        //
                        parent  = current;
                        current = current.Left;
                    }
                    else if (result < 0)
                    {
                        //
                        parent  = current;
                        current = current.Right;
                    }
                    else
                    {
                        //
                        break;
                    }
                }

                return(current);
            }
Example #5
0
        public void CompareToTest_UsingAnotherComparatorWithMoreValue_Returned1()
        {
            BinaryTreeNode <double> node = new BinaryTreeNode <double>(5.12);

            int actual = node.CompareTo(6.21, (x, y) => x.CompareTo(y));

            Assert.AreEqual(1, actual);
        }
Example #6
0
        public void CompareToTest_UsingDefaultComparatorWithMoreValue_Returned1()
        {
            BinaryTreeNode <int> node = new BinaryTreeNode <int>(5);

            int actual = node.CompareTo(6, null);

            Assert.AreEqual(1, actual);
        }
Example #7
0
        public void Compare_Should_Return_Minus_One()
        {
            int expectedValue              = -1;
            BinaryTreeNode <int> node      = new BinaryTreeNode <int>(3);
            BinaryTreeNode <int> otherNode = new BinaryTreeNode <int>(5);

            Assert.AreEqual(node.CompareTo(otherNode.Value), expectedValue);
        }
Example #8
0
        public void CompareTo_ComparesTwoBinaryTreeNodes()
        {
            var binaryNode1 = new BinaryTreeNode <int>(5);
            var binaryNode2 = new BinaryTreeNode <int>(5);

            var result = binaryNode1.CompareTo(binaryNode2);

            result.Should().Be(0);
        }
Example #9
0
        public void CompareTo_ComparesBinaryTreeNodeWithGenericNode()
        {
            var binaryNode  = new BinaryTreeNode <int>(5);
            var genericNode = new Node <int>(5);

            var result = binaryNode.CompareTo(genericNode);

            result.Should().Be(0);
        }
Example #10
0
        public void CompareToTest_UsingAnotherComparatorWithMoreValue_ShouldThrowComparatorHasNotBeenFoundException()
        {
            BinaryTreeNode <MyClass> node = new BinaryTreeNode <MyClass>(null);

            node.CompareTo(new MyClass(), null);
        }