def _traverse_level_order_iterative(self, start_node, visit):
        """Traverse this binary tree with iterative level-order traversal (BFS).
        Start at the given node and visit each node with the given function.
        TODO: Running time: ??? Why and under what conditions?
        TODO: Memory usage: ??? Why and under what conditions?"""

        # a queue of unvisited descendant nodes
        queue = LinkedQueue()

        # begin with the start_node
        queue.enqueue(start_node)

        # continue if queue contains any unvisited nodes
        while queue.is_empty() is False:
            # deque and visit the node
            node = queue.dequeue()
            visit(node.data)

            # enqueue the node's children, if exists
            queue.enqueue(node.left) if node.left is not None else None
            queue.enqueue(node.right) if node.right is not None else None
Example #2
0
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Start at the given node and visit each node with the given function.
     Best case running time: O(1) if there is only one item
     Worst case running time: O(n) if there is more than one item"""
     # Create queue to store nodes not yet traversed in level-order
     queue = LinkedQueue()
     # Enqueue given starting node
     queue.enqueue(start_node)
     # Loop until queue is empty
     while not queue.is_empty():
         # Dequeue node at front of queue
         node = queue.dequeue()
         # Visit this node's data with given function
         visit(node.data)
         # Enqueue this node's left child, if it exists
         if node.left:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right:
             queue.enqueue(node.right)
Example #3
0
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Start at the given node and visit each node with the given function.
     TODO: Running time: ??? Why and under what conditions?
     TODO: Memory usage: ??? Why and under what conditions?"""
     # TODO: Create queue to store nodes not yet traversed in level-order
     queue = LinkedQueue()
     # TODO: Enqueue given starting node
     queue.enqueue(start_node)
     # TODO: Loop until queue is empty
     # Loop until queue is empty
     while queue.is_empty() is False:
         # TODO: Dequeue node at front of queue
         node = queue.dequeue()
         # TODO: Visit this node's data with given function
         visit(node.data)
         # TODO: Enqueue this node's left child, if it exists
         if node.left:
             queue.enqueue(node.left)
         # TODO: Enqueue this node's right child, if it exists
         if node.right:
             queue.enqueue(node.right)
Example #4
0
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Start at the given node and visit each node with the given function.
     Running time: o(n) depends on the number of items in the tree        
     TODO: Memory usage: ??? Why and under what conditions?"""
     # Create queue to store nodes not yet traversed in level-order
     # uses a linked Queue for the most efficiency.
     queue = LinkedQueue()
     # Enqueue given starting node
     queue.enqueue(start_node)
     # Loop until queue is empty
     while len(queue) != 0:
         # Dequeue node at front of queue
         node = queue.dequeue()
         # Visit this node's data with given function
         visit(node.data)
         # Enqueue this node's left child, if it exists
         if node.left is not None:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right is not None:
             queue.enqueue(node.right)
Example #5
0
    def items_level_order(self):
        """Return a list of all items in this binary search tree found using
        level-order traversal"""
        # Create a queue to store nodes not yet traversed in level-order
        queue = LinkedQueue()
        # Create an items list
        items = list()

        if not self.is_empty():
            queue.enqueue(self.root)

        while queue.is_empty() is False:
            node = queue.dequeue()
            items.append(node.data)

            if node.left is not None:
                queue.enqueue(node.left)

            if node.right is not None:
                queue.enqueue(node.right)
        # Return the items list
        return items
Example #6
0
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Start at the given node and visit each node with the given function.
     Running time: Theta(n) must visit every node
     Memory usage: Theta((widest level)) if a level has 100 nodes we need to
     enqueue all of them."""
     # TODO: Create queue to store nodes not yet traversed in level-order
     queue = LinkedQueue()
     # TODO: Enqueue given starting node
     queue.enqueue(start_node)
     # TODO: Loop until queue is empty
     while queue.is_empty() is False:
         # TODO: Dequeue node at front of queue
         node = queue.dequeue()
         # TODO: Visit this node's data with given function
         visit(node.data)
         # TODO: Enqueue this node's left child, if it exists
         if node.left is not None:
             queue.enqueue(node.left)
         # TODO: Enqueue this node's right child, if it exists
         if node.right is not None:
             queue.enqueue(node.right)
Example #7
0
    def _traverse_level_order_iterative(self, start_node, visit):
        """Traverse this binary tree with iterative level-order traversal (BFS).
        Start at the given node and visit each node with the given function.
        Running time: O(n) we visit n times each node
        Memory usage: O(n) we are creating a queue, and adding each node to it"""

        # Create queue to store nodes not yet traversed in level-order
        queue = LinkedQueue()
        # Enqueue given starting node
        queue.enqueue(start_node)
        # Loop until queue is empty
        while not queue.is_empty():
            # Dequeue node at front of queue
            node = queue.dequeue()
            # Visit this node's data with given function
            visit(node.data)
            if (node.left != None):
                # Enqueue this node's left child, if it exists
                queue.enqueue(node.left)
            if (node.right != None):
                # Enqueue this node's right child, if it exists
                queue.enqueue(node.right)
