def test_pre_order_more(): tree = BinarySearchTree() tree.add(20) tree.add(5) tree.add(10) tree.add(15) tree.add(25) tree.add(35) tree.add(40) tree.add(23) tree.add(45) # assert tree._root.value == 5 # assert not tree._root.left # assert tree._root.right.value == 10 # assert not tree._root.right.left # assert tree._root.right.right.value == 15 # assert not tree._root.right.right.left # assert tree._root.right.right.right.value == 20 # assert not tree._root.right.right.right.left expected = [20, 5, 10, 15, 25, 23, 35, 40, 45] actual = tree.pre_order() assert actual == expected
def test_post_order_three(): tree = BinarySearchTree() tree.add(5) tree.add(3) tree.add(15) expected = [3, 15, 5] actual = tree.post_order() assert actual == expected
def test_contains_three_right(): tree = BinarySearchTree() tree.add(20) tree.add(30) tree.add(10) expected = True actual = tree.contains(30) assert actual == expected
def test_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
def make_tree(): tree = BinarySearchTree() tree.put("mak", 50) tree.put("bop", 60) tree.put("sdd", 34) tree.put("elo", 36) print(tree.length())
def test_traverse_ordered(): bst = BinarySearchTree() item_list = list(range(10)) for item in item_list: bst.add_value(item) assert_traverse_order(item_list, bst)
def test_pop_min_ordered(): bst = BinarySearchTree() item_list = list(range(10)) for item in item_list: bst.add_value(item) assert bst.pop_min() == 0
def test_pop_max_reverse_ordered(): bst = BinarySearchTree() item_list = list(range(10))[::-1] for item in item_list: bst.add_value(item) assert bst.pop_max() == 9
def test_pop_min_random(): bst = BinarySearchTree() item_list = list(range(10)) shuffled_item_list = item_list[:] random.shuffle(shuffled_item_list) for item in shuffled_item_list: bst.add_value(item) assert bst.pop_min() == 0
def test_traverse_random(): bst = BinarySearchTree() item_list = list(range(10)) shuffled_item_list = item_list[:] random.shuffle(shuffled_item_list) for item in shuffled_item_list: bst.add_value(item) assert_traverse_order(item_list, bst)
def test_contains_lots(): tree = BinarySearchTree() tree.add(20) tree.add(15) tree.add(24) tree.add(16) tree.add(14) tree.add(22) tree.add(30) expected = True actual = tree.contains(16) assert actual == expected
def test_in_order_more(): tree = BinarySearchTree() tree.add(20) tree.add(5) tree.add(10) tree.add(15) tree.add(25) tree.add(35) tree.add(40) tree.add(23) tree.add(45) expected = [15, 10, 5, 23, 45, 40, 35, 25, 20] actual = tree.post_order() assert actual == expected
if target > nums[0] and target < nums[mid]: return search(nums[:mid], target) else: return search(nums[mid + 1:], target) if __name__ == "__main__": search([ 10, 10, 10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -9, -9, -8, -8, -8, -8, -8, -8, -8, -8, -7, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -4, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10, 10 ], -6) bst = BinarySearchTree() input = [] for _ in range(5): input.append(_ * _) construct_bst_from_sorted_array(input, bst) print(f"{bst.traverse('bfs')}, Balanced: {bst.is_balanced()}") #bst.get_heights() bst.clear() for _ in [49, 9, 121, 1, 25, 81, 169, 0, 4, 16, 36, 64, 100, 144, 196]: bst.insert(_) children_list = [] parent_list = [bst.root]
def bst(): bst = BinarySearchTree() bst.__setitem__(29, "Omondi") bst.__setitem__(14, "Allaw") bst.__setitem__(58, "Ouru") print(bst.__getitem__(14))
def bst(): return BinarySearchTree()
y_distrs.append(Y[z_t > 0]) return np.stack(stds), y_distrs, zs if __name__ == "__main__": from math import isclose from trees import BinarySearchTree NB_CLASSES = 1 NB_POINTS = 10 Y = np.array([0] * NB_POINTS) bst1 = BinarySearchTree(4) # highest score when all points are assigned to the same leaf purity = np.array([[1] * bst1.nb_nodes]) assert dendrogram_purity(bst1, Y, Y, purity, NB_CLASSES) == 1.0 assert dendrogram_purity(bst1, Y + 1, Y, purity, NB_CLASSES) == 1.0 purity = np.array([[1, 0] * bst1.nb_nodes]) assert dendrogram_purity(bst1, Y + 1, Y, purity, 2) == 1.0 purity = np.array([[0.5, 0.5] * bst1.nb_nodes]) assert dendrogram_purity(bst1, Y + 1, Y, purity, 2) == 0.5 # lowest score when purity is null purity = np.array([[0] * bst1.nb_nodes]) assert dendrogram_purity(bst1, Y, Y, purity, NB_CLASSES) == 0.
def test_pop_min_empty(): bst = BinarySearchTree() assert bst.pop_min() is None
def test_traverse_empty(): bst = BinarySearchTree() with pytest.raises(StopIteration): next(bst.traverse())
def test_contains_one(): tree = BinarySearchTree() tree.add(20) expected = True actual = tree.contains(20) assert actual == expected
def __init__(self, tree_dict=None): if not tree_dict: self.tree_dict = {'BST':BinarySearchTree(), 'SplayTree':SplayTree()} else: self.tree_dict = tree_dict
def test_tree_one_member(): tree = BinarySearchTree() tree.add('apples') assert tree._root.value == 'apples'