///this is the Entry /* * Copyright 2014, Michael T. Goodrich, Roberto Tamassia,
ID: 3737435 • Letter: #
Question
///this is the Entry
/*
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
*
* Developed for use with the book:
*
* Data Structures and Algorithms in Java, Sixth Edition
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
* John Wiley & Sons, 2014
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.datastructures;
/**
* Interface for a key-value pair.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*/
public interface Entry<K,V> {
/**
* Returns the key stored in this entry.
* @return the entry's key
*/
K getKey();
/**
* Returns the value stored in this entry.
* @return the entry's value
*/
V getValue();
}
import net.datastructures.Entry;
public class MyMaxPriorityQueue<K, V> {
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Entry<K, V> insert(K key, V value) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public Entry<K, V> max() {
// TODO Auto-generated method stub
return null;
}
@Override
public Entry<K, V> removeMax() {
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
import net.datastructures.Entry; import java.util.Comparator; import java.util.SortedSet; import java.util.TreeSet; public class MyMaxPriorityQueue { SortedSet set; public MyMaxPriorityQueue() { set = new TreeSet(new Comparator() { @Override public int compare(Entry o1, Entry o2) { return o2.getKey().compareTo(o1.getKey()); } }); } public int size() { return set.size(); } public Entry insert(K key, V value) throws IllegalArgumentException { final K k = key; final V v = value; Entry entry = new Entry() { @Override public K getKey() { return k; } @Override public V getValue() { return v; } }; set.add(entry); return entry; } public Entry max() { return set.first(); } public Entry removeMax() { Entry entry = set.first(); set.remove(entry); return entry; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.