Category : binary-tree

I am looking for implementing BFS (Breadth-First Search) of binary tree in Python using Stack (not Queue!). class Stack: def __init__(self): self.data = [] def Empty(self): return self.data == [] def Push(self, x): self.data.append(x) def Pop(self): return self.data.pop() def Peek(self): return self.data[len(self.data)-1] def Size(self): return len(self.data) class Node: def __init__(self, data, left, right): self.data = ..

Read more

from tree import Node, leaf from expr_parser import parse from typing import Optional def opt_times_two(e:Optional[Node])->Optional[Node]: if e.left != e.right: return None if e.mark == "+": if e.left == e.right: return #fill in assert opt_times_two(parse("x + y")) == None assert opt_times_two(parse("x + (x + x)")) == None assert opt_times_two(parse("x + x")) == parse("2 * x") So ..

Read more

from tree import Node, leaf def is_var(tree:Node)->bool: if type(tree.mark) == str: return True elif tree.mark == "+" or tree.mark == "*": return False def is_val(tree:Node)->bool: if type(tree.mark) == int: return True def show_node(tree:Node)->str: if is_var(tree) or is_val(tree): return str(tree.mark) else: #fill in example = Node("+",Node("*", leaf(2), # + leaf("x")), # / leaf(5)) # * 5 ..

Read more

I am looking to get Tree paths with required_sum 23: [[12, 7, 4], [12, 1, 10]] But instead I am getting Tree paths with required_sum 23: [[], []] my code is as follows… def findPaths(root, val): allPaths = [] findPathsHelper(root, sum, [], allPaths) return allPaths def findPathsHelper(currentNode, sum, currentPath, allPaths): if currentNode is None: return ..

Read more

I have a dictionary of words containing 370100 elements (strings). I need to add them into a binary tree to sort and find them. How would I do this without hitting python’s max iterations? I tried using sys.setrecursionlimit but it was slow and eventually spit out zsh: segmentation fault python tree.py. import sys class Node(object): ..

Read more