class TreeDeleteTests(unittest.TestCase): def setUp(self): # 10 # / \ # 5 15 # / / \ # 2 12 20 # / \ # 1 3 self.t = BinarySearchTree(10) self.t.insert(5) self.t.insert(2) self.t.insert(1) self.t.insert(3) self.t.insert(15) self.t.insert(12) self.t.insert(20) def test_delete_leaf_left(self): self.t.delete(12) self.assertIsNone(self.t.lookup(12)[0]) self.assertEqual(self.t.lookup(15)[0].key, 15) self.assertEqual(self.t.lookup(20)[0].key, 20) def test_delete_leaf_right(self): self.t.delete(20) self.assertIsNone(self.t.lookup(20)[0]) self.assertEqual(self.t.lookup(12)[0].key, 12) self.assertEqual(self.t.lookup(15)[0].key, 15) def test_delete_one_child(self): self.t.delete(5) self.assertIsNone(self.t.lookup(5)[0]) self.assertEqual(self.t.lookup(1)[0].key, 1) self.assertEqual(self.t.lookup(2)[0].key, 2) self.assertEqual(self.t.lookup(3)[0].key, 3) self.assertEqual(self.t.lookup(10)[0].key, 10) def test_delete_parent(self): self.t.delete(2) self.assertIsNone(self.t.lookup(2)[0]) self.assertEqual(self.t.lookup(5)[0].key, 5) self.assertEqual(self.t.lookup(3)[0].key, 3) self.assertEqual(self.t.lookup(1)[0].key, 1) def test_delete_no_parent(self): self.t.delete(10) self.assertIsNone(self.t.lookup(10)[0]) self.assertEqual(self.t.lookup(5)[0].key, 5) def test_one_child_no_parent(self): #Test for one child and no parent, left side self.t.delete(15) self.t.delete(12) self.t.delete(20) self.t.delete(10) self.assertEqual(self.t.lookup(5)[0].key, 5) self.assertListEqual([x.key for x in self.t.lookup(2)], [2, 5]) self.assertListEqual([x.key for x in self.t.lookup(1)], [1, 2]) self.assertListEqual([x.key for x in self.t.lookup(3)], [3, 2]) def test_traverse(self): self.assertEqual([1, 2, 3, 5, 10, 12, 15, 20], [x.key for x in self.t.traverse()]) def test_str(self): self.assertEqual(self.t.__str__(), '1, 2, 3, 5, 10, 12, 15, 20')
class TreeTests(unittest.TestCase): def setUp(self): self.tree = BinarySearchTree(50) def text_insert(self): self.tree.insert(40) self.tree.insert(30) self.tree.insert(60) self.tree.insert(70) assertEqual([self.tree.left.left, self.tree.left, self.tree, self.right, self.right.right], self.tree.tranverse()) def test_lookup(self): self.assertEqual(self.tree, self.tree.lookup(50)[0]) self.tree.insert(40) self.tree.insert(60) self.assertEqual(self.tree.left, self.tree.lookup(40)[0]) self.assertEqual(self.tree.right, self.tree.lookup(60)[0]) self.assertEqual(None, self.tree.lookup(10)[0]) def test_traverse(self): self.tree.insert(40) self.tree.insert(60) self.tree.insert(45) self.tree.insert(55) self.tree.insert(65) result = [] for node in self.tree.traverse(): result.append(node.key) self.assertEqual([40,45,50,55,60,65], result) def test_delete(self): self.tree.insert(25) self.tree.insert(30) self.tree.insert(20) self.tree.insert(27) self.tree.insert(23) self.tree.insert(24) self.tree.insert(19) self.tree.insert(75) self.tree.insert(70) self.tree.insert(80) self.tree.insert(72) self.tree.insert(77) self.tree2 = BinarySearchTree(50) self.tree2.insert(27) self.tree2.insert(30) self.tree2.insert(20) self.tree2.insert(23) self.tree2.insert(24) self.tree2.insert(19) self.tree2.insert(75) self.tree2.insert(80) self.tree2.insert(72) self.tree.delete(25) self.tree.delete(77) self.tree.delete(70) self.assertTrue(self.tree == self.tree2) def test_str(self): self.tree.insert(25) self.tree.insert(30) self.tree.insert(20) self.tree.insert(27) self.tree.insert(23) self.tree.insert(24) self.tree.insert(19) self.tree.insert(75) self.tree.insert(70) self.tree.insert(80) self.tree.insert(72) self.tree.insert(77) self.assertEqual(str(self.tree), str([19,20,23,24,25,27,30,50,70,72,75,77,80]))
class TreeTests(unittest.TestCase): def setUp(self): self.tree = BinarySearchTree(50) def text_insert(self): self.tree.insert(40) self.tree.insert(30) self.tree.insert(60) self.tree.insert(70) assertEqual([ self.tree.left.left, self.tree.left, self.tree, self.right, self.right.right ], self.tree.tranverse()) def test_lookup(self): self.assertEqual(self.tree, self.tree.lookup(50)[0]) self.tree.insert(40) self.tree.insert(60) self.assertEqual(self.tree.left, self.tree.lookup(40)[0]) self.assertEqual(self.tree.right, self.tree.lookup(60)[0]) self.assertEqual(None, self.tree.lookup(10)[0]) def test_traverse(self): self.tree.insert(40) self.tree.insert(60) self.tree.insert(45) self.tree.insert(55) self.tree.insert(65) result = [] for node in self.tree.traverse(): result.append(node.key) self.assertEqual([40, 45, 50, 55, 60, 65], result) def test_delete(self): self.tree.insert(25) self.tree.insert(30) self.tree.insert(20) self.tree.insert(27) self.tree.insert(23) self.tree.insert(24) self.tree.insert(19) self.tree.insert(75) self.tree.insert(70) self.tree.insert(80) self.tree.insert(72) self.tree.insert(77) self.tree2 = BinarySearchTree(50) self.tree2.insert(27) self.tree2.insert(30) self.tree2.insert(20) self.tree2.insert(23) self.tree2.insert(24) self.tree2.insert(19) self.tree2.insert(75) self.tree2.insert(80) self.tree2.insert(72) self.tree.delete(25) self.tree.delete(77) self.tree.delete(70) self.assertTrue(self.tree == self.tree2) def test_str(self): self.tree.insert(25) self.tree.insert(30) self.tree.insert(20) self.tree.insert(27) self.tree.insert(23) self.tree.insert(24) self.tree.insert(19) self.tree.insert(75) self.tree.insert(70) self.tree.insert(80) self.tree.insert(72) self.tree.insert(77) self.assertEqual( str(self.tree), str([19, 20, 23, 24, 25, 27, 30, 50, 70, 72, 75, 77, 80]))