C# language 6. (45 points) Below is a partial definition of a PriorityQueue clas
ID: 3573697 • Letter: C
Question
C# language
6. (45 points) Below is a partial definition of a PriorityQueue class that stores its elements in a heap formed with BinaryTreeNode>s. Fill in the merge method below to return the result of combining the elements of the given heaps into a single heap. For efficiency reasons, if both h1 and h2 are nonempty, the result should be formed as follows: • Its root should be the root of either h1 or h2, whichever contains the larger priority (we will refer to this heap as L below). • Its left child should be the right child of L. • Its right child should be the result of merging the left child of L with the other given heap. If either heap is empty, return the other (if both are empty, return one of them — it doesn’t matter which one). A description of the BinaryTreeNode class is given on the last page. You may not use any loops — use recursion instead. You may not add any code outside of this method.
public partial class PriorityQueue
where TPriority : IComparable
{
private static BinaryTreeNode>
Merge(BinaryTreeNode> h1,
BinaryTreeNode> h2)
{ Fill in Code }
BinaryTreeNode: • A constructor that takes a data item (of type T), a left child, and a right child (both of type BinaryTreeNode). • T Data: Gets the data stored in this node. • BinaryTreeNode LeftChild: Gets the left child. • BinaryTreeNode RightChild: Gets the right child.
Explanation / Answer
I am giving the PseudoCode here. You can translate the same to C# implementation.
public partial class PriorityQueue
where TPriority : IComparable
{
private static BinaryTreeNode
BinaryTreeNode L;
Merge(BinaryTreeNode h1, BinaryTreeNode h2){
if(h1 is empty)
return h2;
if(h2 is empty)
return h1;
if (h1.root.priority > h2.root.priority ){
L.root = h1.root;
L.left = h1.right;
L.right = Merge(h1.left,h2)
}
if (h2.root.priority > h1.root.priority ){
L.root = h2.root;
L.left = h2.right;
L.right = Merge(h2.left,h1)
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.