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(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.left.right = Node(8) root.right.left = Node(5) root.right.right = Node(6) return root
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(): 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, 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 using_dummy_node(head1, head2): dummy = Node(None) tail = dummy while head1 and head2: print(head1, head2, tail, dummy) if head1.value <= head2.value: tail.next = head1 tail = tail.next head1 = head1.next else: tail.next = head2 tail = tail.next head2 = head2.next while head1: tail.next = head1 tail = tail.next head1 = head1.next while head2: tail.next = head2 tail = tail.next head2 = head2.next dummy = dummy.next return dummy
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 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 enqueue(val, tail): node = Node(val) if tail is None: tail = node else: tail.next = node tail = tail.next return tail
def clone(head): clonehead = None clonehead1 = Node tmp = head while tmp: if clonehead is None: clonehead = Node(tmp.value) tmp.clonenode = clonehead clonehead1 = clonehead else: clonehead.next = Node(tmp.value) clonehead = clonehead.next tmp.clonenode = clonehead tmp = tmp.next # tmp2 = head # while tmp2: # print(tmp2, tmp2.clonenode, tmp2.random) # tmp2 = tmp2.next tmp = head while tmp: print(tmp, tmp.clonenode, tmp.random) clonednode = tmp.clonenode rnode = tmp.random if rnode: clonednode.random = rnode.clonenode #tmp.clonenode = None tmp = tmp.next tmp2 = clonehead1 while tmp2: print(tmp2 , tmp2.random) tmp2 = tmp2.next
def create_ll(): head = Node(10) tmp = head tmp.next = Node(12) tmp = tmp.next tmp.next = Node(15) tmp = tmp.next tmp.next = Node(25) tmp = tmp.next tmp.next = Node(30) tmp = tmp.next tmp.next = Node(36) return head
def ll_to_bt(head, i): queue = [] node = Node(head.value) queue.append(node) while len(queue) > 0: tmp = queue[0] queue.remove(tmp) left = get_left(head, i) right = get_right(head, i) print(tmp, left, right) if left: tmp.left = left queue.append(left) if right: tmp.right = right queue.append(right) i += 1 return node
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 head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5) stack = [] while head: stack.append(head) head = head.next p = stack.pop() head = p while len(stack) > 0: e = stack.pop() p.next = e p = e p.next = None t = head while t: print(t) t = t.next c = head p = None n = None
def set(k, t, v, map): eval = map.get(k) print(eval) if eval is None: head = Node(t) arr = [] heapq.heappush(arr, v) head.set_heap_ref(arr) map[k] = head else: node = eval inserted = False prev = None while node: if node.value == t: arr = node.heapref heapq.heappush(arr, v) inserted = True break elif node.prev and node.prev.value < t and node.next and node.next.value > t: tmp = Node(t) arr = [] heapq.heappush(arr, v) tmp.set_heap_ref(arr) prev = node.prev next = node.next prev.next = tmp tmp.prev = prev tmp.next = next next.prev = tmp inserted = True break else: prev = node node = node.next if not inserted: if t > prev.value: print('inserting at ', t) tmp = Node(t) arr = [] heapq.heappush(arr, v) tmp.set_heap_ref(arr) prev.next = tmp tmp.prev = prev else: print('inserting1 at ', t, prev) tmp = Node(t) arr = [] heapq.heappush(arr, v) tmp.set_heap_ref(arr) prev.prev.next = tmp tmp.prev = prev.prev tmp.next = prev prev.prev = tmp prev.next = None
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 head = Node(4) head.next = Node(1) head.next.next = Node(-3) head.next.next.next = Node(9) head.next.next.next.next = Node(2) head2 = Node(10) head2.next = Node(80) head2.next.next = Node(15) head2.next.next.next = Node(20) head2.next.next.next.next = Node(40) head2.next.next.next.next.next = Node(70) head2.next.next.next.next.next.next = Node(30) # 4 -> 1 -> -3 -> 9 -> 2 def print_ll(head): lst = [] while head: lst.append(str(head.value)) lst.append(' -> ') head = head.next return "".join(lst) def get_tail(start, partition): while start and start.next != partition: start = start.next
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)])
def inorder_successor(key): 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)
from src.main.prep.node import Node head1 = Node(1) head1.next = Node(2) head1.next.next = Node(3) head1.next.next.next = Node(4) head1.next.next.next.next = Node(5) head2 = Node(8) head2.next = Node(9) head2.next.next = Node(8) head2.next.next.next = Node(6) head2.next.next.next.next = Node(7) head3 = None remainder = 0 tmp = None prev = 0 while head1 and head2: s = head1.value + head2.value + remainder prev = s print(remainder, s) if s >=10: remainder = s//10 s = s%10 else: remainder = 0 if head3 is None: head3 = Node(s)
max_sum_path(root.right, path) 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)
def ll_to_bt(head, i): queue = [] node = Node(head.value) queue.append(node) while len(queue) > 0: tmp = queue[0] queue.remove(tmp) left = get_left(head, i) right = get_right(head, i) print(tmp, left, right) if left: tmp.left = left queue.append(left) if right: tmp.right = right queue.append(right) i += 1 return node head = create_ll() #print_ll(head) root = Node(head.value) root = ll_to_bt(head, 0) print(root, root.left, root.right, root.left.left, root.left.right, root.right.left)
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 print_ll(h): lst = [] while h: lst.append(h) h = h.next print(lst) head1 = Node(5) head1.next = Node(8) head1.next.next = Node(10) head2 = Node(4) head2.next = Node(9) head2.next.next = Node(15) print_ll(head1) print_ll(head2) def using_dummy_node(head1, head2): dummy = Node(None) tail = dummy while head1 and head2: print(head1, head2, tail, dummy) if head1.value <= head2.value: tail.next = head1
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
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)
root = Node(1) root.left = Node(2) root.right = Node(3) root.left.right = Node(5) return root def is_identical(root1, root2, lst): print(root1, root2) if not root1 and not root2: return if root2 and not root1: lst.append(False) if root1 and not root2: lst.append(False) if root1.value == root2.value: lst.append(True) is_identical(root1.left, root2.left, lst) is_identical(root1.right, root2.right, lst) else: lst.append(False) root1 = create_tree() root2 = create_tree() root2.left =Node(9) lst = [] is_identical(root1, root2, lst) print(lst)
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)