-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
243 lines (173 loc) · 6.53 KB
/
HashTable.java
File metadata and controls
243 lines (173 loc) · 6.53 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import java.lang.Math; // for the power function
import java.util.Arrays;
/**
* Author: Norhan Abbas
* Date: 31st of May, 2019
*
* Overview: this program implements hash table
* it allows the following:
*
* 1- Insertion by Linear probing
* 2- Insertion by Quadratic probing
* 3- delete an integer and returning the array without that integer
* 4- Search for an integer and get to know if it exists (True)
* Or it is not there (False)
*
* All the above take place by the user choice,
* Also, the loading factor of our table is always taken into consideration
* WHILE INSERTING ANY INTEGER
*
* NB. In this program, any Zero is considered to be a NULL element
* Accordingly, the user should insert any integer OTHER THAN ZERO
*/
public class HashTable {
//TODO: insert function -Quadratic probing
public static int[] insertQuadratic(int[] arr, int val, int key, int x){
x++;
if (key < arr.length) {
if (arr[key] == 0) {
arr[key] = val;
return arr;
}
else if (arr[key] != 0) {
key += Math.pow(x, 2);
insertQuadratic(arr, val, key, x);
}
}
else if (key >= arr.length){
key = key - arr.length;
insertQuadratic(arr, val, key, x);
}
return arr;
}
/***********************************************************************************/
//TODO: insert function -Linear probing
public static int[] insertLinear(int[] arr, int val, int key){
if (key < arr.length) {
// base case
if (arr[key] == 0) {
arr[key] = val;
return arr;
}
else if (arr[key] != 0){
key++;
insertLinear(arr, val, key);
}
}
else if (key >= arr.length){
key = key - arr.length;
insertLinear(arr, val, key);
}
return arr;
}
/***********************************************************************************/
//TODO: Search function
public static boolean search(int[] arr, int val, int i){
if (i < arr.length) {
if (arr[i] == val) {
return true;
} else {
i++;
return search(arr, val, i);
}
}
return false;
}
static boolean searchGo(int[]arr, int val){
return search(arr, val, 0);
}
/***********************************************************************************/
//TODO: delete function
public static int[] delete(int[] arr, int val){
int index = 0;
if (searchGo(arr, val) == true) { // if the value entered by the user exists
int[] newArray = new int[arr.length - 1];
for (int y = 0; y < arr.length; y++)
{
if (arr[y] == val) {
index = y;
break;
}
}
for (int i = 0, k = 0; i < arr.length; i++) {
if (i == index) {
arr[i] = 0;
continue;
}
newArray[k] = arr[i];
}
System.out.println("After deletion");
//System.out.println(Arrays.toString(newArray));
/* for (int i = 0; i < newArray.length; i++) {
System.out.println(newArray[i]);
}*/
return newArray;
}
System.out.println("The value inserted DOES NOT EXIST");
return arr;
}
/***********************************************************************************/
//TODO: resize function
public static void reSize(int[] arr, String userChoice, int value, int key){
// the new array is double the size
int[] newArr = new int[arr.length * 2];
double s = 0;
// counting how many integers in my array are not ZERO
// TAKEN INTO CONSIDERATION THAT in this program, Zero is being dealt with as if it was NULL
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
s++;
}
}
// calculate the loading factor
s = s/arr.length;
// resize happens ONLY when the loading factor is greater than 0.8
if (s > 0.8) {
//key = value%newArr.length;
switch (userChoice) {
case "IL":
for (int i = 0; i < arr.length; i++) {
key = arr[i]%newArr.length;
insertLinear(newArr, arr[i], key);
}
key = value%newArr.length; // my hash function
insertLinear(newArr, value, key);
break;
case "IQ":
for (int i = 0; i < arr.length; i++) {
key = arr[i]%newArr.length;
// x starts from 2 as key + (1^2) has been already included in linear probing
insertQuadratic(newArr, arr[i], key, 2);
}
key = value%newArr.length; // my hash function
insertLinear(newArr, value, key);
break;
}
System.out.println("PS. the loading factor is greater than 0.8");
System.out.println("Accordingly, resizing is needed\n");
System.out.println("After rehashing again");
System.out.println(Arrays.toString(newArr));
}
else {
// No resizing needed if S less than or equal to 0.8
switch (userChoice) {
case "IL":
key = value%arr.length; // my hash function
insertLinear(arr, value, key);
break;
case "IQ":
key = value%arr.length; // my hash function
insertQuadratic(arr, value, key, 1);
break;
}
System.out.println("PS. the loading factor is less than or equal to 0.8");
System.out.println("Accordingly, resizing is NOT needed\n");
System.out.println("After rehashing again");
System.out.println(Arrays.toString(arr));
}
/* System.out.println("After resizing");
for (int i = 0; i < newArr.length; i++) {
System.out.println(newArr[i]);
}*/
}
}