def test_delete_twice_leaf_left(self):
        #Arrange
        bst = BinarySearchTree()
        bst.insertNode(3)
        bst.insertNode(1)
        bst.deleteNode(1)

        #Act
        deleted_position = bst.deleteNode(3)

        #Assert
        self.assertIsNone(bst.root)
        self.assertEqual(deleted_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_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)
    def test_delete_one_right_node(self):
        #Arrange
        bst = BinarySearchTree()
        bst.insertNode(2)
        bst.insertNode(3)

        #Act
        deleted_position = bst.deleteNode(3)

        #Assert
        self.assertIsNone(bst.root.right)
        self.assertEqual(deleted_position, 3)
示例#5
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