def _postorder_iter(node, func): from pyds.stack.linkstack import LinkStack stack = LinkStack([]) prior = None while not stack.is_empty or node: if node: stack.push(node) node = node.left elif stack.top().right != prior: node = stack.top().right prior = None else: prior = stack.pop() func(prior.data)
def _preorder_iter_linkstack(node, func): from pyds.stack.linkstack import LinkStack stack = LinkStack([]) stack.push(node) while not stack.is_empty: current_node = stack.pop() func(current_node.data) if current_node.right: stack.push(current_node.right) if current_node.left: stack.push(current_node.left)