def create_tree(): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.right = Node(5) return root
def create_tree(): node = Node(10) node.left = Node(20) node.right = Node(30) node.left.right = Node(50) node.right.left = Node(60) return node
def create_tree(): root = Node(10) root.left = Node(20) root.right = Node(30) root.left.left = Node(40) root.left.right = Node(50) return root
def invert(root, invertroot): if not root: return invertroot = Node(root.value) invertroot.left = invert(root.right, invertroot) invertroot.right = invert(root.left, invertroot) return invertroot
def create_tree(): root = Node(10) root.left = Node(5) root.right = Node(20) root.left.left = Node(3) root.left.right = Node(7) root.right.left = Node(15) root.right.right = Node(25) return root
def create_mirror(root): if not root: return node = Node(root.value) node.left = create_mirror(root.right) node.right = create_mirror(root.left) return node
def create_tree(): root = Node(10) root.left = Node(12) root.right = Node(15) root.right.left = Node(36) root.left.left = Node(25) root.left.right = Node(30) return root
def create_tree(): root = Node('+') root.left = Node('*') root.right = Node('-') root.left.left = Node(5) root.left.right = Node('-') root.left.right.left = Node(30) root.left.right.right = Node(50) root.right.left = Node(100) root.right.right = Node(20) return root
def create_tree(root, inord): global postord print(inord, postord) if not inord or len(inord) == 0: return val = postord[len(postord) - 1] root = Node(val) index = inord.index(val) postord = postord[:len(postord) - 1] root.right = create_tree(root, inord[index + 1:]) root.left = create_tree(root, inord[:index]) return root
def create_tree(root, ino): global preo # print(ino, preo) if len(ino) == 0: return val = preo[0] root = Node(val) inoi = ino.index(val) preo = preo[1:] root.left = create_tree(root, ino[0:inoi]) root.right = create_tree(root, ino[inoi + 1:]) return root
def create_bst(arr, root, tmp): if len(arr) == 0: return if root is None: root = Node(arr[0]) tmp.append(root) create_bst(arr[1:], root, tmp) else: v = arr[0] if v <= root.value: if not root.left: root.left = Node(v) create_bst(arr[1:], root, tmp) else: create_bst(arr, root.left, tmp) else: if not root.right: root.right = Node(v) create_bst(arr[1:], root, tmp) else: create_bst(arr, root.right, tmp)
from src.main.prep.node import Node root = Node(0) root.left = Node(0) root.right = Node(0) root.right.right = Node(0) root.right.left = Node(0) root.right.left.right = Node(0) root.right.left.left = Node(1) def find_all_zero(node, lst) : if not node: return if node.value == 1: lst.append(1) find_all_zero(node.left, lst) find_all_zero(node.right, lst) def prune(root, parent, lst): if not root: return tmp = [] find_all_zero(root, tmp) print(tmp)
from src.main.prep.node import Node root = Node(1) root.right = Node(2) root.right.left = Node(4) root.left = Node(-2) root.left.left = Node(3) root.left.left.left = Node(4) root.left.right = Node(5) root.left.right.left = Node(2) root.left.right.right = Node(1) k = 6 def find_path_to_sum(k, root, path): if not root: return path.append(root.value) find_path_to_sum(k, root.left, path) find_path_to_sum(k, root.right, path) print('path: ', path) s = 0 for i in range(len(path) - 1, -1, -1): s += path[i] if s == k: print(path[i:len(path)])
print('left is bst ', root, pathl) else: print('find largest bst at ', root.left) find_largest_bst(root.left) if isrightbst: pathr.append(root) print('right is bst ', root, pathr) else: print('find largest bst at ', root.right) find_largest_bst(root.right) root = Node(50) root.left = Node(20) node13 = Node(18) root.left.left = node13 node13.left = Node(8) node13.left.left = Node(5) node13.left.left.left = Node(3) node13.left.right = Node(10) node13.left.right.left = Node(9) node13.right = Node(27) node13.right.right = Node(37) root.left.right = Node(28) root.right = Node(75) root.right.left = Node(65) root.right.right = Node(85)
from src.main.prep.node import Node root = Node(3) root.left = Node(9) root.right = Node(20) root.right.left = Node(15) root.right.right = Node(7) queue = [] queue.append(root) bit = 0 while len(queue) > 0: tmp = [] while len(queue) > 0: n = queue[0] queue.remove(n) print(n) if bit: if n.left: tmp.append(n.left) if n.right: tmp.append(n.right) else: if n.right: tmp.append(n.right) if n.left:
from src.main.prep.node import Node def find_node(root, k, count, hashmap): if not root: return find_node(root.left, k, count, hashmap) if root.value in hashmap: print(hashmap[root.value], root.value) return else: hashmap[k - root.value] = root.value find_node(root.right, k, count, hashmap) root = Node(10) root.left = Node(5) root.right = Node(15) root.right.left = Node(11) root.right.right = Node(15) hashmap = {} find_node(root, 20, 0, hashmap) print(hashmap)
from src.main.prep.node import Node root = Node('a') root.left = Node('b') root.right = Node('c') root.left.left = Node('d') root.left.right = Node('e') root.right.left = Node('f') def invert(root, invertroot): if not root: return invertroot = Node(root.value) invertroot.left = invert(root.right, invertroot) invertroot.right = invert(root.left, invertroot) return invertroot iroot = invert(root, None) print(iroot, iroot.left, iroot.right, iroot.left.left, iroot.left.right, iroot.right.left, iroot.right.right)
from src.main.prep.node import Node from src.main.prep.counter import Boolean root = Node(1) root.left = Node(2) root.right = Node(3) root.left.right = Node(4) root.right.right = Node(5) root1 = Node(1) root1.left = Node(2) root1.right = Node(3) root1.left.right = Node(4) root1.right.right = Node(5) root3 = Node(10) root3.left = Node(11) root3.right = Node(21) root3.left.right = root1 def check_if_same_tree(root1, root2, boolean): if root1 and not root2: return if root2 and not root1: return if not root1 and not root2: boolean.set_value(True)
print(key) if not key: return if key.right is not None: obj = Object() inorder_left(key.right, obj) return obj else: obj = Object() go_up(key, key.parent, obj) return obj root = Node(50) root.left = Node(30) root.left.parent = root root.left.right = Node(40) root.left.right.parent = root.left root.left.right.right = Node(45) root.left.right.right.parent = root.left.right root.right = Node(70) root.right.parent = root root.right.left = Node(60) root.right.left.parent = root.right root.right.left.left = Node(55) root.right.left.left.parent = root.right.left
from src.main.prep.node import Node #5, 1, 14, 4, 15, 9, 7, 20, 11 root = Node(5) root.left = Node(1) root.right = Node(14) root.left.right = Node(4) root.right.left = Node(9) root.right.left.left = Node(7) root.right.left.right = Node(11) root.right.right = Node(15) root.right.right.right = Node(20) def find_length(root, k, lst): if not root: lst.append(k) return max(lst) l = find_length(root.left, k + 1, lst) r = find_length(root.right, k + 1, lst) return max(l, r) def find_rank(root, key): if not root: return
def for_each_node(root): if not root: return pathl = [] max_sum_path(root, pathl) print(pathl, sum(pathl)) for_each_node(root.left) for_each_node(root.right) root = Node(-15) root.left = Node(5) root.right = Node(6) root.left.left = Node(-8) root.left.left.left = Node(2) root.left.left.right = Node(6) root.left.right = Node(1) root.right.left = Node(3) root.right.right = Node(9) root.right.right.right = Node(0) root.right.right.right.right = Node(-1) root.right.right.right.left = Node(4) root.right.right.right.right.left = Node(10) #for_each_node(root)
from src.main.prep.node import Node root = Node(7) root.left = Node(4) root.right = Node(11) root.left.left = Node(3) root.left.right = Node(6) root.right.left = Node(10) #root.right.left.left = Node(12) root.right.right = Node(15) def validate_bst(root): if not root: return if root.left: if root.left.value <= root.value: validate_bst(root.left) else: raise Exception('invalid bst in left', root.value, root.left.value) if root.right: if root.right.value > root.value: validate_bst(root.right) else: raise Exception('invalid bst in right', root.value,
from src.main.prep.node import Node def lock_node(node): if not node: return if not node.left and not node.right: return node.lock else: left_lock = lock_node(node.left) right_lock = lock_node(node.right) if left_lock == False and right_lock == False: node.lock = True return True else: return False root = Node(1) root.left = Node(2) root.right = Node(3) root.right.left = Node(4) root.right.right = Node(3) print(lock_node(root))