Example #8
0
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Start at the given node and visit each node with the given function.
     TODO: Running time: O(n) we visit every node in tree
     TODO: Memory usage: O(n) because the most nodes that will be in queue at a time are (n+1)/2 due to furthest depth in
     the tree O(n)
     """
     # Create queue to store nodes not yet traversed in level-order
     queue = LinkedQueue()
     # Enqueue given starting node
     queue.enqueue(start_node)
     # Loop until queue is empty
     while not queue.is_empty():
         # Dequeue node at front of queue
         node = queue.dequeue()
         # Visit this node's data with given function
         visit(node.data)
         # Enqueue this node's left child, if it exists
         if node.left is not None:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right is not None:
             queue.enqueue(node.right)
Example #9
0
    def _traverse_level_order_iterative(self, start_node, visit):
        """Traverse this binary tree with iterative level-order traversal (BFS).
        Start at the given node and visit each node with the given function.
        Running time: O(n) Where n is the number of nodes in the tree
        Memory usage: O(h) Where h is the height of the tree"""
        # Create queue to store nodes not yet traversed in level-order
        queue = LinkedQueue()
        # Enqueue given starting node
        queue.enqueue(start_node)
        # Loop until queue is empty
        while not queue.is_empty():
            # Dequeue node at front of queue
            node = queue.dequeue()
            # Visit this node's data with given function
            visit.append(node.data)
            
            if node.left:
            # Enqueue this node's left child, if it exists
                queue.enqueue(node.left)

            if node.right:
            # Enqueue this node's right child, if it exists
                queue.enqueue(node.right)
Example #10
0
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Start at the given node and visit each node with the given function.
     Running time:   4 operations for each visited node (dequeue, visit, 2 enqueues(children))
                     O(4n) reduces to O(n)
     Memory usage:   'Width' of tree or most items that end up in queue O(w)
                     Worst case is a balanced tree or greatest # of leaves"""
     #  Create queue to store nodes not yet traversed in level-order
     queue = LinkedQueue()
     #  Enqueue given starting node
     queue.enqueue(start_node)
     #  Loop until queue is empty
     while queue.is_empty() == False:
         #  Dequeue node at front of queue
         node = queue.dequeue()
         #  Visit this node's data with given function
         visit(node.data)
         #  Enqueue this node's left child, if it exists
         if node.left is not None:
             queue.enqueue(node.left)
         #  Enqueue this node's right child, if it exists
         if node.right is not None:
             queue.enqueue(node.right)
 def items_level_order(self):
     """Return a list of all items in this binary search tree found using
     level-order traversal"""
     # TODO: Create a queue to store nodes not yet traversed in level-order
     queue = LinkedQueue()
     # Create an items list
     items = list()
     # TODO: Enqueue the root node if this tree is not empty
     if self.root is not None:
         queue.enqueue(self.root)
     # TODO: Loop until the queue is empty
     while queue.length() > 0:
         # TODO: Dequeue the node at the front of the queue
         node = queue.dequeue()
         # TODO: Add this node's data to the items list
         items.append(node.data)
         # TODO: Enqueue this node's left child if it exists
         if node.left is not None:
             queue.enqueue(node.left)
         # TODO: Enqueue this node's right child if it exists
         if node.right is not None:
             queue.enqueue(node.right)
     # Return the items list
     return items
Example #12
0
    def _traverse_level_order_iterative(self, start_node, visit):
        """Traverse this binary tree with iterative level-order traversal (BFS).
        Start at the given node and visit each node with the given function.
        Running time: O(n), function visits all nodes
        Memory usage: O(n^2)
        """

        # Thank you Connor and Lucia for great references: https://github.com/lvreynoso/CS1.3-Coursework/blob/master/binarytree.py
        # Create queue to store nodes not yet traversed in level-order
        queue = LinkedQueue()
        # Enqueue given starting node
        queue.enqueue(start_node)
        # Loop until queue is empty
        while not queue.is_empty():
            # Dequeue node at front of queue
            node = queue.dequeue()
            # Visit this node's data with given function
            visit(node.data)
            # Enqueue this node's left child, if it exists
            if node.left is not None:
                queue.enqueue(node.left)
            # Enqueue this node's right child, if it exists
            if node.right is not None:
                queue.enqueue(node.right)
Example #13
0
 def setUp(self):
     self.queue = LinkedQueue()
Example #14
0
 def __init__(self):
     self._totalCustomerWaitTime = 0
     self._customersServed = 0
     self._currentCustomer = None
     self._queue = LinkedQueue()