-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuadraticProbing.java
182 lines (139 loc) · 3.68 KB
/
QuadraticProbing.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
public class QuadraticProbing extends Hashtable {
int capacity;
int numOfElements;
// Default Constructor
public QuadraticProbing() {
this.capacity = 10;
}
// Signature Constructor
public QuadraticProbing(int capacity) {
this.capacity = capacity;
hashtable = new MapElements[capacity];
}
// size
public int size() {
return numOfElements;
}
// isEmpty
public boolean isEmpty() {
return (numOfElements == 0);
}
// Put Method
public void put(Integer key, Character value) {
// Attributes
int sizeBefore = numOfElements;
int collisions = 0;
char oldValue;
int currentIndex;
int hashcode;
int newIndex;
int index;
int k = 0;
// Creates new entry, hashcode, hashCompression
MapElements entry = new MapElements(key, value);
hashcode = entry.hashCode();
newIndex = hashCompression(hashcode, capacity);
index = newIndex;
// If there is something in that position then do this
if (occupied(newIndex)) {
currentIndex = newIndex;
if (hashtable[newIndex].getKeys() == key) {
oldValue = hashtable[newIndex].getValues();
hashtable[newIndex].setValues(value);
System.out.println(oldValue);
}
else {
k++;
newIndex = (index + (k * k)) % hashtable.length;
while (occupied(newIndex)) {
collisions++;
if (hashtable[newIndex].getKeys() == key) {
oldValue = hashtable[newIndex].getValues();
hashtable[newIndex].setValues(value);
System.out.println(oldValue);
}
k++;
newIndex = (index + (k * k)) % hashtable.length;
}
if (!occupied(newIndex)) {
hashtable[newIndex] = entry;
numOfElements++;
}
}
}
// Is the position is empty then add new entry.
else {
hashtable[newIndex] = entry;
numOfElements++;
}
System.out.println("Size of table: " + sizeBefore);
System.out.println("Number of Elements after putting " + numOfElements);
System.out.println("Number of probing attempts: " + collisions);
}
// getMethod
public Character get(Integer k) {
int newIndex = findIndex(k);
if (newIndex == -1) {
return null;
}
return hashtable[newIndex].getValues();
}
private int findIndex(int key) {
int k = 0;
int newIndex = hashCompression(key, capacity);
int index = newIndex;
if (hashtable[newIndex] != null && hashtable[newIndex].getKeys() == key) {
return newIndex;
}
if (newIndex == hashtable.length - 1) {
newIndex = 0;
}
else {
k += 1;
newIndex = hashCompression((index + (k * k)), capacity);
}
while ((hashtable[newIndex] != null) && !(hashtable[newIndex].getKeys() == key)) {
k += 1;
newIndex = hashCompression((index + (k * k)), capacity);
}
if (hashtable[newIndex] != null && hashtable[newIndex].getKeys() == key) {
return newIndex;
}
else {
return -1;
}
}
// Remove
public Character remove(Integer key) {
char oldValue;
int newIndex = findIndex(key);
if (newIndex == -1) {
return null;
}
oldValue = hashtable[newIndex].getValues();
hashtable[newIndex] = null;
MapElements[] oldHashtable = hashtable;
hashtable = new MapElements[oldHashtable.length];
for (int i = 0; i < oldHashtable.length; i++) {
if (oldHashtable[i] != null) {
MapElements map = new MapElements(oldHashtable[i].getKeys(), oldHashtable[i].getValues());
hashtable[i] = map;
}
}
return oldValue;
}
// Check if the position is occupied or not
private boolean occupied(int index) {
return hashtable[index] != null;
}
// Prints out the hashtable
public void printHashtable() {
for (int i = 0; i < hashtable.length; i++) {
if (hashtable[i] == null) {
System.out.println("empty");
} else {
System.out.println("Position " + i + ": " + hashtable[i].getValues());
}
}
}
}