1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ivahew [28]
1 year ago
6

Once you have your heap structure created, next you must use it as a backing structure to a priority queue. Develop a PriorityQu

eue data structure in a file named PriorityQueue that is backed by a linked heap. Implement the normal methods that accompany a priority queue structure:
SAT
1 answer:
saveliy_v [14]1 year ago
6 0

Using the knowledge in computational language in python it is possible to write a code that Develop a PriorityQueue data structure in a file.

<h3>Writting the code in python:</h3>

<em>import sys</em>

<em>class Node:</em>

<em>def __init__(self,key):</em>

<em>self.left = None</em>

<em>self.right = None</em>

<em>self.val = key</em>

<em>self.parent = None</em>

<em>class MinHeap:</em>

<em>def __init__(self, maxsize):</em>

<em>self.maxsize = maxsize</em>

<em>self.size = 0</em>

<em>self.Heap = Node(-1 * sys.maxsize )</em>

<em>self.FRONT = self.Heap</em>

<em># A utility function to do inorder tree traversal</em>

<em>def isLeaf(self, node):</em>

<em>if node.left == None and node.right == Node:</em>

<em>return True</em>

<em>return False</em>

<em># Function to swap two nodes of the heap</em>

<em>def swap(self, node1, node2):</em>

<em>node1.data, node2.data = node2.data, node1.data</em>

<em># Function to heapify the node at pos</em>

<em>def minHeapify(self, node):</em>

<em># If the node is a non-leaf node and greater</em>

<em># than any of its child</em>

<em>if not self.isLeaf(node):</em>

<em>if (node.data > node.left.data or</em>

<em>node.data > node.right.data):</em>

<em># Swap with the left child and heapify</em>

<em># the left child</em>

<em>if node.left.data < node.right.data:</em>

<em>self.swap(node, node.left)</em>

<em>self.minHeapify(node.left)</em>

<em># Swap with the right child and heapify</em>

<em># the right child</em>

<em>else:</em>

<em>self.swap(node, node.right)</em>

<em>self.minHeapify(node.right)</em>

<em># Function to insert a node into the heap</em>

<em>def insert(self, element):</em>

<em>if self.size >= self.maxsize :</em>

<em>return</em>

<em>self.size+= 1</em>

<em>self.bst_insert(FRONT, element)</em>

<em>current = FRONT</em>

<em>while current.parent != None and current.data < current.parent.data:</em>

<em>self.swap(current, current.parent)</em>

<em>current = current.parent</em>

<em># Function to print the contents of the heap</em>

<em>def Print(self):</em>

<em>self.inorder()</em>

<em># Function to build the min heap using</em>

<em># the minHeapify function</em>

<em>def inorder(self, root):</em>

<em>if root:</em>

<em>inorder(root.left)</em>

<em>print(root.val)</em>

<em>inorder(root.right)</em>

<em>def bst_insert(self, root, node):</em>

<em>if root is None:</em>

<em>root = node</em>

<em>else:</em>

<em>root.next = node</em>

<em>self.FRONT = node</em>

<em># Driver Code</em>

<em>if __name__ == "__main__":</em>

<em>r = Node(50)</em>

<em>bst_insert(r,Node(30))</em>

<em>bst_insert(r,Node(20))</em>

<em>bst_insert(r,Node(40))</em>

<em>bst_insert(r,Node(70))</em>

<em>bst_insert(r,Node(60))</em>

<em>bst_insert(r,Node(80))</em>

<em># Print inoder traversal of the BST</em>

<em>inorder(r)</em>

<em>print('The minHeap is ')</em>

<em>minHeap = MinHeap(15)</em>

<em>minHeap.insert(5)</em>

<em>minHeap.insert(3)</em>

<em>minHeap.insert(17)</em>

<em>minHeap.insert(10)</em>

<em>minHeap.insert(84)</em>

<em>minHeap.insert(19)</em>

<em>minHeap.insert(6)</em>

<em>minHeap.insert(22)</em>

<em>minHeap.insert(9)</em>

<em>minHeap.minHeap()</em>

<em>minHeap.Print()</em>

<em>print("The Min val is " + str(minHeap.remove()))</em>

See more about python at brainly.com/question/13437928

#SPJ1

You might be interested in
Which of the following offers students a group of courses to be taught over a couple of years?
Nikolay [14]
IB does this. The IB plans out a students high school courses over at least 2 years. Trust me, I'm in it! Hope that helps!
7 0
3 years ago
Read 2 more answers
Questão 01- As relações entre ciência e senso co-
azamat

Answer:

a

Explanation:

5 0
2 years ago
Aside from a student ID what is another card for college student can use to obtain discounts
MissTica

Student Advantage card



8 0
2 years ago
Read 2 more answers
Why does hamilton think judges should be appointed permanently rather than on a periodic basis? permanent appointments allow for
Lana71 [14]

Hamilton thinks judges should be appointed permanently rather than on a periodic basis because periodic appointments would destroy a judge’s independence.

Alexander Hamilton was an American revolutionary. He was also a statesman and the Founding Father of the United States. He played a key role in promotion and interpretation of the constitution.

Permanent appointments would help to regulate more as well ensure greater independence of the judge.

Periodic appointment on the other hand would destroy a judge independence as he/she can be transferred or impeached anytime that fosters insecurity and lack of motivation to work.

Thus, permanent appointments is the only option.

To learn more about judges here

brainly.com/question/1059156

#SPJ4

7 0
1 year ago
The price of a coat is marked down 35%. The original price was $127. What is the sale price of the coat?.
Serhud [2]

Answer:

$82.55

Explanation:

127 - (0.35 x 127) = $82.55

6 0
2 years ago
Other questions:
  • Lincoln makes an appeal to logos through his passionate support of the Union. description of the Union’s history. concern about
    6·2 answers
  • Is a 3.86 weighted gpa good for freshman year of highschool?
    8·2 answers
  • The "sunflower-bordered roads" instill in the narrator a sense of A) adventure. B) creativity. Eliminate C) freedom. D) regret.
    12·2 answers
  • A weather map shows a low pressure area with circles around it. What does this represent?
    8·2 answers
  • Is she pretty? this is not me but tho what is 2344+ 2945-234= <br> ^ so i don't get a warning lol
    7·2 answers
  • Hi! I have started a new Google Classroom for anyone who needs a little extra help with homework! All answers are verified and e
    8·1 answer
  • Heres some points ya piont goblins​
    6·2 answers
  • According to the table, what is the total distance the hiker walked? 4 km 6 km 10 km 20 km.
    13·1 answer
  • On a small planet, an astronaut uses a vertical force of 175 newtons to lift an 87. 5-kilogram boulder at constant velocity to a
    6·1 answer
  • Which of the following statements is true about a quick termination connector
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!