def post_order_dft(self, node): order_stack = Stack() order_stack.push(node) print_stack = Stack() while order_stack.len() > 0: current_node = order_stack.pop() print_stack.push(current_node) if current_node.left: order_stack.push(current_node.left) if current_node.right: order_stack.push(current_node.right) while print_stack.len() > 0: print(print_stack.pop().value) # test_bst = BinarySearchTree(1) # test_bst.insert(8) # test_bst.insert(5) # test_bst.insert(7) # test_bst.insert(6) # test_bst.insert(3) # test_bst.insert(4) # test_bst.insert(2) # test_bst.in_order_print(test_bst) # test_bst.bft_print(test_bst)
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 dft_print(self, node): # If there is a node if node: # Save the current node in current current = node # Create a stack my_stack = Stack() # While (current is not None) or (my_stack.len is not 0) while current or my_stack.len() != 0: # if current is not empty if current: # Add current to the stack ( push ) my_stack.push(current) # print the value of current print(current.value) # Make current.left the new current node # So we can keep going left current = current.left # Else: If current is empty else: # pop from the stack the last node added # Save that node in popped_node popped_node = my_stack.pop() # Save popped_node.right as current # So we can check if popped_node.right is # empty in the next while loop current = popped_node.right
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 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 dft_print(self, node): to_print = Stack() to_print.push(node) while to_print.len() > 0: popped_node = to_print.pop() print(popped_node.value) popped_node.right and to_print.push(popped_node.right) popped_node.left and to_print.push(popped_node.left)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.len() > 0: current = stack.pop() print(current.value) current.left and stack.push(current.left) current.right and stack.push(current.right)
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, *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(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, node): storage = Stack() storage.push(self) while storage.len() > 0: current = storage.pop() print(current.value) if current.left: storage.push(current.left) if current.right: storage.push(current.right)
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): 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): 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): 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): 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): stack = Stack() stack.push(node) while stack.len() > 0: current_pop = stack.pop() print(current_pop.value) if current_pop.left: stack.push(current_pop.left) if current_pop.right: stack.push(current_pop.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): stack = Stack() stack.push(node) while stack.len() > 0: current = stack.pop() if current.right is not None: stack.push(current.right) if current.left is not None: stack.push(current.left) print(current.value)
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=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): 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): if node: stack = Stack() stack.push(node) while stack.len(): current = stack.pop() if current.right: stack.push(current.right) if current.left: stack.push(current.left) print(current.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): 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): 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): 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): # 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