Beispiel #1
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
        while queue.is_empty() == False:
            node = queue.dequeue()
            visit(node.data)
            if node.left is not None:
                queue.enqueue(node.left)
            if node.right is not None:
                queue.enqueue(node.right)
            # TODO: Dequeue node at front of queue
            node = ...
            # TODO: Visit this node's data with given function
            ...
            # TODO: Enqueue this node's left child, if it exists
            ...
            # TODO: Enqueue this node's right child, if it exists
            ...
Beispiel #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.
        Running time: O(n) because there is O(1) operation on each node
        Memory usage: O(width) the stack won't exceed the width of the level it's in"""

        # 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)
    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."""

        # ! Runtime = O(3n), must vist every node in order => tends towards O(n)
        # ! Space = O(w), w being the width of the binary tree (w is the largest queue)
        # ! worst case = O(h^2), h being height
        # ! height = log2n <=> (n + 1)/2

        # 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)
Beispiel #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.
        Visits its neighbors
        Run time: O(n) where n is the number of nodes in the entire tree. O(4n)
        Memory usage: O(w) where w is the width of the tree
                        O(2^h) worst case bottom most level of tree
                        (n+1) / 2"""
        # 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()

            if node is not None:
                # Visit this node's data with given function
                visit(node.data)
                # Enqueue this node's left child
                if node.left != None:
                    queue.enqueue(node.left)
                # Enqueue this node's right child
                if node.right != None:
                    queue.enqueue(node.right)
 def bad_balance(self):
     print('Previous height:')
     print(self._height())
     queue = LinkedQueue()
     ls = self.in_order()
     self._binary_sorter(ls, queue.enqueue)
     self.tree = BinarySearchTree()
     count = 1
     while queue.front() is not None:
         self.add(ls[queue.dequeue()])
         count += 1
     print('Balanced height:')
     print(self._height())
     print(count)
Beispiel #6
0
 def levelorder(self, lyst):
     """Adds items to lyst during
     a levelorder traversal."""
     levelsQueue = LinkedQueue()
     levelsQueue.enqueue(self)
     while not levelsQueue.isEmpty():
         node = levelsQueue.dequeue()
         lyst.append(node.getRoot())
         left = node.getLeft()
         right = node.getRight()
         if not left.isEmpty():
             levelsQueue.enqueue(left)
         if not right.isEmpty():
             levelsQueue.enqueue(right)
Beispiel #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.
     TODO: Running time: O(n), every node is visited
     TODO: Memory usage: O(n), max # nodes in queue dependent on max depth of tree, reduces from (n+1)/2 to O(n)"""
     queue = LinkedQueue(
     )  #create queue to store nodes not yet traversed in level-order
     queue.enqueue(start_node)  #enqueue start_node
     while not queue.is_empty():  #loop until queue is empty
         node = queue.dequeue()  #dequeue node @ front of queue b/c fifo
         visit(node.data)  #visit this node's data using given fxn
         if node.left is not None:  #if this node's left child exists,
             queue.enqueue(node.left)  #enqueue it
         if node.right is not None:  #if this node's right child exists,
             queue.enqueue(node.right)  #enqueue it
Beispiel #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.
        Running time: O(n) because every node is visited once
        TODO: Memory usage: ??? Why and under what conditions?"""
        queue = LinkedQueue()
        queue.enqueue(start_node)

        while not queue.is_empty():
            node = queue.dequeue()
            visit(node.data)
            if node.left is not None:
                queue.enqueue(node.left)
            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"""
     queue = LinkedQueue()
     # Create an items list
     items = list()
     if self.is_empty() is False:
         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
Beispiel #10
0
 def levelorder(self, lyst):
     """Adds items to lyst during
     a levelorder traversal."""
     levelsQueue = LinkedQueue()
     levelsQueue.enqueue(self)
     while not levelsQueue.isEmpty():
         node = levelsQueue.dequeue()
         lyst.append(node.getRoot())
         left = node.getLeft()
         right = node.getRight()
         if not left.isEmpty():
             levelsQueue.enqueue(left)
         if not right.isEmpty():
             levelsQueue.enqueue(right)
