Preorder traversal in python: suppressing output -
i have tree exists tree object:
class tree: ... class node: ... self.branch_type='node' self.children=[] # list of immediate children ... class leaf: ... self.branch_type='leaf' ...
this representation written else, i'm building on top of.
i've written preorder_traverse() function so:
def preorder_traverse(node, preorder_ls, verbose=false): preorder_ls.append(node) if node.branch_type == "node": if verbose: print(node) preorder_traverse(node.children[0], preorder_ls) preorder_traverse(node.children[1], preorder_ls) elif node.branch_type == "leaf": if verbose: print(node, node.name)
usage:
>>>preorder_ls = [] # init empty list filled >>>preorder_traverse(node, preorder_ls)
this populate preorder_ls
, desired, prints out objects whenever call preorder_traverse
! weird because happens if set verbose
false. there quirk way return
works, or how recursive algorithms called in python i'm missing? i'm looking suppress output being printed screen.
Comments
Post a Comment