def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek ------------------------------------------------------- """ pq = Priority_Queue() print(pq.is_empty()) print(array_to_pq(pq, a)) print(pq.is_empty()) print(pq.peek()) a = pq.remove() print(pq.peek()) pq.insert(a) print(pq.peek()) print(pq.is_empty()) return
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: Priority_Queue_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both is_empty and non-is_empty priority queues using the data in a: is_empty, push, pop, peek ------------------------------------------------------- """ pq = Priority_Queue() print("Priority Queue Initialised") print() print(SEP) print("Priority Queue is_empty (expect True): {}".format(pq.is_empty())) print("Priority Queue size (expect 0): {}".format(len(pq))) print(SEP) print("Add one item to Priority Queue") pq.insert(a[0]) print("Front of Priority Queue (peek):") print(pq.peek()) print(SEP) print("Priority Queue is_empty (expect False): {}".format(pq.is_empty())) print("Priority Queue size (expect 1): {}".format(len(pq))) print(SEP) print("Priority Queue remove") v = pq.remove() print(v) print(SEP) print("Priority Queue is_empty (expect True): {}".format(pq.is_empty())) print("Priority Queue size (expect 0): {}".format(len(pq))) print(SEP) print("Copy all data to Priority Queue") array_to_pq(pq, a) print("Priority Queue is_empty (expect False): {}".format(pq.is_empty())) print("Priority Queue size (expect > 0): {}".format(len(pq))) print(SEP) print("Front of Priority Queue (peek):") print(pq.peek()) print(SEP) print("Remove all elements from Priority Queue") while not pq.is_empty(): v = pq.remove() print(v) print() print(SEP) print("Priority Queue is_empty (expect True): {}".format(pq.is_empty())) print("Priority Queue size (expect 0): {}".format(len(pq))) print() return
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Test the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: None ------------------------------------------------------- """ pq = Priority_Queue() print(pq.is_empty()) print("inserting 5:", pq.insert(5)) print("removing", pq.remove()) print("Peek:", pq.peek()) # tests for the priority queue methods go here # print the results of the method calls and verify by hand return None
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Test the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: None ------------------------------------------------------- """ pq = Priority_Queue() array_to_pq(pq, a) print(pq.is_empty()) print(pq.peek()) print(pq.remove()) #print(pq.insert(1)) # print the results of the method calls and verify by hand return
def levelorder(self): """ ------------------------------------------------------- Copies the contents of the tree in levelorder order to a list. Use: values = bst.levelorder() ------------------------------------------------------- Returns: values - a list containing the values of bst in levelorder. (list of ?) ------------------------------------------------------- """ node = self._root if not node: return queue = Priority_Queue() queue.insert(node) traversal = [] while not queue.is_empty(): traversal.append(queue.peek()) node = queue.remove() if node._left: queue.insert(node._left) if node._right: queue.insert(node._right) return traversal
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek ------------------------------------------------------- """ pq = Priority_Queue() dummy = [] if pq.is_empty() == True: print('pq is empty.') array_to_pq(pq, a) print('Converting a into a pq...') if pq.is_empty() == False: print('a has been transferred into pq!') print('\nRemoving pq...') while pq.is_empty() == False: temp = pq.remove() print(temp) dummy.append(temp) print('\pq is empty. Inserting values back into queue...') while dummy != []: temp = dummy.pop() print(temp) pq.insert(temp) print('\nPushing complete! Peeking...') print(pq.peek()) print('\npq is {} objects long!'.format(len(pq))) return
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek ------------------------------------------------------- """ pq = Priority_Queue() # tests for the priority priority queue methods go here # print the results of the method calls and verify by hand # Test if priority queue is empty (Expected: TRUE) print("priority queue contents: ") for i in pq: print(i, end=' ') print("\tEmpty? {}".format(pq.is_empty())) # Load elements from a into priority queue print(">> Insert elements into a onto priority queue") print(">> Num elements to insert: {}".format(len(a))) for elem in a: pq.insert(elem) # Check if the priority queue is empty again (T/F depending on a contents) print("priority queue contents: ") for i in pq: print(i, end=' ') print("\tEmpty? {}".format(pq.is_empty())) # Peek the top of the priority queue top = None try: top = pq.peek() except: print(">>! Peeked at an empty priority queue, assertion caught, continuing...") # Check if top of priority queue is the end element of a print("Top of priority queue should be same as beginning of list") highest_priority = 0 if pq.is_empty(): print("List empty so no check") else: for i in range(len(a)): if a[i] < a[highest_priority]: highest_priority = i priority_same = (top == a[highest_priority]) print("Highest priority same as smallest in list?") print("Top: \n{}\tFront of list: \n{}".format(top, a[highest_priority])) print("Same? {}".format(priority_same)) # Testing pop #try: print(">> Remove top of priority queue") popped = pq.remove() print("Removed value from pq: \n{}\tLowest value of list: \n{}".format(popped, a[highest_priority])) print("Removed value should be same as lowest value of list.\tSame? {}".format(popped == a[highest_priority])) #except: #print(">>! Removed from an empty priority queue, exception caught, continuing...") return
""" ------------------------------------------------------------------------ [program description] ------------------------------------------------------------------------ Author: Nicolas Mills ID: 180856100 Email: [email protected] __updated__ = 2019-01-28 ------------------------------------------------------------------------ """ from Priority_Queue_array import Priority_Queue pq = Priority_Queue() values = [66, 55, 44, 33, 22, 11] for i in values: pq.insert(i) removed = pq.remove() print("removed: {}\tpeeked: {}".format(removed, pq.peek())) assert pq.peek() == values[-2]