def dft_print(self, node): """ Iterative approach notes from lecture Make a stack add root to stack while stack.length > 0 immediately pop root and save to a temp var (temp = stack.pop()) Do the thing if temp.left add to stack if temp.right add to stack """ # instantiating stack level_stack = Stack() # pushing initial node to stack level_stack.push(node) # while something is in the stack while level_stack.len() > 0: # doing this after when enter loop, otherwise it will be 0 and it won't start the loop current_node = level_stack.pop() print(current_node.value) # if there is a node to the right, push that onto the stack if current_node.right: level_stack.push(current_node.right) # if there is a node to the left, push that onto the stack if current_node.left: level_stack.push(current_node.left)
def dft_print(self, node=None): my_stack = Stack() if node == None: current_node = self else: current_node = node my_stack.push(current_node) while my_stack.len() > 0: current_node = my_stack.pop() print(current_node.value) if current_node.right: # exists: my_stack.push(current_node.right) if current_node.left: my_stack.push(current_node.left) # put self on stack # current = pop from stack # print current # put current.right on stack # put current.left on stack. # repeat # # use a stack pass
def dft_print(self, node): #PLAN #create/initialize stack #add/push root to stack #while stack is not empty #pop head/top item out of stack & into temp variable [node = pop head of stack] #if temp exists # if there is temp var on the right #put that into the stack on the right # if there is a left temp var #put that into the stack on the left # DO the THING!... print temp value # else break stack = Stack() stack.push(node) while stack.len() > 0: temp: BinarySearchTree = stack.pop() if temp: if temp.right: stack.push(temp.right) if temp.left: stack.push(temp.left) print(temp.value) else: break
def for_each(self, cb): # if not self.value: # return None # cb(self.value) # if self.left: # self.left.for_each(cb) # if self.right: # self.right.for_each(cb) # cb(self.value): # if self.left: # self.left.for_each(cb) # if self.right: # seelf.right.for_each(cb) stack = Stack() stack.push(self) while stack.len() > 0: current_node = stack.pop() if current_node.right: stack.push(current_node.right) if current_node.left: stack.push(current_node.left) cb(current_node.value)
def __init__(self, value): self.value = value self.left = None self.right = None self.queue = Queue() self.stack = Stack() self.lastPrintedValue = -math.inf
def __init__(self, value): # set the value at the current node self.value = value # add ref to left child node self.left = None # add ref to the right child node self.right = None self.stack = Stack()
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len() > 0: current = stack.pop() print(current.value) if current.left: stack.push(current.left) if current.right: stack.push(current.right)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len() > 0: root = stack.pop() print(root.value) if root.left != None: stack.push(root.left) if root.right != None: stack.push(root.right)
def dft_print(self, *args): s = Stack() s.push(self) while s.len(): node = s.pop() print(node.value) if node.left: s.push(node.left) if node.right: s.push(node.right)
def dft_print(self, node): stack = Stack() stack.push(self) while stack.len() > 0: node = stack.pop() print(node.value) if node.left: stack.push(node.left) if node.right: stack.push(node.right)
def dft_print(self, node): s = Stack() s.push(node) while s.len() != 0: node = s.pop() print(node.value) if node.left: s.push(node.left) if node.right: s.push(node.right)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len(): current_node = stack.pop() print(current_node.value) # d if current_node.left: stack.push(current_node.left) # l if current_node.right: stack.push(current_node.right) # r
def dft_print(self, node): s = Stack() s.push(node) while s.len() > 0: t = s.pop() print(t.value) if t.right: s.push(t.right) if t.left: s.push(t.left)
def dft_print(self, node): dft_stack = Stack() dft_stack.push(node) while dft_stack.len() > 0: element = dft_stack.pop() print(element.value) if element.right: dft_stack.push(element.right) if element.left: dft_stack.push(element.left)
def dft_print(self, node): storage = Stack() storage.push(node) while storage.len(): current_node = storage.pop() print(current_node.value) if current_node.left: storage.push(current_node.left) if current_node.right: storage.push(current_node.right)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len() > 0: temp = stack.pop() print(temp) if temp.left: stack.push(temp.left) if temp.right: stack.push(temp.right)
def dft_print(self, node=None): st = Stack() st.push(self) while st.len() > 0: node_current = st.pop() print(node_current.value) if node_current.left: st.push(node_current.left) if node_current.right: st.push(node_current.right)
def dft_print(self, node): s = Stack() s.push(self) while s.len() > 0: current_node = s.pop() if current_node.left: s.push(current_node.left) if current_node.right: s.push(current_node.right) print(current_node.value)
def dft_print(self, node): s = Stack() s.push(self.value) while s: s.pop() print(self.value) if (self.left): s.push(self.left.value) if (self.right): s.push(self.right.value)
def dft_print(self, node): # iterative stack = Stack() stack.push(self) while stack.len() > 0: current_node = stack.pop() print(current_node.value) if current_node.left: stack.push(current_node.left) if current_node.right: stack.push(current_node.right)
def dft_print(self, node): # make stack self.stack = Stack() self.stack.push(node) while self.stack.len() > 0: node = self.stack.pop() if node.left: self.stack.push(node.left) if node.right: self.stack.push(node.right) print(node.value)
def dft_print(self, node): my_stack = Stack() my_stack.push(node) while my_stack.len() > 0: new_node = my_stack.pop() print(new_node.value.value) if new_node.value.left: my_stack.push(new_node.value.left) if new_node.value.right: my_stack.push(new_node.value.right)
def dft_print(self, node): self.dft_stack = Stack() self.dft_stack.push(node) while self.dft_stack.len() > 0: item = self.dft_stack.pop() print(item.value) if item.left: self.dft_stack.push(item.left) if item.right: self.dft_stack.push(item.right)
def dft_print(self, node): # use a stack add and remove from head stack = Stack() # set up an empty stack stack.push(node) # push a node onto the stack while stack.len() > 0: # as long as there are nodes in the stack current = stack.pop() # pop off from the top of the stack print(current.value) if current.left: # check if the node has a left child # if so, push that child onto the stack stack.push(current.left) if current.right: # now check if that node has a right child stack.push(current.right) # if so, push that onto the stack
def dft_print(self, node): pile = Stack() pile.push(node) while pile.len() > 0: current = pile.pop() print(current.value) if current.left: pile.push(current.left) if current.right: pile.push(current.right)
def dft_print(self, node): stack = Stack() temp_node = None stack.push(node) while stack.len() > 0: temp_node = stack.pop() print(temp_node.value) if temp_node.right: stack.push(temp_node.right) if temp_node.left: stack.push(temp_node.left)
def dft_print(self, node): if node: stack = Stack() stack.push(node) while stack.len(): temp = stack.pop() print(temp.value) if temp.right: stack.push(temp.right) if temp.left: stack.push(temp.left)
def dft_print(self, node): s = Stack() s.push(starting_node) while s.len() > 0: current = s.pop() print(current.value) if current.left: s.push(current.left) if current.right: s.push(current.right)
def dft_print(self, node): print_stack = Stack() print_stack.push(node) while print_stack.len() > 0: current_node = print_stack.pop() print(current_node.value) if current_node.left: print_stack.push(current_node.left) if current_node.right: print_stack.push(current_node.right)
def dft_print(self, node): s = Stack() s.push(node) while s.len() > 0: pop = s.pop() print(pop.value) if pop.right is not None: s.push(pop.right) if pop.left is not None: s.push(pop.left)