def test_is_internal(self):
     # Create node with no children
     node = BinaryNode(2)
     assert node.is_internal() is False
     # Attach left child node
     node.left = BinaryNode(1)
     assert node.is_internal() is True
     # Attach right child node
     node.right = BinaryNode(3)
     assert node.is_internal() is True
     # Detach left child node
     node.left = None
     assert node.is_internal() is True
     # Detach right child node
     node.right = None
     assert node.is_internal() is False
 def test_init(self):
     data = 123
     node = BinaryNode(data)
     assert node.data is data
     assert node.left is None
     assert node.right is None
Example #3
0
    def test_height(self):

        # rightmost leaf
        node = BinaryNode(0)
        assert node.is_leaf() is True
        assert node.height() == 0
        node.left = BinaryNode(1)
        assert node.height() == 1
        node.left.left = BinaryNode(2)
        assert node.height() == 2
        node.left.left.left = BinaryNode(3)
        assert node.height() == 3

        # rightmost leaf
        node = BinaryNode(0)
        node.left = None
        node.right = None
        assert node.is_leaf() is True
        assert node.height() == 0
        node.right = BinaryNode(1)
        assert node.height() == 1
        node.right.right = BinaryNode(2)
        assert node.height() == 2
        node.right.right.right = BinaryNode(3)
        assert node.height() == 3