-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearProbing.java
164 lines (134 loc) · 3.76 KB
/
LinearProbing.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
public class LinearProbing extends Hashtable {
int capacity; // Number of elements that can be stored
int numOfElements = 0; // Number of elements in the array
// Default Constructor
public LinearProbing() {
this.capacity = 10;
}
// Signature Constructor
public LinearProbing(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 stopIndex;
int hashcode;
int newIndex;
// Creates new entry, hashcode, hashCompression
MapElements entry = new MapElements(key, value);
hashcode = entry.hashCode();
newIndex = hashCompression(hashcode, capacity);
// If there is something in that position then do this
if (occupied(newIndex)) {
stopIndex = newIndex;
// If first index is what we are looking for
if (hashtable[newIndex].getKeys() == key) {
oldValue = hashtable[newIndex].getValues();
hashtable[newIndex].setValues(value);
System.out.println(oldValue);
}
else {
newIndex++;
// Traverse until the stopping index
while (occupied(newIndex) && newIndex != stopIndex) {
collisions++;
if (hashtable[newIndex].getKeys() == key) {
oldValue = hashtable[newIndex].getValues();
hashtable[newIndex].setValues(value);
System.out.println(oldValue);
}
newIndex = (newIndex + 1) % 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);
}
// Get
public Character get(Integer k) {
// Index of hashFunction
int newIndex = findIndex(k);
if (newIndex == -1) {
return null;
}
return hashtable[newIndex].getValues();
}
// Remove
public Character remove(Integer key) {
// Attributes
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;
}
// FindIndex : finds the key through linear probing
private int findIndex(int key) {
int newIndex = hashCompression(key, capacity);
if (hashtable[newIndex] != null && hashtable[newIndex].getKeys() == key) {
return newIndex;
}
int stopIndex = newIndex;
if (newIndex == hashtable.length - 1) {
newIndex = 0;
} else {
newIndex++;
}
while (newIndex != stopIndex && hashtable[newIndex] != null && !(hashtable[newIndex].getKeys() == key)) {
newIndex = (newIndex + 1) % hashtable.length;
}
if (hashtable[newIndex] != null && hashtable[newIndex].getKeys() == key) {
return newIndex;
} else {
return -1;
}
}
// 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("Index is null");
} else {
System.out.println("Index " + i + ": " + hashtable[i].getValues());
}
}
}
}