def listtoTreeNode(tree_list): if not tree_list: return root = LinkedBinaryTree.Node(tree_list[0]) queue = [root] front = 0 index = 1 while True: node = queue[front] front += 1 if index >= len(tree_list): return root left_child = tree_list[index] index += 1 if left_child is not None: node.left = LinkedBinaryTree.Node(left_child) node.left.parent = node queue.append(node.left) if index >= len(tree_list): break right_child = tree_list[index] index += 1 if right_child is not None: node.right = LinkedBinaryTree.Node(right_child) node.right.parent = node queue.append(node.right) return root
def create_expression_tree_helper(root, expr, index): if expr[index].isdigit(): return LinkedBinaryTree.Node(expr[index]), index root = LinkedBinaryTree.Node(expr[index]) l, l_index = create_expression_tree_helper(expr, index + 1) r, r_index = create_expression_tree_helper(expr, l + 1) root.left = l root.right = r return root, r_index
def create_expression_tree_helper(prefix_exp, start_pos): elem = expr[start_pos] if elem not in opr: return LinkedBinaryTree.Node(elem) else: left = LinkedBinaryTree.Node(elem) prefix_exp.left = create_expression_tree_helper( left, start_pos + 1) right = LinkedBinaryTree.Node(elem) prefix_exp.right = create_expression_tree_helper( right, start_pos + 2) return prefix_exp
def main(): z = LinkedBinaryTree.Node(6) a = LinkedBinaryTree.Node(1) b = LinkedBinaryTree.Node(3, None, z) c = LinkedBinaryTree.Node(19) d = LinkedBinaryTree.Node(25) e = LinkedBinaryTree.Node(21, c, d) f = LinkedBinaryTree.Node(9) g = LinkedBinaryTree.Node(12, f, e) h = LinkedBinaryTree.Node(2, a, b) i = LinkedBinaryTree.Node(5, h, g) tree = LinkedBinaryTree(i) print(is_BST(i))
def invert_subtree(subtree_root): position = subtree_root if position.right is None and position.left is None: return position elif position.left is None and position.right is not None: right = invert_subtree(position.right) return LinkedBinaryTree.Node(subtree_root.data, right, None) elif position.left is not None and position.right is None: left = invert_subtree(position.left) return LinkedBinaryTree.Node(subtree_root.data, None, left) else: left = invert_subtree(position.left) right = invert_subtree(position.right) return LinkedBinaryTree.Node(subtree_root.data, right, left)
def invert_helper(root): if root is None: return node = LinkedBinaryTree.Node(root.data) node.right = invert_helper(root.left) node.left = invert_helper(root.right) return node
def main(): n5 = LinkedBinaryTree.Node(5) n1 = LinkedBinaryTree.Node(1) n8 = LinkedBinaryTree.Node(8) n4 = LinkedBinaryTree.Node(4) n9 = LinkedBinaryTree.Node(9, n5, n1) n2 = LinkedBinaryTree.Node(2, n9) n7 = LinkedBinaryTree.Node(7, n8, n4) n3 = LinkedBinaryTree.Node(3, n2, n7) tree = LinkedBinaryTree(n3) min_and_max(tree)
def main(): g = LinkedBinaryTree.Node(5) h = LinkedBinaryTree.Node(11) i = LinkedBinaryTree.Node(4) f = LinkedBinaryTree.Node(9, i) c = LinkedBinaryTree.Node(5, None, f) d = LinkedBinaryTree.Node(2) e = LinkedBinaryTree.Node(6, g, h) b = LinkedBinaryTree.Node(7, d, e) a = LinkedBinaryTree.Node(2, b, c) tree = LinkedBinaryTree(a) for i in range(4): print_tree_level(tree, i)
def create_expression_tree_helper(prefix_exp, start_pos): root = LinkedBinaryTree.Node(prefix_exp[start_pos]) size = 1 if prefix_exp[start_pos + size] in '+-*/': left_side = create_expression_tree_helper(prefix_exp, start_pos + size) root.left = left_side[0].root size += left_side[1] else: token_int = int(prefix_exp[start_pos + size]) root.left = LinkedBinaryTree.Node(token_int) size += 1 if prefix_exp[start_pos + size] in '+-*/': right_side = create_expression_tree_helper(prefix_exp, start_pos + size) root.right = right_side[0].root size += right_side[1] else: token_int = int(prefix_exp[start_pos + size]) root.right = LinkedBinaryTree.Node(token_int) size += 1 return (LinkedBinaryTree(root), size)
def create_expression_tree(prefix_exp_str): if prefix_exp_str == "": return LinkedBinaryTree() lst = prefix_exp_str.split(" ") for i in range(len(lst)): if lst[i] not in "+-*/": lst[i] = int(lst[i]) output = LinkedBinaryTree(LinkedBinaryTree.Node(lst[0])) prev = output.root counter = 1 while counter < len(lst): current = LinkedBinaryTree.Node(lst[counter]) if prev.left is not None and prev.right is not None: prev = prev.parent if str(prev.data) in "+-*/": if prev.left is None: prev.left = current current.parent = prev prev = current counter += 1 elif prev.right is None: prev.right = current current.parent = prev prev = current counter += 1 else: prev = prev.parent if prev.right is None: prev.right = current current.parent = prev prev = current counter += 1 return output
def main(): g = LinkedBinaryTree.Node(5) a = LinkedBinaryTree.Node(5) b = LinkedBinaryTree.Node(4) c = LinkedBinaryTree.Node(6, a, b) d = LinkedBinaryTree.Node(8, None, g) e = LinkedBinaryTree.Node(10, None, d) f = LinkedBinaryTree.Node(12, e, c) tree = LinkedBinaryTree(f) for i in tree: print(i) print(is_height_balanced(tree)) for i in tree: print(i)
def create_expression_tree(prefix_exp_str): expr = prefix_exp_str.split(" ") def create_expression_tree_helper(prefix_exp, start_pos): elem = expr[start_pos] if elem not in opr: return LinkedBinaryTree.Node(elem) else: left = LinkedBinaryTree.Node(elem) prefix_exp.left = create_expression_tree_helper( left, start_pos + 1) right = LinkedBinaryTree.Node(elem) prefix_exp.right = create_expression_tree_helper( right, start_pos + 2) return prefix_exp root = LinkedBinaryTree.Node(expr[1]) return LinkedBinaryTree(create_expression_tree_helper(root, 0))
def main(): a = LinkedBinaryTree.Node(5) b = LinkedBinaryTree.Node(4,a) c = LinkedBinaryTree.Node(6,b) d = LinkedBinaryTree.Node(8,c) e = LinkedBinaryTree.Node(10,d) f = LinkedBinaryTree.Node(12, e) tree=LinkedBinaryTree(f) for i in tree: print(i) print(min_and_max(tree)) for i in tree: print(i)
from LinkedBinaryTree import LinkedBinaryTree p1 = LinkedBinaryTree.Node(6) p2 = LinkedBinaryTree.Node(4) p3 = LinkedBinaryTree.Node(2, p1, p2) p4 = LinkedBinaryTree.Node(3) p5 = LinkedBinaryTree.Node(5, p3, p4) p6 = LinkedBinaryTree.Node(0) p7 = LinkedBinaryTree.Node(1, p6, p5) tree = LinkedBinaryTree(p7) # for i in tree: # print(i) def invert_binary_tree(bin_tree): if bin_tree is None: raise Expception('Tree is empty') else: return LinkedBinaryTree(invert_subtree(bin_tree.root)) def invert_subtree(subtree_root): position = subtree_root if position.right is None and position.left is None: return position elif position.left is None and position.right is not None: right = invert_subtree(position.right) return LinkedBinaryTree.Node(subtree_root.data, right, None) elif position.left is not None and position.right is None: left = invert_subtree(position.left)
from LinkedBinaryTree import LinkedBinaryTree node4a = LinkedBinaryTree.Node(4) node1a = LinkedBinaryTree.Node(1) node3b = LinkedBinaryTree.Node(3,node4a,node1a) node1b = LinkedBinaryTree.Node(1) node6 = LinkedBinaryTree.Node(6,right = node1b) node4b = LinkedBinaryTree.Node(4,node3b,node6) tree = LinkedBinaryTree(node4b) #tree.draw() def is_sum_balanced(bin_tree): boolean,value = is_subtree_sum_balanced(bin_tree.root) return boolean def is_subtree_sum_balanced(subtree_root): if subtree_root is None: return True,0 left_bool, left_value = is_subtree_sum_balanced(subtree_root.left) right_bool, right_value = is_subtree_sum_balanced(subtree_root.right) value = left_value + right_value + subtree_root.data boolean = False if (left_value - right_value)**2 <= 1: boolean = True return left_bool and boolean and right_bool , value #print(is_sum_balanced(tree)) from Stack import ArrayStack
def main(): bst = BinarySearchTreeMap() bst.insert(2) bst.insert(12) bst.insert(1) bst.insert(3) bst.insert(9) bst.insert(21) bst.insert(19) bst.insert(25) r_1 = LinkedBinaryTree.Node(5) l_2 = LinkedBinaryTree.Node(4, None, r_1) r_2 = LinkedBinaryTree.Node(10) r_3 = LinkedBinaryTree.Node(8, None, r_2) l_3 = LinkedBinaryTree.Node(6, l_2, r_3) l_1 = LinkedBinaryTree.Node(31) r_1 = LinkedBinaryTree.Node(49) r_2 = LinkedBinaryTree.Node(43, l_1, r_1) l_2 = LinkedBinaryTree.Node(17) r_3 = LinkedBinaryTree.Node(19, l_2, r_2) root = LinkedBinaryTree.Node(11, l_3, r_3) bt = LinkedBinaryTree(root) node1 = bst.root.right node2 = bst.root.right print(lca_BST(bst, node1, node2).item.key)
from LinkedBinaryTree import LinkedBinaryTree node4 = LinkedBinaryTree.Node(4) node1 = LinkedBinaryTree.Node(1) node8 = LinkedBinaryTree.Node(8) node6 = LinkedBinaryTree.Node(6, node4, node1) node7 = LinkedBinaryTree.Node(7) node4b = LinkedBinaryTree.Node(4, node7, node6) node10 = LinkedBinaryTree.Node(10) node5 = LinkedBinaryTree.Node(5, None, node10) node2 = LinkedBinaryTree.Node(2, node4b, node5) node19 = LinkedBinaryTree.Node(19, node8) node13 = LinkedBinaryTree.Node(13) node9 = LinkedBinaryTree.Node(9, node19, node13) node5b = LinkedBinaryTree.Node(5, None, node9) node1 = LinkedBinaryTree.Node(1, node2, node5b) tree = LinkedBinaryTree(node1) tree.draw() def level_list(root, level): if root is None: return [] if level == 0: return [root.data] left = level_list(root.left, level - 1) right = level_list(root.right, level - 1) return left + right #print(level_list(tree.root,6))
def main(): l3 = LinkedBinaryTree.Node(1) l2 = LinkedBinaryTree.Node(2, l3, None) l4 = LinkedBinaryTree.Node(1) r4 = LinkedBinaryTree.Node(1) l3 = LinkedBinaryTree.Node(3, l4, r4) l5 = LinkedBinaryTree.Node(1) r5 = LinkedBinaryTree.Node(1) r4 = LinkedBinaryTree.Node(3, l5, r5) r3 = LinkedBinaryTree.Node(4, None, r4) r2 = LinkedBinaryTree.Node(12, l3, r3) root = LinkedBinaryTree.Node(11, l2, r2) bt = LinkedBinaryTree() bt.root = root ''' pq = ExtendedPartiesQueue() pq.enq_party("Jeff", 3) print(pq) pq.enq_party("Mike", 5) print(pq) pq.enq_party("Nick", 2) print(pq) pq.deq_first_party() print(pq) pq.enq_party("Jessica", 4) print(pq) pq.add_to_party("Nick", 2) print(pq) pq.deq_first_party() print(pq) ''' print(level_list(bt.root, 5))
from LinkedBinaryTree import LinkedBinaryTree def binary_tree_even_sum(root): ''' Returns the sum of all even integers in the binary tree''' if (root is None): return 0 else: count1 = binary_tree_even_sum(root.left) count2 = binary_tree_even_sum(root.right) if (root.data % 2 == 0): even_num = root.data return even_num + count1 + count2 else: return count1 + count2 a = LinkedBinaryTree.Node(5) b = LinkedBinaryTree.Node(4) c = LinkedBinaryTree.Node(6, a, b) d = LinkedBinaryTree.Node(8) e = LinkedBinaryTree.Node(10, None, d) f = LinkedBinaryTree.Node(12, e, c) mytree = LinkedBinaryTree(f) print(binary_tree_even_sum(f))
def main(): ''' Define a normal binary tree ''' r_1 = LinkedBinaryTree.Node(5) l_2 = LinkedBinaryTree.Node(4, None, r_1) r_2 = LinkedBinaryTree.Node(10) r_3 = LinkedBinaryTree.Node(8, None, r_2) l_3 = LinkedBinaryTree.Node(6, l_2, r_3) l_1 = LinkedBinaryTree.Node(31) r_1 = LinkedBinaryTree.Node(49) r_2 = LinkedBinaryTree.Node(43, l_1, r_1) l_2 = LinkedBinaryTree.Node(17) r_3 = LinkedBinaryTree.Node(19, l_2, r_2) root = LinkedBinaryTree.Node(11, l_3, r_3) bt = LinkedBinaryTree(root) ''' Define a full(proper) binary tree ''' l = LinkedBinaryTree.Node(9) r = LinkedBinaryTree.Node(11) l = LinkedBinaryTree.Node(1, l, r) r = LinkedBinaryTree.Node(4) r = LinkedBinaryTree.Node(6, l, r) l = LinkedBinaryTree.Node(3) root = LinkedBinaryTree.Node(7, l, r) full_bt = LinkedBinaryTree(root) ''' Define a complete binary tree ''' l1_1 = LinkedBinaryTree.Node(14) r1_1 = LinkedBinaryTree.Node(5) l1_2 = LinkedBinaryTree.Node(12) r1_2 = LinkedBinaryTree.Node(7) l1_3 = LinkedBinaryTree.Node(8) r1_3 = LinkedBinaryTree.Node(15) l1_4 = LinkedBinaryTree.Node(1) r1_4 = LinkedBinaryTree.Node(4) l2_1 = LinkedBinaryTree.Node(13, l1_1, r1_1) r2_1 = LinkedBinaryTree.Node(9, l1_2, r1_2) l2_2 = LinkedBinaryTree.Node(3, l1_3, r1_3) r2_2 = LinkedBinaryTree.Node(6, l1_4, r1_4) l3 = LinkedBinaryTree.Node(2, l2_1, r2_1) r3 = LinkedBinaryTree.Node(11, l2_2, r2_2) root = LinkedBinaryTree.Node(10, l3, r3) complete_bt = LinkedBinaryTree(root)
##print(lnk) #Problem 3 from LinkedBinaryTree import LinkedBinaryTree def count_val(root,value): if root is None: return 0 count = 0 if root.data == value: count += 1 count += count_val(root.left,value) count += count_val(root.right,value) return count node5 = LinkedBinaryTree.Node(5) node6 = LinkedBinaryTree.Node(6,node5) node9 = LinkedBinaryTree.Node(9) node7 = LinkedBinaryTree.Node(7,node6,node9) node1 = LinkedBinaryTree.Node(1) node2 = LinkedBinaryTree.Node(2,node1) node4 = LinkedBinaryTree.Node(4,node2,node7) tree = LinkedBinaryTree(node4) ##print(count_val(tree.root,4)) #Problem 4 def invert_binary_tree2(bin_tree): def invert_helper(root):