Question

Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage.

Please also create a Main Program to test the code.

Provided File

******************************

HeapIntPriorityQueue.java

******************************

import java.util.Arrays;
import java.util.NoSuchElementException;

// Implements a priority queue of integers using a
// min-heap represented as an array.
public class HeapIntPriorityQueue {
private int[] elementData;
private int size;
  
// Constructs an empty queue.
public HeapIntPriorityQueue() {
elementData = new int[10];
size = 0;
}
  
// Adds the given element to this queue.
public void add(int value) {
// resize if necessary
if (size + 1 >= elementData.length) {
elementData = Arrays.copyOf(elementData, elementData.length * 2);
}
  
// insert as new rightmost leaf
elementData[size + 1] = value;
  
// "bubble up" toward root as necessary to fix ordering
int index = size + 1;
boolean found = false; // have we found the proper place yet?
while (!found && hasParent(index)) {
int parent = parent(index);
if (elementData[index] < elementData[parent]) {
swap(elementData, index, parent(index));
index = parent(index);
} else {
found = true; // found proper location; stop the loop
}
}
  
size++;
}
  
// Returns true if there are no elements in this queue.
public boolean isEmpty() {
return size == 0;
}
  
// Returns the minimum value in the queue without modifying the queue.
// If the queue is empty, throws a NoSuchElementException.
public int peek() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return elementData[1];
}
  
// Removes and returns the minimum value in the queue.
// If the queue is empty, throws a NoSuchElementException.
public int remove() {
int result = peek();

// move rightmost leaf to become new root
elementData[1] = elementData[size];
size--;
  
// "bubble down" root as necessary to fix ordering
int index = 1;
boolean found = false; // have we found the proper place yet?
while (!found && hasLeftChild(index)) {
int left = leftChild(index);
int right = rightChild(index);
int child = left;
if (hasRightChild(index) &&
elementData[right] < elementData[left]) {
child = right;
}
  
if (elementData[index] > elementData[child]) {
swap(elementData, index, child);
index = child;
} else {
found = true; // found proper location; stop the loop
}
}
  
return result;
}
  
// Returns the number of elements in the queue.
public int size() {
return size;
}
  
// Returns a string representation of this queue, such as "[10, 20, 30]";
// The elements are not guaranteed to be listed in sorted order.
public String toString() {
String result = "[";
if (!isEmpty()) {
result += elementData[1];
for (int i = 2; i <= size; i++) {
result += ", " + elementData[i];
}
}
return result + "]";
}
  
  
// helpers for navigating indexes up/down the tree
private int parent(int index) {
return index / 2;
}
  
// returns index of left child of given index
private int leftChild(int index) {
return index * 2;
}
  
// returns index of right child of given index
private int rightChild(int index) {
return index * 2 + 1;
}
  
// returns true if the node at the given index has a parent (is not the root)
private boolean hasParent(int index) {
return index > 1;
}
  
// returns true if the node at the given index has a non-empty left child
private boolean hasLeftChild(int index) {
return leftChild(index) <= size;
}
  
// returns true if the node at the given index has a non-empty right child
private boolean hasRightChild(int index) {
return rightChild(index) <= size;
}
  
// switches the values at the two given indexes of the given array
private void swap(int[] a, int index1, int index2) {
int temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.stream.Collectors;

public class TestMain {

// method to remove Duplicates from priority queue

public static HeapIntPriorityQueue removeDuplicates(HeapIntPriorityQueue queue) {

Stack<Integer> auxiliary = new Stack<Integer>();

while (!queue.isEmpty()) {

int n = queue.remove();

auxiliary.push(n);

}

for(Integer no: auxiliary.stream().collect(Collectors.toSet())) {

queue.add(no);

}

return queue;

}

// main method to test data

public static void main(String[] args) {

HeapIntPriorityQueue obj = new HeapIntPriorityQueue();

obj.add(7);

obj.add(7);

obj.add(8);

obj.add(8);

obj.add(8);

obj.add(10);

obj.add(45);

obj.add(45);

obj = removeDuplicates(obj);

System.out.println(obj);

}

}

Please find formatted code and output in screenshot

Add a comment
Know the answer?
Add Answer to:
Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method in the HashIntSet class called addAll that accepts another hash set as a...

    Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

  • Implement the class MaxHeapPriorityQueue as a heap with the following operations using python: - ...

    Implement the class MaxHeapPriorityQueue as a heap with the following operations using python: - MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. - parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None - leftChild(index) returns the value of the left child of heap[index], returns None if there is no value - rightChild(index) returns...

  • add/ remove any methods to the program. please post new code and output Min Heap: public...

    add/ remove any methods to the program. please post new code and output Min Heap: public class MinHeap { private int[] Heap; private int size; private int maxsize; private static final int FRONT = 1; public MinHeap(int maxsize) { this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MIN_VALUE; } private int parent(int pos) { return pos / 2; } private int leftChild(int pos) { return (2 * pos); } private int rightChild(int pos) {...

  • Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the...

    Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the function headers would be the following: class MaxHeap { vector<int> data; public: MaxHeap() { // ... } int size() { // ... } int maxLookup() { // ... } void extractMax() { // ... } void insert(int data) { // ... } void remove(int index) { // ... } }; ======================== import java.util.Arrays; import java.util.Scanner; public class MaxHeap { Integer[] a; int size; //...

  • JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...

    JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...

  • Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new...

    Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...

  • JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of...

    JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of objects using a hash table. // The hash table uses separate chaining to resolve collisions. // Original from buildingjavaprograms.com supplements public class HashSet<E> { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry<E>[] elementData; private int size; // Constructs an empty set. @SuppressWarnings("unchecked") public HashSet() { elementData = new HashEntry[10]; size = 0; } // ADD METHODS HERE for exercise solutions: // Adds the...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT