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 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 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(): a = LinkedBinaryTree().Node(5, None, None) b = LinkedBinaryTree().Node(1, None, None) c = LinkedBinaryTree().Node(8, None, None) d = LinkedBinaryTree().Node(4, None, None) e = LinkedBinaryTree().Node(9, a, b) f = LinkedBinaryTree().Node(2, e, None) g = LinkedBinaryTree().Node(7, c, d) h = LinkedBinaryTree().Node(3, f, g) bin_tree = LinkedBinaryTree(h) i = min_and_max(bin_tree) #pass in the root node for the whole tree print(i)
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 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 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 clone(t, p): tree = LinkedBinaryTree() tree._add_root(p._node._element) if t.num_children(p) == 2: left = clone(t, t.left(p)) right = clone(t, t.right(p)) tree._attach(tree.root(), left, right) return tree
def invert_binary_tree(bin_tree): 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 r = invert_helper(bin_tree.root) tree = LinkedBinaryTree(r) return tree
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_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(): 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)
""" Let T be a binary tree with n positions. Define a Roman position to be a position p in T, such that the number of descendants in p’s left subtree differ from the number of descendants in p’s right subtree by at most 5. Describe a linear-time method for finding each position p of T, such that p is not a Roman position, but all of p’s descendants are Roman. defining not linear time algorithm would be a challenge! """ from LinkedBinaryTree import LinkedBinaryTree from other import intented_parenthetic t = LinkedBinaryTree() n0 = t._add_root(0) n1 = t._add_left(n0, 1) n2 = t._add_right(n0, 2) n3 = t._add_left(n1, 3) n4 = t._add_right(n1, 4) # e = t._add_left(d, 5) """ 0 2 0 0 0 """ ROMAN_NUMBER = 1 def find_romanians(tree, p): if tree.is_leaf(p): return True, 0
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 def count_left_children(t, p): s = 0 if t.is_leaf(p): return 0 if t.left(p) is not None: s = 1 + count_left_children(t, t.left(p)) if t.right(p) is not None: s += count_left_children(t, t.right(p)) return s t = LinkedBinaryTree() root = t._add_root(2) l = t._add_left(root, 5) r = t._add_right(root, 7) t._add_left(l, 5) t._add_right(l, 5) t._add_left(r, 5) t._add_right(r, 5) """ x x x x x x x """ assert count_left_children(t, t.root()) == 3 print(count_left_children(t, t.root()))
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))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from EulerTour import EulerTour from LinkedBinaryTree import LinkedBinaryTree class PreorderPrintIndentedTour(EulerTour): def _hook_previsit(self, p, d, path): print(2 * d * " " + str(p, element())) if __name__ == "__main__": LBT = LinkedBinaryTree()
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))
from LinkedBinaryTree import LinkedBinaryTree from BinarySearchTree import BinarySearchTree tree = LinkedBinaryTree() print("Tree ->", tree) rp = tree.add_root("*") print("RP container ", rp.container) rlp = tree.add_left(rp, "+") rrp = tree.add_right(rp, "-") tree.add_left(rlp, "a") tree.add_right(rlp, "b") tree.add_left(rrp, "c") tree.add_right(rrp, "d") btree = BinarySearchTree() rp = btree.add_root(1) rlp = btree.add_left(rp, 2) rrp = btree.add_right(rp, 3) rllp = btree.add_left(rlp, 4)
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))
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 __init__(self, elements): self.elements = elements self.tree = LinkedBinaryTree() # 初始化一颗空树
def clone(tree1): tree2 = LinkedBinaryTree() tree2._add_root(tree1._root._element) _clone(tree1, tree1.root(), tree2, tree2.root()) return tree2 def _clone(tree1, t, tree2, p): if tree1.left(t) is not None: new_left = tree2._add_left(p, tree1.left(t)._node._element) _clone(tree1, tree1.left(t), tree2, new_left) if tree1.right(t) is not None: new_right = tree2._add_right(p, tree1.right(t)._node._element) _clone(tree1, tree1.right(t), tree2, new_right) t = LinkedBinaryTree() root = t._add_root(0) a = t._add_left(root, 1) b = t._add_right(root, 2) c = t._add_left(a, 3) d = t._add_right(a, 4) # e = t._add_left(d, 5) parenthesize(t, t.root()) new = clone(t) print() parenthesize(new, new.root())
f(p), as defined in Section 8.3.2, of each position in a binary tree T. """ from EulerTour import BinaryEulerTour from LinkedBinaryTree import LinkedBinaryTree class LevelTour(BinaryEulerTour): def _hook_invisit(self, p, d, path): ans = 0 for val in path: ans = 2 * ans + val + 1 print(p.element(), ans) t = LinkedBinaryTree() root = t._add_root(0) l = t._add_left(root, 1) r = t._add_right(root, 2) t._add_left(l, 3) t._add_right(l, 4) t._add_left(r, 5) t._add_right(r, 6) """ x x x x x x x """ a = LevelTour(t) a.execute()
class LinkedBinaryTreeTest(object): def __init__(self, elements): self.elements = elements self.tree = LinkedBinaryTree() # 初始化一颗空树 def _add_nodes_test(self): # 节点添加方法测试 print("节点添加方法测试中--------") print("当前树的根结点为:", self.tree.root()) print("添加根节点") self.tree.add(0, self.elements.pop(0)) print("当前树的根结点为:", self.tree.root()) print("添加剩余元素--------") queue = list() queue.append(self.tree.root()) while self.elements: # 添加左边元素 newNode = queue.pop(0) self.tree.add(1, self.elements.pop(0), newNode) queue.append(self.tree.left(newNode)) if self.elements: self.tree.add(2, self.elements.pop(0), newNode) queue.append(self.tree.right(newNode)) # 调用其迭代方法 print("打印整棵树,调用中序遍历--------") for element in self.tree: print(element, end=" ") def _add_nodes_test(self): # 节点添加方法测试 print("节点添加方法测试中--------") print("当前树的根结点为:", self.tree.root()) print("添加根节点") self.tree.add(0, self.elements.pop(0)) print("当前树的根结点为:", self.tree.root()) print("添加剩余元素--------") queue = list() queue.append(self.tree.root()) while self.elements: # 添加左边元素 newNode = queue.pop(0) self.tree.add(1, self.elements.pop(0), newNode) queue.append(self.tree.left(newNode)) if self.elements: self.tree.add(2, self.elements.pop(0), newNode) queue.append(self.tree.right(newNode)) # 调用其迭代方法 print("打印整棵树,调用中序遍历--------") for element in self.tree: print(element, end=" ") def _replace_delete_test(self): # 节点元素替代/删除方法测试 print("节点元素替代方法测试--------") self.tree.replace(self.tree.root(), 9) print("当前树的根节点为9:", self.tree.root().element()) print("节点元素删除方法测试--------") self.tree.delete(self.tree.right(self.tree.left(self.tree.root()))) print("当前树删除节点5") for element in self.tree: print(element, end=" ") def run(self): self._add_nodes_test() self._replace_delete_test()
def clone(tree1): tree2 = LinkedBinaryTree() tree2._add_root(tree1._root._element) _clone(tree1, tree1.root(), tree2, tree2.root()) return tree2
if (p is None): return [0, 0] else: if T.num_children(p) is not 0: left = cal_path_sum(T, T.left(p), n+1) right = cal_path_sum(T, T.right(p), n+1) return [left[0] + right[0] + (left[1] + right[1]) * (p.element() * (10 ** n)), left[1] + right[1]] else: return [p.element() * (10 ** n), 1] #------------Test code------------------------------------------------------------------------- if __name__ == '__main__': #-------------------------- Init a tree for further use -------------------- T = LinkedBinaryTree() r0 = T.add_root(1) r1 = T.add_left(r0,2) r2 = T.add_right(r0,3) r3 = T.add_left(r1,4) r4 = T.add_right(r1,5) r5 = T.add_left(r2,6) r6 = T.add_right(r2,7) r7 = T.add_left(r3,8) r8 = T.add_right(r3,9) r9 = T.add_left(r4,10) r10 = T.add_right(r4,11) print '' T.pretraversal() T.posttraversal() m = cal_path_sum(T, r0, 0)