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]
2 years 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]2 years 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
True or false a circle will have different radii
PilotLPTM [1.2K]
Yes it’s true because of the size of the circle
8 0
2 years ago
After the end of world war ii, nations met at the _______ to create a new worldwide economic system.
madam [21]
They met at the Mount Washington Hotel I believe.
3 0
2 years ago
Type your response in the box.
kow [346]

Answer:

The author makes this claim in the passage: Keeping animals in zoos is cruel and unfair to the animals.

The author supports this claim by discussing three major problems with zoos. The first problem is that zoos cause behavior problems in animals. The author supports this claim by mentioning studies that prove the claim. The author also states that zoos do not consider the specific traits of different animals. The author cites a study of a zoo in Belfast, Ireland, to support this claim. Lastly, the author states that zoos do not focus on protecting endangered animals. The author supports this claim by noting the number of zoos worldwide that support wildlife conservation.

Explanation:

that is the sample answer.

7 0
2 years ago
A force of 30 n stretches a very light ideal spring 0. 73 m from equilibrium. What is the force constant (spring constant) of th
Rainbow [258]

According to Hooke's law, a 30-N force stretches a spring by 0.73 m if the spring constant k is such that

30 N = k (0.73 m)

k = (30 N) / (0.73 m)

k ≈ 4.1 N/m

3 0
3 years ago
The credit side of journal entry is to record the following EXCEPT
barxatty [35]
I think it’s C but I could be wrong
5 0
3 years ago
Read 2 more answers
Other questions:
  • Kanya drops her notebook in the busy hallway at school. all the paper spill out and its a mess people see her accident but nobod
    12·2 answers
  • If you are charged in a crash with injuries and are driving a motorcycle, you are _______________ for any bodily injuries and pr
    15·1 answer
  • Which statement is true about light-colored surfaces?
    6·1 answer
  • In 10 years where do you think you will be in life, with how our world is do you think it will be better or worse?
    6·2 answers
  • MORE FREEEEEE POINTSSSSS
    7·2 answers
  • Choose The corect opion Her promoțio Was The momente of Her lifer​
    9·1 answer
  • Yo it's ur girl naty, who graduating this year
    9·2 answers
  • Carson lost his job as a construction worker when his company had to make cuts. How might Carson’s job loss affect his local com
    14·1 answer
  • Determine the minimum mass of hydrogen gas required to produce 226 kj of heat.
    7·1 answer
  • Given 88. 0 grams of boron, how many atoms of boron (b) are present?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!