-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashAVLSpellTable.java
More file actions
121 lines (110 loc) · 3.91 KB
/
HashAVLSpellTable.java
File metadata and controls
121 lines (110 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.LinkedList;
import java.util.List;
public class HashAVLSpellTable {
private LinkedList<AVLTree> buckets[];
private int tableSize;
private int numSpells=0;
private int capacity;
/**
* Constructor- creating link list and every element is null at start
*/
public HashAVLSpellTable(int size) {
buckets = new LinkedList[size];
capacity = size;
for (int i=0; i < size; i++){
buckets[i] = null;
}
}
/**
* Hash function
*/
private int hash(String category) {
int hash =0;
for (int i =0; i < category.length(); i++){
char ch = category.charAt(i);
hash = hash + (31 * (int)ch);
}
return hash % capacity;
}
/**
* Adding a new spell the hash table, if the hash index is taking, check if
* there is already the spell category and if yes add the spell his AVl tree category
* and if not then crate new avl tree as it own category
*/
public void addSpell(Spell s) {
numSpells++;
int index = hash(s.getCategory());
if (buckets[index] == null){ // not taken
LinkedList<AVLTree> new_link_list = new LinkedList<>(); // new link list to its index
AVLTree new_avl = new AVLTree(s); // new avl tree
new_link_list.add(new_avl);
buckets[index] = new_link_list;
}
else { // was taken
LinkedList<AVLTree> link_list = buckets[index];
boolean found_avl = false;
for (AVLTree avlTree : link_list) { // check spell category have already an avl tree
if (s.getCategory().equals(avlTree.getCategory())) { // if there is then adding the new spell to the avl tree
avlTree.insert(s);
found_avl = true;
break;
}
}
if (!found_avl){ // if there not then create new avl tree
AVLTree new_avl = new AVLTree(s);
link_list.add(new_avl);
}
}
}
/**
* Searching for the spell in the link list avl trees
*/
public Spell searchSpell(String category, String spellName, int powerLevel) {
int index = hash(category);
if (buckets[index] != null){
LinkedList<AVLTree> link_list = buckets[index];
for (AVLTree avlTree : link_list) {
if ((category.equals(avlTree.getCategory()))){
return avlTree.search(spellName, powerLevel); // found
}
}
}
return null; // not exits return null
}
/**
* return the nuber of spell in all the hash table
*/
public int getNumberSpells(){
return numSpells;
}
/**
* returning the number of spells in the avl tree category
*/
public int getNumberSpells(String category){
int index = hash(category);
if (buckets[index] != null){
LinkedList<AVLTree> link_list = buckets[index];
for (AVLTree avlTree : link_list) {
if ((category.equals(avlTree.getCategory()))){
return avlTree.getSize(); // retuning the avl tree size
}
}
}
return 0; // not exits return zero
}
/**
* retuning the biggest top k spells in the same category
*/
public List<Spell> getTopK(String category, int k) {
int index = hash(category);
if (buckets[index] != null){
LinkedList<AVLTree> link_list = buckets[index];
for (AVLTree avlTree : link_list) {
if ((category.equals(avlTree.getCategory()))) {
return avlTree.getTopK(k);
}
}
}
return null; // not exits
}
}