Task: Implement the methods with the two classes from above in Java.
Create a Priority Queue with binary trees. The implementation is based on two classes: PQueue and PNode. A priority queue object has a pointer "root" to an element of class PNode, as follows:
public class PQueue {
PNode root;
public static class PNode {
int key;
PNode left, right, parent;
int lcount, rcount;
}
Lcount and rcount are counters that record the number of nodes in the left and right subtree of a node. Using the data above, implement priority queues as binary trees with the heap property: "for each subtree, the key of the root is greater or equal to all other values in the subtree."
Create the following methods:
1. void insert(int value): inserts a value into the queue. Requirements: insertion keeps the binary tree size-balanced, that is, for each node, the number of nodes in its left and right subtrees differ at most by one. Since each node holds information about the size of its left and right subtrees (lcount and rcount), you can decide into which subtree to insert a new node while keeping the tree size-balanced. And since inserting a new value requires inserting a new node, you also have to update these counters when inserting. If you have found out where to insert the new node in your tree as a leaf, the insertion of the new value may violate the heap property.
2. Boolean isEmpty()
3. int extractMax(): returns the maximum value in the queue and deletes it. If the queue is empty, make a RuntimeException.
Hints: The maximum value in a heap is in the root. If you delete the root, this leaves a gap that needs to be filled. You should look for a leaf that can be dropped, load its value to the root, and delete the leaf.