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
All of the following are typical ways through which to find an apprenticeship except:
Lorico [155]
I think the answe is A
8 0
3 years ago
Read 2 more answers
What is the effect of wildfires on biodiversity? a. temporary decrease, but long-term increase b. temporary increase, but long-t
krok68 [10]

The effect of wildfires on biodiversity is temporary decrease, but long-term increase.

The effect of wildfires also known as forest fire on biodiversity is wildfires break down organic material faster than decomposition, thus renewing soil nutrients more quickly.

Effect of wildfires is that the loss of biodiversity and extinction of plants and animals. Loss of wildlife habitat and depletion of wildlife. Loss of natural regeneration and reduction in forest cover, warming.

The smoke from wildfires causes pollution and is bad for your health regardless of where you reside. Wildfires release harmful pollutants including particulate and toxin gases like carbon monoxide, nitrogen oxides, and non-methane organic compounds into the atmosphere, wildfires have many acute and chronic effects on human health too.

To know more about wildfires here

brainly.com/question/2026716

#SPJ4

4 0
2 years ago
What condition best describes the heat flow from one object to another.
lapo4ka [179]
Heat can be transferred in three ways: by conduction, by convention, and by radiation. Conduction is transferred of energy from one molecule to another by direct contact.
4 0
2 years ago
Thanks for helping. really thank you it was very helpful. THANKS A LOT.
Katyanochek1 [597]
What is your question
8 0
3 years ago
Read 2 more answers
what does the united nations estimate will happen to the population of developed countries between now and 2025?
Andre45 [30]
United Nations estimates that the population of developed countries between now and 2025 will grow more slowly than in developing countries.

By the year 2050, the worlds population is projected to reach 9 billion people.

According to a report by the United Nations, the current world population of 7.6 billion is expected to reach 8.6 billion in 2030, 9.8 billion in 2015 and 11.2 billion in 2100. Besides, The biggest increase in population is estimated to happen in Africa, and Asia.
6 0
2 years ago
Other questions:
  • Analyze: What is Gladwell's purpose in the last part of the essay? How does he achieve this purpose? Support your explanation wi
    10·1 answer
  • Maintaining good contact in a relationship is also known as the__of a relationship
    11·1 answer
  • Which of the following statements is true?
    12·2 answers
  • Which is a verb: mrs. b, glasses, wears
    5·2 answers
  • A North American Hummingbird's wings beat. approximately 53 beats per second. How many beals will the hummingbird's wings beat i
    5·2 answers
  • Please help! this is due today! No links please and full paragraph form answer! Will give you brainliest!
    8·2 answers
  • A species of mud snail was studied for five years. During this time, scientists observed the season in which they mated, the num
    13·1 answer
  • Calculate the number of different words that can be obtained by rearranging the letters of the word trust.
    8·1 answer
  • How much pure alcohol must a pharmacist add to 10 cm.
    10·1 answer
  • the presentation of events suggests that the narrator likely intends for a reader to react to the passage with
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!