def test_insert_one_node(self):
        #Arrange
        bst = BinarySearchTree()

        #Act
        inserted_position = bst.insertNode(1)

        #Assert
        self.assertEqual(bst.root.value, 1)
        self.assertEqual(inserted_position, 1)
    def test_delete_non_existant_value(self):
        #Arrange
        bst = BinarySearchTree()
        bst.insertNode(1)

        #Act
        deleted_position = bst.deleteNode(2)

        #Assert
        self.assertEqual(bst.root.value, 1)
        self.assertEqual(deleted_position, -1)
    def test_delete_one_node(self):
        #Arrange
        bst = BinarySearchTree()
        bst.insertNode(1)

        #Act
        deleted_position = bst.deleteNode(1)

        #Assert
        self.assertIsNone(bst.root)
        self.assertEqual(deleted_position, 1)
    def test_insert_one_right_node(self):
        #Arrange
        bst = BinarySearchTree()
        bst.insertNode(2)

        #Act
        inserted_position = bst.insertNode(3)

        #Assert
        self.assertEqual(bst.root.right.value, 3)
        self.assertEqual(inserted_position, 3)
    def test_delete_root(self):
        #Arrange
        bst = BinarySearchTree()
        bst.insertNode(2)
        bst.insertNode(3)

        #Act
        deleted_position = bst.deleteNode(2)

        #Assert
        self.assertEqual(bst.root.value, 3)
        self.assertEqual(deleted_position, 1)
示例#6
0
def main(args=None):
    bst = BinarySearchTree()
    try:
        for _ in range(int(input())):
            input_line = input().split()

            command = str(input_line[0])
            value = int(input_line[1])

            if (command == 'i'):
                position = bst.insertNode(value)
            elif (command == 'd'):
                position = bst.deleteNode(value)
            if position >= 1:
                print(position)
    except EOFError:
        pass
 def test_empty_binarysearchtree(self):
     bst = BinarySearchTree()
     self.assertIsNone(bst.root)