def run(): # tree = BuildLinkedBinaryTree(auto_populate=True).get_tree() tree = BuildLinkedBinaryTree().get_diamond_tree() print('Left-view: ') for node in left_view(tree, LinkedBinaryTree.DFS): print(tree.element(node), end=' ')
def run(): binary_tree = BuildLinkedBinaryTree(list_of_nodes=[60, 70, 80, 90, 100, ], auto_populate=True).get_tree() searched_node = 100 print('Tree Level order traversal: ') print_bfs(binary_tree) print(f'Path to node ({searched_node}): ') for node in get_bt_path_to_node(binary_tree, searched_node): print(binary_tree.element(node), end=' ')
def run(): binary_tree = BuildLinkedBinaryTree(list_of_nodes=[60, 70, 80, 90, 100, ], auto_populate=True).get_tree() node_1 = 30 node_2 = 70 print('Tree Level order traversal: ') print_bfs(binary_tree) print(f'LCA of {node_1} and {node_2} is: ') lca_node = get_LCA_in_BT(binary_tree, node_1, node_2) if lca_node: print(binary_tree.element(lca_node)) else: print('No LCA exists!')
def run(): binary_tree = BuildLinkedBinaryTree(list_of_nodes=[ 8, 3, 10, ]).get_tree() root = binary_tree.root() root._left._left = LinkedBinaryTree.get_new_node(1) root._right._right = LinkedBinaryTree.get_new_node(14) root._right._right._left = LinkedBinaryTree.get_new_node(13) root._right._left = LinkedBinaryTree.get_new_node(6) root._right._left._left = LinkedBinaryTree.get_new_node(4) root._right._left._right = LinkedBinaryTree.get_new_node(7) for diagonal_nodes_list in diagonal_traversal(binary_tree): print([node.data for node in diagonal_nodes_list])
def run(): binary_tree = BuildLinkedBinaryTree(auto_populate=True, list_of_nodes=[60, 70, 80, 90, 100]).get_tree() node_data = 60 inorder_successor_node = get_bt_inorder_successor(binary_tree, node_data=node_data) Traversal(binary_tree).print_inorder_traversal() if inorder_successor_node: print(f'In-order successor of the node({node_data}): ', inorder_successor_node.data) else: print(f'In order successor of given node({node_data}) doesn\'t exists')
def run(): bst = BuildLinkedBinaryTree().get_tree() bst._root = bst.get_new_node(1) bst._root._left = bst.get_new_node(3) bst._root._left._right = bst.get_new_node(2) print('Initial', end=' ') Traversal(bst).print_inorder_traversal() RecoverBST(bst).fix_bst() print('After swapping incorrect nodes', end=' ') Traversal(bst).print_inorder_traversal()
def run(): from trees.utils import Traversal binary_tree = BuildLinkedBinaryTree(list_of_nodes=[ 60, 70, 80, 90, 100, ], auto_populate=True).get_tree() Traversal(binary_tree).print_level_order_traversal() print('ZigZag Traversal of above tree is: ') append_right_first = False for node, level_bool in zigzag_traversal( binary_tree=binary_tree, append_right_first=append_right_first): if level_bool != append_right_first: print('\n', '=' * 15, sep='') append_right_first = level_bool print(node.data, end=' ')
def run(): node_1 = 40 node_2 = 60 binary_tree = BuildLinkedBinaryTree(list_of_nodes=[ 60, 70, 80, 90, 100, ], auto_populate=True).get_tree() Traversal(binary_tree).print_inorder_traversal() distance = get_dist_bw_nodes(binary_tree, node_1, node_2) if distance: print(f'Distance b/w nodes {node_2} and {node_1} is: {distance}') else: print( f'One of given or both nodes {node_1} and {node_2}, do not exists in given tree' )
def run(): # -------------------- Create Tree -------------------- # binary_tree = BuildLinkedBinaryTree(auto_populate=True).get_tree() binary_tree = BuildLinkedBinaryTree(list_of_nodes=[ 20, 8, 22, 4, 12, ]).get_tree() binary_tree._root._right._right = binary_tree.get_new_node(25) binary_tree._root._left._right._right = binary_tree.get_new_node(14) binary_tree._root._left._right._left = binary_tree.get_new_node(10) # -------------------- -------------------- # print('Boundary Nodes traversal: ') for boundary_node in BoundaryTraversal(binary_tree).boundary_traversal(): print(boundary_node.data, end=' ')
def run(): # Make Tree binary_tree = BuildLinkedBinaryTree(list_of_nodes=[1, 0, 0, 1, 0]).get_tree() root_right = binary_tree.right(binary_tree.root()) binary_tree.add_right_child(root_right, 1) binary_tree.add_right_child(binary_tree.right(root_right), 1) root_left = binary_tree.left(binary_tree.root()) binary_tree.add_left_child(binary_tree.left(root_left), 1) root_left_right = binary_tree.right(root_left) binary_tree.add_right_child(root_left_right, 0) binary_tree.add_left_child(root_left_right, 1) binary_tree.add_left_child(binary_tree.left(root_left_right), 1) binary_tree.add_left_child(binary_tree.right(root_left_right), 0) binary_tree.add_left_child(binary_tree.left(binary_tree.right(root_left_right)), 1) # Traversal(binary_tree).print_level_order_traversal() ############################################# equal_01_subtrees = get_equal_0_1_subtrees(binary_tree) if equal_01_subtrees: print(f'Yes, {len(equal_01_subtrees)} subtrees having equal 0s and 1s nodes are present') for subtree in equal_01_subtrees: Traversal(binary_tree).print_level_order_traversal(subtree) else: print('No subtree having equal 0s 1s is present')