Beispiel #11
0
 def levelorderTraverse(self):
     """Returns a list containing the results of
     a levelorder traversal."""
     def levelorderHelper(levelsQu, lyst):
         if not levelsQu.isEmpty():
             node = levelsQu.dequeue()
             lyst.append(node.data)
             if node.left != None:
                 levelsQu.enqueue(node.left)
             if node.right != None:
                 levelsQu.enqueue(node.right)
             levelorderHelper(levelsQu, lyst)
     lyst = []
     levelsQu = LinkedQueue()
     if self._root != None:
         levelsQu.enqueue(self._root)
         levelorderHelper(levelsQu, lyst)
     return lyst
Beispiel #12
0
class Cashier(object):

    def __init__(self):
        self._totalCustomerWaitTime = 0
        self._customersServed = 0
        self._currentCustomer = None
        self._queue = LinkedQueue()

    def addCustomer(self, c):
        self._queue.enqueue(c)
   
    def serveCustomers(self, currentTime):
        if self._currentCustomer is None:
            # No customers yet
            if self._queue.isEmpty():
                return
            else:
                # Dequeue first waiting customer and tally results
                self._currentCustomer = self._queue.dequeue()
                self._totalCustomerWaitTime += \
                                            currentTime - \
                                            self._currentCustomer.arrivalTime()
                self._customersServed += 1

        # Give a unit of service
        self._currentCustomer.serve()

        # If current customer is finished, send it away   
        if self._currentCustomer.amountOfServiceNeeded() == 0:
            self._currentCustomer = None
   
    def __str__(self):
        result = "TOTALS FOR THE CASHIER\n" + \
                 "Number of customers served:        " + \
                 str(self._customersServed) + "\n"
        if self._customersServed != 0:
            aveWaitTime = float(self._totalCustomerWaitTime) /\
                          self._customersServed
            result += "Number of customers left in queue: " + \
                      str(len(self._queue)) + "\n" + \
                      "Average time customers spend\n" + \
                      "waiting to be served:              " + \
                      "%5.2f" % aveWaitTime
        return result
Beispiel #13
0
class Cashier(object):
    def __init__(self):
        self._totalCustomerWaitTime = 0
        self._customersServed = 0
        self._currentCustomer = None
        self._queue = LinkedQueue()

    def addCustomer(self, c):
        self._queue.enqueue(c)

    def serveCustomers(self, currentTime):
        if self._currentCustomer is None:
            # No customers yet
            if self._queue.isEmpty():
                return
            else:
                # Dequeue first waiting customer and tally results
                self._currentCustomer = self._queue.dequeue()
                self._totalCustomerWaitTime += \
                                            currentTime - \
                                            self._currentCustomer.arrivalTime()
                self._customersServed += 1

        # Give a unit of service
        self._currentCustomer.serve()

        # If current customer is finished, send it away
        if self._currentCustomer.amountOfServiceNeeded() == 0:
            self._currentCustomer = None

    def __str__(self):
        result = "TOTALS FOR THE CASHIER\n" + \
                 "Number of customers served:        " + \
                 str(self._customersServed) + "\n"
        if self._customersServed != 0:
            aveWaitTime = float(self._totalCustomerWaitTime) /\
                          self._customersServed
            result += "Number of customers left in queue: " + \
                      str(len(self._queue)) + "\n" + \
                      "Average time customers spend\n" + \
                      "waiting to be served:              " + \
                      "%5.2f" % aveWaitTime
        return result
Beispiel #14
0
    def levelorderTraverse(self):
        """Returns a list containing the results of
        a levelorder traversal."""
        def levelorderHelper(levelsQu, lyst):
            if not levelsQu.isEmpty():
                node = levelsQu.dequeue()
                lyst.append(node.data)
                if node.left != None:
                    levelsQu.enqueue(node.left)
                if node.right != None:
                    levelsQu.enqueue(node.right)
                levelorderHelper(levelsQu, lyst)

        lyst = []
        levelsQu = LinkedQueue()
        if self._root != None:
            levelsQu.enqueue(self._root)
            levelorderHelper(levelsQu, lyst)
        return lyst
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Coded with Nathan in our room 💪🏼
     Start at the given node and visit each node with the given function."""
     # 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)
Beispiel #16
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?"""
        queue = LinkedQueue()
        # Enqueue given starting node
        queue.enqueue(start_node)

        # Loop until queue is empty
        while not queue.is_empty():
            node = queue.dequeue()

            visit.append(node.data)

            if node.left:
                queue.enqueue(node.left)

            if node.right:
                queue.enqueue(node.right)
