def deserialize(arr, i): """Deserialize the binary tree""" if arr[i[0]] is None: i[0] = i[0] + 1 return None node = Node(arr[i[0]]) i[0] = i[0] + 1 node.left = deserialize(arr, i) node.right = deserialize(arr, i) return node
def bst(start, end, arr): """ Convert given sorted array to balanced BST""" if end < start: return mid = (start + end) / 2 mid = int(mid) newnode = Node(arr[mid]) newnode.left = bst(start, mid - 1, arr) newnode.right = bst(mid + 1, end, arr) return newnode
def insert_node(node, key): """Insert node in the given binary tree""" if key > node.data: if node.right is None: newnode = Node(key) node.right = newnode else: insert_node(node.right, key) elif key < node.data: if node.left is None: newnode = Node(key) node.left = newnode else: insert_node(node.left, key)
def bt_from_linked_list(node, queue): """ Binary tree from linked list """ root = Node(node.data) queue.append(root) while len(queue) > 0: pop_node = queue.pop(0) if node.right is not None: temp_node = Node(node.right.data) pop_node.left = temp_node queue.append(temp_node) node = node.right if node.right is not None: temp_node = Node(node.right.data) pop_node.right = temp_node queue.append(temp_node) node = node.right return root
while rtree.left: rtree = rtree.left rtree.left = root root.right = rtree return root def convert_to_bt_dll_approach2(root): pass def convert_to_bt_dll_approach3(root): pass if __name__ == '__main__': root = Node(1) root.left = Node(2) root.left.left = Node(3) root.left.right = Node(4) convert_to_bt_dll_approach1(root)
from trees.utils import Node from collections import defaultdict def diagonal_traversal(node, level, diagonal): """ Diagonal traversal of binary tree """ if node is None: return None diagonal[level].append(node.data) diagonal_traversal(node.left, level + 1, diagonal) diagonal_traversal(node.right, level, diagonal) return diagonal if __name__ == '__main__': root = Node(8) root.left = Node(3) root.right = Node(10) root.left.left = Node(1) root.left.right = Node(6) root.right.right = Node(14) root.right.right.left = Node(13) root.left.right.left = Node(4) root.left.right.right = Node(7) diagonal = defaultdict(list) diagonal_traversal(root, 0, diagonal) print(diagonal)
""" Check whether tree is a sub-tree of given binary subtree """ if tree is None: return 0 if tree.data == subtree.data: if identical_tree(tree, subtree) == 1: print('subtree is present in given binary tree') return 1 return bt_subtree_approach1(tree.left, subtree) or bt_subtree_approach1( tree.right, subtree) def bt_subtree_approach(tree, subtree): pass if __name__ == '__main__': T = Node(26) T.right = Node(3) T.right.right = Node(3) T.left = Node(10) T.left.left = Node(4) T.left.left.right = Node(30) T.left.right = Node(6) S = Node(10) S.right = Node(6) S.left = Node(4) S.left.right = Node(30) bt_subtree_approach1(T, S)
""" Convert given bst to array""" if node is None: return bst_array(node.left, arr) arr.append(node) bst_array(node.right, arr) return arr def array_balanced_bst(arr, start, end): """ Convert given array of nodes to balanced BST """ if start > end: return mid = int((start + end) / 2) node = arr[mid] node.left = array_balanced_bst(arr, start, mid - 1) node.right = array_balanced_bst(arr, mid + 1, end) return node if __name__ == '__main__': root = Node(10) root.left = Node(8) root.left.left = Node(7) root.left.left.left = Node(6) root.left.left.left.left = Node(5) arr = bst_array(root, []) node = array_balanced_bst(arr, 0, len(arr) - 1) in_order_traversal(node)
if node is None: return 0 if node.left is None and node.right is None: return node.data left_exp = evaluate_expression_tree(node.left) right_exp = evaluate_expression_tree(node.right) if node.data == '+': return left_exp + right_exp if node.data == '-': return left_exp - right_exp if node.data == '*': return left_exp * right_exp if node.data == '/': return left_exp / right_exp if __name__ == '__main__': root = Node('+') root.left = Node('*') root.left.left = Node(5) root.left.right = Node(4) root.right = Node('-') root.right.left = Node(100) root.right.right = Node(20) print(evaluate_expression_tree(root))
from trees.utils import Node def count_no_of_nodes(node, size): """Checks removing an edge from binary tree can divide it into two halfs""" if node is None: return False count = count_no_of_nodes(node.left, size) + 1 + count_no_of_nodes(node.right, size) if count == size - count: print('1') return count if __name__ == '__main__': root = Node(5) root.left = Node(1) root.right = Node(6) root.left.left = Node(3) root.right.left = Node(7) root.right.right = Node(4) count_no_of_nodes(root, 6)
if pop_node.data == key: if len(queue) == 0 or pop_level != levels[0]: print("No right node to given key") return print(queue.pop(0).data) return 0 if pop_node.left is not None: queue.append(pop_node.left) levels.append(pop_level + 1) if pop_node.right is not None: queue.append(pop_node.right) levels.append(pop_level + 1) print("No right node to given key") if __name__ == '__main__': root = Node(10) root.left = Node(2) root.right = Node(6) root.right.right = Node(5) root.left.left = Node(8) root.left.right = Node(4) next_right_node(root, 8)
while i < count_node: if queue[0].left is not None: queue.append(queue[0].left) if queue[0].right is not None: queue.append(queue[0].right) pop_node = queue.pop(0) if not flag and i == 0: print(pop_node.data) elif flag and i == count_node - 1: print(pop_node.data) i = i + 1 flag = not flag if __name__ == '__main__': root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.right = Node(7) root.left.left.left = Node(8) root.left.left.right = Node(9) root.left.right.left = Node(10) root.left.right.right = Node(11) root.right.right.left = Node(14) root.right.right.right = Node(15)
while len(stack2) > 0: node = stack2.pop(0) sum += node.data if node.left is not None: stack1.append(node.left) if node.right is not None: stack1.append(node.right) if sum != 0: product = product * sum if len(stack2) == 0 and len(stack1) == 0: print(product) if len(stack1) > 0 or len(stack2) > 0: return product_sum(stack1, stack2, node, product) if __name__ == '__main__': stack1 = [] stack2 = [] product = 1 root = Node(2) stack1.append(root) root.left = Node(7) root.right = Node(5) root.left.right = Node(6) root.left.left = Node(8) product_sum(stack1, stack2, root, 1)
def children_sum(node): """ children sum property """ if node.left is None and node.right is None: return else: children_sum(node.left) children_sum(node.right) if node.left is not None: left_data = node.left.data if node.right is not None: right_data = node.right.data diff = left_data + right_data - node.data if diff > 0: node.data = node.data + diff else: increment_child(node, -diff) return node if __name__ == '__main__': root = Node(50) root.left = Node(7) root.right = Node(2) root.left.left = Node(3) root.left.right = Node(5) root.right.left = Node(1) root.right.right = Node(30) node = children_sum(root) pre_order_traversal(node)
elif (key > root.data): root.right = delete_node(root.right, key) else: if root.left is None: temp = root.right return temp elif root.right is None: temp = root.left return temp temp = min_value_node(root.right) root.data = temp.data root.right = delete_node(root.right, temp.data) return root if __name__ == '__main__': root = Node(50) root.left = Node(30) root.right = Node(70) root.left.left = Node(20) root.left.right = Node(40) root.right.left = Node(60) root.right.right = Node(80) #insert_node(root, 90) #search(root, 80) delete_node(root, 20) pre_order_traversal(root)
from trees.utils import Node, pre_order_traversal def sum_tree(node): """ Convert the given binary tree into sum tree""" if node is None: return 0 left_sum = sum_tree(node.left) right_sum = sum_tree(node.right) previous = node.data node.data = left_sum + right_sum return node.data + previous if __name__ == '__main__': root = Node(10) root.left = Node(-2) root.right = Node(6) root.left.left = Node(8) root.left.right = Node(-4) root.right.left = Node(7) root.right.right = Node(5) sum_tree(root) pre_order_traversal(root)
return None if node.right is not None: print(node.data) print_left_boundary(node.right) elif node.left is not None: print(node.data) print_left_boundary(node.left) def print_leaves(node): """ Print leaves of binary tree """ if node is None: return None print_leaves(node.left) if node.left is None and node.right is None: print(node.data) print_leaves(node.right) if __name__ == '__main__': root = Node(20) root.left = Node(8) root.left.left = Node(4) root.left.right = Node(12) root.left.right.left = Node(10) root.left.right.right = Node(14) root.right = Node(22) root.right.right = Node(25) print_left_boundary(root) print_leaves(root) print_right_boundary(root)