【发布时间】:2016-11-27 23:43:42
【问题描述】:
所以我试图创建一个基于 AVL 树和哈希表的智能数据结构。
我确定我需要首先检查数据类型将具有哪种实现,具体取决于提供给它的列表的大小。
例如,如果我有一个大小为 1000 的列表 n,它将使用哈希表来实现。对于超过 1000 的任何内容,使用 AVL 树。
代码:
public class SmartULS<K,V> {
protected TreeMap<K,V> tree = new TreeMap<>();
protected AbstractHashMap<K,V> hashMap = new AbstractHashMap<K,V>();
public void setSmartThresholdULS(size){
int threshold = 1000;
if (size >= threshold) {
map = new AbtractMap<K,V>();
}
else
map = new TreeMap<K,V>();
}
}
现在,我应该编写标准方法,例如
get(SmartULS, Key), add(SmartULS, Key, Value), remove(SmartULS,Key), nextKey(Key), previousKey(Key)等
我真的不知道如何开始这个?我考虑过像这样创建这些方法(用伪编写):
Algorithm add(SmartULS, Key, Value):
i<- 0
If SmartULS instanceof AbstractHashMap then
For i to SmartULS.size do
If Key equals to SmartULS[i] then
SmartULS.get(Key).setValue(Value)
Else
SmartULS.add(Key, Value)
Else if SmartULS instanceof TreeMap then
Entry newAdd equals new MapEntry(Key, Value)
Position<Entry> p = treeSearch(root( ), Key)
【问题讨论】:
-
请阅读How to Ask
-
@EngineerDollery 与其发帖,不如帮助那个人。
标签: java class tree hashtable abstract-data-type