Beispiel #17
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), n is the number of nodes in the tree. visit node and enqueing its children each takes O(1), collectively O(n)
     Memory usage: O(n), n is the number of nodes in the tree. the size of the queue can grow to be the number of nodes in 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(node.data)
         # Enqueue this node's left child, if it exists
         if node.left != None:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right != None:
             queue.enqueue(node.right)
Beispiel #18
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), because all nodes must be traversed
     Memory usage: O(n) where h equals the height, since all is placed in a queue"""
     # 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() != True:
         # 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 != None:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right != None:
             queue.enqueue(node.right)
Beispiel #19
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)
    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
 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) if implimented with a LL(), O(n square) if Array.
     TODO: Memory usage: ??? Why and under what conditions?"""
     # 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)
Beispiel #22
0
 def breadthfirst(self):
   if self.is_empty():
     return
   queue = LinkedQueue()
   queue.enqueue(self.root())
   while not queue.is_empty():
     pos = queue.dequeue
     yield pos
     for child in this.children(pos):
       queue.enqueue(child)
Beispiel #23
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.
     O(n) becasue each node gets called on 1 time.
     Mem Usage O(logN)if tree is balanced bc of the call stack"""
     # 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() == 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 != None):
             queue.enqueue(node.left)
         # TODO: Enqueue this node's right child, if it exists
         if (node.right != None):
             queue.enqueue(node.right)
Beispiel #24
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) Why and under what conditions?
     TODO: Memory usage: O(n/2) Why and under what conditions?"""
     # Create queue to store nodes not yet traversed in level-order
     from queue import LinkedQueue
     queue = LinkedQueue()
     # Enqueue given starting node
     queue.enqueue(start_node)
     # Loop until queue is empty
     while queue.is_empty() is 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:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right:
             queue.enqueue(node.right)
Beispiel #25
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)
Beispiel #26
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)
 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)
Beispiel #28
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)
Beispiel #29
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)
Beispiel #30
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)
 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
Beispiel #32
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) because traversal means that every node has to be visited
     TODO: Memory usage:"""
     # 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.size > 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:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right:
             queue.enqueue(node.right)
Beispiel #33
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), for each node
     Memory usage: O(b) because we have to traverse the bredth/width of the tree on each layer before we can go on to the next"""
     # 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.list.length() == 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 != None:
             queue.enqueue(node.left)
         # Enqueue this node's right child, if it exists
         if node.right != None:
             queue.enqueue(node.right)
Beispiel #34
0
 def __init__(self):
     self._totalCustomerWaitTime = 0
     self._customersServed = 0
     self._currentCustomer = None
     self._queue = LinkedQueue()
Beispiel #35
0
 def setUp(self):
     self.queue = LinkedQueue()
Beispiel #36
0
class TestLinkedQueue(unittest.TestCase):
    def setUp(self):
        self.queue = LinkedQueue()

    def test_enqueue(self):
        self.queue.enqueue(1)
        self.queue.enqueue(2)
        self.queue.enqueue(3)
        self.assertEqual(self.queue.size, 3)

    def test_dequeue(self):
        self.queue.enqueue(1)
        self.queue.enqueue(2)
        self.assertEqual(self.queue.dequeue(), 1)

    def test_peek(self):
        self.queue.enqueue(1)
        self.assertEqual(self.queue.peek(), 1)

    def test_peek_when_stack_is_empty(self):
        with self.assertRaises(Exception):
            self.queue.peek()
    
    def test_is_empty_when_stack_is_empty(self):
        self.assertEqual(self.queue.is_empty(), True)
    
    def test_is_empty_when_stack_is_not_empty(self):
        self.queue.enqueue(1)
        self.assertEqual(self.queue.is_empty(), False)
    
    def test_show_all(self):
        self.queue.show_all()