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
a box contains 10 equal-sized balls, numbered 1 to 10. two balls are drawn at random simultaneously. what is the probability tha
EleoNora [17]

The probability that the two balls are chosen <u>will differ by more than 2</u> is 43/45

Probability is the likelihood or chance that an event will occur.

If a box contains 10 equal-sized balls and 2 balls are chosen at random from the box, the total outcome will be expressed as:

10C_2=\frac{10!}{(10-2)!2!}\\ 10C_2=\frac{10!}{8!2!}\\10C_2=\frac{10\times 9 \times 8!!}{8!2!}\\10C2=\frac{90}{2} =45 ways

If the number on the two balls chosen will<u> differ by less than or equal to 2,</u> the expected outcomes are (1, 2), (1, 3) that is 2 outcomes

The probability that the two balls are chosen will differ by less than or equal to 2 is 2/45

The probability that the two balls are chosen <u>will differ by more than 2</u> is 1 - 2/45 = 43/45

Learn more here: brainly.com/question/13604758

3 0
2 years ago
when trevor finished high school, he ended up working "in the hood" for a few years, instead of pursuing his education. when he
sp2606 [1]

Most people are most comfortable doing what they can do easily, to the extent that they don't want to leave their comfort zone. As a result of this, people get stuck doing something comfortable when they:

  • <u>Find it difficult to leave their comfort zone</u>

Because of this, the most important thing that people need to be propelled forward is:

  • <u>Motivation</u>

According to the given scenario, we can see that Trevor was stuck with doing something comfortable after his high school, which was working in the hood which made him not to go to college.

This clearly shows that he was finding it difficult to leave what he was comfortable doing because of how easy it was for him.

As a result of this, the thing which is needed if a person wants to propel himself forward is to have the right motivation which would make him move out of comfort to do something difficult that would be ultimately rewarding.

Read more here:

brainly.com/question/21222735

6 0
2 years ago
How were the planets in the solar system formed?
cupoosta [38]

Answer: The Sun and the planets formed together, 4.6 billion years ago, from a cloud of gas and dust called the solar nebula. A shock wave from a nearby supernova explosion probably initiated the collapse of the solar nebula. The Sun formed in the center, and the planets formed in a thin disk orbiting around it.

Explanation: how were the planets in the solar system formed?

5 0
3 years ago
Read 2 more answers
9. The length of a rectangle is 3 cm greater than
Alla [95]

Answer:

l = 18

w = 9

Explanation:

8 0
3 years ago
Okay 480...... here is the next one
kozerog [31]
B) Texture
hope this helps x
would love brainliest if it’s right and you can give it! :)
8 0
3 years ago
Other questions:
  • What’s the meaning of a boffin? Anyone?
    7·1 answer
  • 99 POINTS!!! If all your friends are called cliff, would you jump off them?
    10·2 answers
  • There are many factors that selective colleges take into consideration when deciding whether to admit a student list 4 of them
    7·1 answer
  • Classify the triangle by its angles.
    5·2 answers
  • Can flexible objects transfer electrical energy
    14·1 answer
  • Describe the scope of philosophy of education​
    7·2 answers
  • Do you play ff <br>yes <br>no​
    9·2 answers
  • Which of the following is not a reason for cluster distribution of organisms? a. Localized availability of resources b. Herd beh
    6·1 answer
  • Which word in the passage helps the reader determine the meaning of the word miscarriage? What is the best definition of the wor
    13·1 answer
  • Which circuit diagram represents voltmeter v connected correctly to measure the potential difference across resistor r2?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!