Java 使用AVL Tree findAll()方法实现字典
我正在尝试使用AVL树实现字典ADT,但findAllK键方法有问题。我知道如何使用entries方法并遍历每个方法,但是我需要我的方法是Ologn,所以我需要使用treeSearchkey,position方法。 我的字典是这样的: 公共类FoodDictionary,V>实现字典{Java 使用AVL Tree findAll()方法实现字典,java,dictionary,tree,avl-tree,Java,Dictionary,Tree,Avl Tree,我正在尝试使用AVL树实现字典ADT,但findAllK键方法有问题。我知道如何使用entries方法并遍历每个方法,但是我需要我的方法是Ologn,所以我需要使用treeSearchkey,position方法。 我的字典是这样的: 公共类FoodDictionary,V>实现字典{ /** List in which all the entries are stored. */ private AVLTree<K,V> dictionary; /** Create a new,
/** List in which all the entries are stored. */
private AVLTree<K,V> dictionary;
/** Create a new, empty unordered Dictionary. */
public FoodDictionary() {
dictionary = new AVLTree<K,V>();
}
/**
* FOR TESTING PURPOSES ONLY!
*/
protected AVLTree<K,V> getTree(){
return dictionary;
}
/**
* Returns an iterable object of all the entries in the tree
* @returns the tree, an iterable object
*/
public Iterable<Entry<K, V>> entries() {
return dictionary;
}
/**
* Returns the first entry found with the matching
* key.
* @param key the key to be found
* @return the first entry found
*/
public Entry<K, V> find(K key) {
return dictionary.find(key).element();
}
/**
* Returns all of the entries with the given key
*
* @param key the key to be found
* @return an iterable object containing all the
* entries found with the given key
*/
public Iterable<Entry<K, V>> findAll(K key) {
PLSequence<Entry<K,V>> list = new PLSequence<Entry<K,V>>();
for(Entry<K,V> entry : entries()){
if(entry.getKey().equals(key)){
list.addLast(entry);
}
}
return list;
}
/**
* Returns the size of the dictionary
* @return the size of the dictionary
*/
@Override
public int size() {
return dictionary.size();
}
/**
* Returns whether or not the dictionary
* is empty
* @return true if the dictionary is empty
* false if the dictionary is not empty
*/
@Override
public boolean isEmpty() {
return dictionary.isEmpty();
}
/**
* Adds an entry to this dictionary
* @param key they key of the new entry
* @param value the value of the new entry
* @return the new entry added to the dictionary
* @throws InvalidKeyException if the given key was not valid
*/
@Override
public Entry<K, V> insert(K key, V value) throws InvalidKeyException {
return dictionary.add(key, value);
}
/**
* Removes an entry from this dictionary
* @param e the entry to be removed
* @return the entry removed or null if it was not found
* @throws InvalidEntryException if the entry input was invalid
*/
@Override
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
return dictionary.remove(e.getKey());
}
}
目前,正如我所说,我的findAll正在使用条目进行迭代,但我需要以某种方式使用treeSearch
有人能帮我完成这个实现吗?我没有太多时间来完成它,我完全沉浸在想法中。
感谢您的帮助