Question

urgent

, and what is the big-O?Write a Priority Queue method, end_of_path(path), that is given a string, path, as input, which represents the path from the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Big O is O(n) , where n is the path length , 'LR' has path length of 2, because we are just iterating the string and moving either left or right based on the input.

---------------------------------------------------------------------------------------------------------------

class PriorityQueue:

    def __init__(self):

        self.bin_heap = [0]

        self.current_size = 0

    def insert(self,k):

        self.bin_heap.append(k)

        self.current_size = self.current_size + 1

        self.perc_up(self.current_size)

    def build(self,k):

        for i in k:

          self.bin_heap.append(i)

          self.current_size = self.current_size + 1

       

        index = self.current_size;

        while index>=1:

          self.perc_up(index-1)

          index = index-1

       

    def perc_up(self,i):

        while i // 2 > 0:

            if self.bin_heap[i] < self.bin_heap[i // 2]:

                tmp = self.bin_heap[i // 2]

                self.bin_heap[i // 2] = self.bin_heap[i]

                self.bin_heap[i] = tmp

            i = i // 2

    def perc_down(self,i):

        while (i * 2) <= self.current_size:

            mc = self.min_child(i)

            if self.bin_heap[i] > self.bin_heap[mc]:

                tmp = self.bin_heap[i]

                self.bin_heap[i] = self.bin_heap[mc]

                self.bin_heap[mc] = tmp

            i = mc

    def end_of_path(self,string):

      if len(string)==0:

        return self.bin_heap[1]

      index = 0

      k = 1

      while index < len(string):

        if string[index] =='L':

          path = 2*k

        else:

          path = 2*k + 1

       

        index = index + 1

        k = path

      if path<=self.current_size:

        return self.bin_heap[path]

      else:

        return None

values = [1]

pq = PriorityQueue()

for i in values:

pq.insert(i)

print(pq.end_of_path(''))

values = [1,2,3,37,6,5]

pq = PriorityQueue()

for i in values:

pq.insert(i)

print(pq.end_of_path('LR'))

--------------------------------------------------------------------------------------------

SEE OUTPUT
saved main.py http://PiercingMusty Files def end_of_path(self, string): if len(string)0: return self.bin_heap [1] 40 Python 3



Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
urgent , and what is the big-O? Write a Priority Queue method, end_of_path(path), that is given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list...

    Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list using node. (from chapter 24). Your code should utilize the selectionsort method from attached and create another method call selectionsort in linkedlist class. To demonstrate the usage of the selection sort method, you should manually create a link list of random integer (say of 5 numbers), and you need to demonstrate the use the selection sort to sorted the link list. Please submit your...

  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

    Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {        //TODO 1: Create a priority queue of Strings and assign it to variable queue1               //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1                      System.out.println("Priority queue using Comparable:");        //TODO 3: remove from queue1 all the strings one by one        // with the "smaller" strings (higher priority) ahead of "bigger"...

  • Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user...

    Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue. The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • 1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

    1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push( "Jane" ); push( "Jess" ); push( "Jill" ); push( pop() ); push( "Jim" ); String name = pop(); push( peek() ); Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation. What is in...

  • # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sV...

    # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild())+')' return sVal #### Functions and classes to help with Homework 6 #### You should not make any changes to the functions and classes in this file, #### but you should understand what they do and how they work. import sys ### This function will be re-defined in hw6.py. However, it is needed for ExpTree, ###...

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT