def test_size(self): tree = Bst() self.assertEquals(tree.size, 0) self.assertEquals(tree.size, tree.getsize()) # Set tree size to arbitrary number ARBITRARY_SIZE = 5 tree.size = ARBITRARY_SIZE self.assertEquals(tree.size, ARBITRARY_SIZE) self.assertEquals(tree.size, tree.getsize()) # Adding should increment size tree = balanced_tree() self.assertEquals(tree.getsize(), 7) FAR_RIGHT_DATA = 100 tree.add(FAR_RIGHT_DATA) self.assertEquals(tree.getsize(), 8) # Removing should decrement size tree = balanced_tree() self.assertEquals(tree.getsize(), 7) tree.remove(ROOT_DATA) self.assertEquals(tree.getsize(), 6)
def remove_one_child_root(): """Removing root in a tree where the root has only one child""" tree = Bst() tree.add(ROOT_DATA) tree.add(LEFT_DATA) self.assertFalse(tree.isempty()) self.assertEquals(tree.getsize(), 2) self.assertEquals(tree.root.getleft().getdata(), LEFT_DATA) val = tree.remove(ROOT_DATA) self.assertFalse(tree.isempty()) self.assertEquals(tree.getsize(), 1) self.assertEquals(val, ROOT_DATA) self.assertIsNotNone(tree.root) self.assertNotEquals(tree.root.getdata(), ROOT_DATA) self.assertEquals(tree.root, Node(LEFT_DATA))