class TestBinarySearchTreeBfs(unittest.TestCase): def tearDown(self): self.bst = None self.results = None def setUp(self): self.bst = ValidateBst() self.results = Results() self.traversal = Traversal() def test_bst(self): bst = ValidateBst(Node(5)) bst.insert(8) bst.insert(5) bst.insert(6) bst.insert(4) bst.insert(7) self.assertEqual(bst.validate_bst(), True) self.traversal.in_order_traversal(bst.root, self.results.add_result) self.assertEqual(self.results.results, [4, 5, 5, 6, 7, 8]) bst = ValidateBst(Node(5)) left = Node(5) right = Node(8) invalid = Node(20) bst.root.left = left bst.root.right = right bst.root.left.right = invalid self.assertEqual(bst.validate_bst(), False)
def test_reconstruct_bt(): rbt = ReconstructBinaryTree() preorder = [1, 2, 4, 7, 3, 5, 6, 8] inorder = [4, 7, 2, 1, 5, 3, 8, 6] bt = rbt.construct(preorder, inorder) traversal = Traversal() results = Results() traversal.in_order_traversal(bt, results.add_result) print(results.results)
class TestBinarySearchTreeBfs(unittest.TestCase): def tearDown(self): self.bst = None def setUp(self): self.bst = InOrderSuccessorBst() self.traversal = Traversal() self.results = Results() def test_bst(self): node_datas = [5, 2, 8, 1, 3, 9, 7, 6] for node_data in node_datas: self.bst.insert(node_data) self.traversal.in_order_traversal(self.bst.root, self.results.add_result) print(self.results.results) test_node = self.bst.root.left.right print(test_node.parent.parent) self.assertEqual(self.bst.in_order_successor(test_node), 5)
class TestBinarySearchTree(unittest.TestCase): def tearDown(self): self.bst = None self.results = None def setUp(self): self.bst = BinarySearchTree() self.results = Results() self.traversal = Traversal() def test_bst(self): node_datas = [5, 2, 8, 1, 3] for node_data in node_datas: self.bst.insert(node_data) self.assertEqual(self.bst.root.val, 5) self.traversal.in_order_traversal(self.bst.root, self.results.add_result) self.assertEqual(self.results.results, [1, 2, 3, 5, 8]) self.results.clean_results() self.traversal.pre_order_traversal(self.bst.root, self.results.add_result) self.assertEqual(self.results.results, [5, 2, 1, 3, 8]) self.results.clean_results() self.traversal.post_order_traversal(self.bst.root, self.results.add_result) self.assertEqual(self.results.results, [1, 3, 2, 8, 5]) self.results.clean_results()
def setUp(self): self.bst = InOrderSuccessorBst() self.traversal = Traversal() self.results = Results()
def setUp(self): self.bst = ValidateBst() self.results = Results() self.traversal = Traversal()
def setUp(self): self.bst = BinarySearchTree() self.results = Results() self.traversal = Traversal()
def setUp(self): self.bst = MinBst() self.traversal = Traversal() self.results = Results()