def test_add_three_members(): tree = BinarySearchTree() tree.add(10) tree.add(5) tree.add(15) assert tree._root.value == 10 assert tree._root.left.value == 5 assert tree._root.right.value == 15
# self.nums.append(root.val) # pre(root.left) # pre(root.right) # pre(root) # return self.nums # 迭代 class Solution: def preorder(self, root): tree = [root] nums = [] while tree: n = tree.pop() nums.append(n.val) if n.right: tree.append(n.right) if n.left: tree.append(n.left) return nums tree = Tree() tree.add(1) tree.add(None) tree.add(2) tree.add(None) tree.add(None) tree.add(3) cs = Solution() print(cs.preorder(tree.root))
def test_check_one_node_tree(): tree = BinarySearchTree() tree.add(20) assert tree.contains(20) == True assert tree.contains(21) == False
def test_add_one_node(): tree = BinarySearchTree() tree.add(20) assert tree._root.value == 20 assert tree._root.left == None assert tree._root.right == None
def my_bst(): tree = BinarySearchTree() tree.add(15) tree.add(11) tree.add(13) tree.add(7) tree.add(8) tree.add(5) tree.add(19) tree.add(17) tree.add(23) return tree
def test_add_more_members_for_balanced(): tree = BinarySearchTree() tree.add(15) tree.add(11) tree.add(13) tree.add(7) tree.add(25) tree.add(60) tree.add(23) assert tree._root.value == 15 assert tree._root.left.value == 11 assert tree._root.right.value == 25 assert tree._root.left.left.value == 7 assert tree._root.left.right.value == 13 assert tree._root.right.right.value == 60 assert tree._root.right.left.value == 23
def test_tree_one_member(): tree = BinarySearchTree() tree.add('apples') assert tree._root.value == 'apples'
def set_tree(self, array, model): tree = Tree() for i in range(len(array)): tree.add(array[i]) self.output_tree(tree, model)