def test_show(): def convert_show(target): show(convert(target)) def convert_self_show(target): convert(target).show() for invalid_argument in [1, 'foo']: with pytest.raises(ValueError) as err: show(invalid_argument) assert str(err.value) == 'Expecting a list or a node' for show_func in [pprint, show, convert_show]: with CaptureOutput() as output: show_func([]) assert output == [''] for show_func in [pprint, show, convert_show, convert_self_show]: with CaptureOutput() as output: show_func([1, 2]) assert output == ['', ' 1', ' / ', '2 ', ' '] with CaptureOutput() as output: show_func([1, None, 3]) assert output == ['', '1 ', ' \\ ', ' 3', ' '] with CaptureOutput() as output: show_func([1, 2, 3]) assert output == ['', ' 1 ', ' / \\ ', '2 3', ' '] with CaptureOutput() as output: show_func([1, 2, 3, None, 5]) assert output == [ '', ' __1 ', ' / \\ ', '2 3', ' \\ ', ' 5 ', ' ' ] with CaptureOutput() as output: show_func([1, 2, 3, None, 5, 6]) assert output == [ '', ' __1__ ', ' / \\ ', '2 3', ' \\ / ', ' 5 6 ', ' ' ] with CaptureOutput() as output: show_func([1, 2, 3, None, 5, 6, 7]) assert output == [ '', ' __1__ ', ' / \\ ', '2 3 ', ' \\ / \\ ', ' 5 6 7', ' ' ] with CaptureOutput() as output: show_func([1, 2, 3, 8, 5, 6, 7]) assert output == [ '', ' __1__ ', ' / \\ ', ' 2 3 ', ' / \\ / \\ ', '8 5 6 7', ' ' ] for _ in range(repetitions): bt = tree(height=10) with CaptureOutput() as output: show(bt) assert output == stringify(bt).splitlines()
def main(): root = Node(15) for key in [6, 18, 3, 7, 17, 20, 2, 4, 13, 9, 5]: root.insert(Node(key)) binarytree.show(root) print(root.search(6).predecessor) print(root.search(6).successor)
def PrintTree(root): """ Pretty print of a binary tree :type root: TreeNode :rtype: No """ if not root or not isinstance(root, TreeNode): return show(ConvertToNode(root))
def start_UPGMA(cluster_array, distance_matrix): C = [(cluster, 1) for cluster in cluster_array] height_array = [0] * len(cluster_array) dist_array = [x[::1] for x in distance_matrix] b_nodes = {cluster: bt.Node(0) for cluster in C} for ind in range(1, len(cluster_array)): (i, j), dist = get_min_index(dist_array) HCk, dki, dkj = get_cluster_distance(height_array, i, j, dist) C.append((str(C[j][0] + C[i][0]), C[j][1] + C[i][1])) new_clus_ind = len(C) - 1 height_array.append(HCk) dist_array.append([0] * (len(dist_array[0]))) for row in dist_array: row += [0] b_nodes[C[i]].value = (C[i][0], dki) b_nodes[C[j]].value = (C[j][0], dkj) b_nodes[C[new_clus_ind]] = bt.Node((C[new_clus_ind][0], 0)) b_nodes[C[new_clus_ind]].left = b_nodes[C[i]] b_nodes[C[new_clus_ind]].right = b_nodes[C[j]] for c in range(len(C) - 1): dist_array[new_clus_ind][c] = (C[i][1] * dist_array[i][c] + C[j][1] * dist_array[j][c]) / ( C[i][1] + C[j][1]) dist_array[c][new_clus_ind] = dist_array[new_clus_ind][c] if j > i: i, j = j, i C.pop(i) C.pop(j) height_array.pop(i) height_array.pop(j) dist_array.pop(i) for y in range(len(dist_array)): dist_array[y].pop(i) dist_array.pop(j) for y in range(len(dist_array)): dist_array[y].pop(j) bt.show(b_nodes[C[0]])
def left_rotate(self, node): new_parent = node.left_rotate() if node == self.root: self.root = new_parent def right_rotate(self, node): new_parent = node.right_rotate() if node == self.root: self.root = new_parent def random_numbers(total_numbers): return [int(1000 * nprnd.random()) for i in range(total_numbers)] tree = BST() for i in random_numbers(10): tree.add(i) show(tree.root) # tree.print_postorder() # tree.print_preorder() # tree.print_inorder() # tree.get_breadth_first_nodes() # tree.right_rotate(tree.root) # show(tree.root) tree.left_rotate(tree.root) show(tree.root)
def convert_show(target): show(convert(target))