def get_std_tree(): r = Node(1) r.left = Node(2) r.right = Node(3) r.left.left = Node(4) r.left.right = Node(5) r.right.left = Node(6) r.right.right = Node(7) return r
def get_child_sum_tree(): root = Node(10) root.left = Node(8) root.right = Node(2) root.left.left = Node(3) root.left.right = Node(5) root.right.left = Node(2) return root
def get_std_tree(): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) return root
def deserialize(string, m): global IDX if IDX > m: return None if string[IDX] == '$': IDX += 1 return None root = Node(string[IDX]) IDX += 1 root.left = deserialize(string, m) root.right = deserialize(string, m) return root
def build_tree_from_po_and_io(po, io, start, end): global PO_INDEX if start > end: return None key = po[PO_INDEX] node = Node(key) PO_INDEX -= 1 if start == end: return node in_idx = search(io, start, end, key) # start from right node.right = build_tree_from_po_and_io(po, io, in_idx + 1, end) node.left = build_tree_from_po_and_io(po, io, start, in_idx - 1) return node
def create_linked_binary_tree(array): q = Queue() i = 0 root = Node(array[i]) q.put(root) max_index = len(array) - 1 i += 1 while not q.empty(): node = q.get() if i <= max_index: node.left = Node(array[i]) i += 1 q.put(node.left) if i <= max_index: node.right = Node(array[i]) i += 1 q.put(node.right) return root
while root is not None: if root.left is None: print root, root = root.right else: pre = root.left while pre.right is not None and pre.right != root: pre = pre.right if pre.right is None: pre.right = root root = root.left else: pre.right = None print root, root = root.right if __name__ == '__main__': r = Node(1) r.left = Node(2) r.right = Node(3) r.left.left = Node(4) r.left.right = Node(5) morris_traversal(r)
s = root.data + ls + rs is_found = False # to consider non leaf nodes if len(s) > 3: if hm.get(s): is_found = True hm[s] = True return is_found_left or is_found_right or is_found, s if __name__ == '__main__': r = Node('A') r.left = Node('B') r.right = Node('C') r.left.left = Node('D') r.left.right = Node('E') r.right.right = Node('B') r.right.right.left = Node('D') r.right.right.right = Node('E') print check_duplicate(r, {}) # false case r = Node('A') r.left = Node('B') r.right = Node('C') r.left.left = Node('D')
while not s1.is_empty() or not s2.is_empty(): while not s1.is_empty(): node = s1.pop() print node, if node.left: s2.push(node.left) if node.right: s2.push(node.right) while not s2.is_empty(): node = s2.pop() print node, if node.right: s1.push(node.right) if node.left: s1.push(node.left) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) spiral_print(root)
_deepest_odd_level_node = root if l_node is not None and l_level % 2 != 0: deepest_odd_level = l_level _deepest_odd_level_node = l_node if r_node is not None and r_level % 2 != 0 and deepest_odd_level < r_level: deepest_odd_level = r_level _deepest_odd_level_node = r_node return _deepest_odd_level_node, deepest_odd_level if __name__ == '__main__': print '' r = Node(5) r.left = Node(10) r.right = Node(2) r.left.left = Node(3) r.left.right = Node(4) r.right.right = Node(15) r.left.right.left = Node(44) r.left.right.left.right = Node(12) r.right.right.left = Node(9) r.right.right.left = Node(8) print deepest_odd_level_node(r, 1) r = Node(10) r.left = Node(28) r.right = Node(13)
def convert_to_mirror(root): if root is None: return left = root.left root.left = root.right root.right = left convert_to_mirror(root.left) convert_to_mirror(root.right) if __name__ == '__main__': t = Node(1) t.left = Node(2) t.right = Node(3) t.left.left = Node(4) t.left.right = Node(5) convert_to_mirror(t) print t, t.left, t.right, t.right.left, t.right.right def iterative_mirror(root): q = Queue() q.put(root) while not q.empty(): node = q.get()
def eval_expression_tree(root): if is_leaf(root): return root.data left_val = eval_expression_tree(root.left) right_val = eval_expression_tree(root.right) op = root.data funct = EXPS[op] return funct(left_val, right_val) if __name__ == '__main__': print '' r = Node('+') r.left = Node(3) r.right = Node('*') r.right.left = Node('+') r.right.right = Node(2) r.right.left.left = Node(5) r.right.left.right = Node(9) print eval_expression_tree(r) """ amzn http://www.geeksforgeeks.org/given-a-binary-tree-how-do-you-remove-all-the-half-nodes/ """
def is_subtree(root1, root2): if root1: if root1.data == root2.data and are_trees_identical(root1, root2): return True is_found_left = is_subtree(root1.left, root2) is_found_right = is_subtree(root1.right, root2) return is_found_left or is_found_right return False if __name__ == '__main__': print '' root1 = Node(26) root1.left = Node(10) root1.right = Node(3) root1.left.left = Node(4) root1.left.right = Node(6) root1.left.left.right = Node(30) root1.right.right = Node(3) root2 = Node(10) root2.left = Node(4) root2.right = Node(6) root2.left.right = Node(30) print 'is subtree' print is_subtree(root1, root2) """