def general_test(self, bst=None): if bst is not None: result = self.f(bst.root) print(result) for r in result: l = BinSearchTree() for v in r: l.insert(v) if l.root != bst.root: self.fail(bst, r) if len(set(result)) != len(result): self.fail("repeats")
def bst_from_sorted_array(a): if not a: return BinSearchTree() else: b = BinSearchTree() mid = (len(a) - 1) // 2 b.insert(a[mid]) result_left = bst_from_sorted_array(a[0:mid]).root result_right = bst_from_sorted_array(a[mid + 1:]).root if result_left.is_empty(): result_left = None if result_right.is_empty(): result_right = None b.root.left = result_left b.root.right = result_right return b
def test_8(self): t = BinSearchTree(20, 10, 30) r = BinSearchTree(25) t.root.left.right = r.root self.assertFalse(self.f(t.root))
def test_7(self): t = BinSearchTree(1) r = BinSearchTree(0) t.root.right = r.root self.assertFalse(self.f(t.root))
def test_6(self): t = BinSearchTree(1) r = BinSearchTree(5) t.root.left = r.root self.assertFalse(self.f(t.root))
def test_3(self): t = BinSearchTree(1) self.assertTrue(self.f(t.root))
def test_2(self): t = BinSearchTree(1, 2, 3, 4) self.assertTrue(self.f(t.root))
def test_3(self): bst = BinSearchTree(1) self.general_test(bst)
def test_2(self): bst = BinSearchTree(50, 20, 60, 10, 25, 5, 15, 65, 80) self.general_test(bst)
def test_1(self): bst = BinSearchTree(3, 5, 1) self.general_test(bst)