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): 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 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 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): # 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): #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): 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): 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): 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): stack = Stack() temp_node = node while temp_node: print(temp_node.value) if temp_node.left: stack.push(temp_node.left) if temp_node.right: stack.push(temp_node.right) temp_node = stack.pop()
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.size > 0: curr_node = stack.pop() print(curr_node.value) if curr_node.left: stack.push(curr_node.left) if curr_node.right: stack.push(curr_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(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): 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): stack = Stack() stack.push(node) while stack is not None: curr_pop = stack.pop() print(curr_pop.value) if curr_pop.left: stack.push(curr_pop.left) if curr_pop.right: stack.push(curr_pop.right)
def dft_print(self, node): stack = Stack() stack.push(node) while stack.size > 0: n = stack.pop() print(n.value) if n.left: stack.push(n.left) if n.right: stack.push(n.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): stack = Stack() stack.push(node) while stack.size > 0: popped = stack.pop() print(popped.value) if popped.left: stack.push(popped.left) if popped.right: stack.push(popped.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): 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): 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, *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): new_stack = Stack() new_stack.push(node) while new_stack.size > 0: current = new_stack.pop() print(current.value) if current.left: new_stack.push(current.left) if current.right: new_stack.push(current.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.size > 0: head = s.pop() print(head.value) if head.left: s.push(head.left) if head.right: s.push(head.right)
def dft_print(self, node): s = Stack() s.push(node) while(len(s)!=0): popped = s.pop() print(popped.value) if popped.left: s.push(popped.left) if popped.right: s.push(popped.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)