forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapLinearProbing.java
More file actions
163 lines (144 loc) · 4.11 KB
/
HashMapLinearProbing.java
File metadata and controls
163 lines (144 loc) · 4.11 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
package DataStructures.HashMap.Hashing;
/**
* This class is an implementation of a hash table using linear probing
*
*/
public class HashMapLinearProbing {
private int hsize;
private Integer[] buckets;
private Integer AVAILABLE;
/**
* Constructor initializes buckets array, hsize, and creates dummy object for AVAILABLE
* @param hsize the desired size of the hash map
*/
public HashMapLinearProbing(int hsize) {
buckets = new Integer[hsize];
this.hsize = hsize;
AVAILABLE = new Integer(Integer.MIN_VALUE);
}
/**
* The Hash Function takes a given key and finds an index based on its data
* @param key the desired key to be converted
* @return int an index corresponding to the key
*/
public int hashing(int key) {
int hash = key % hsize;
if (hash < 0) {
hash += hsize;
}
return hash;
}
/**
* inserts the key into the hash map by wrapping it as an Integer object
* @param key the desired key to be inserted in the hash map
*/
public void insertHash(int key) {
Integer wrappedInt = new Integer(key);
int hash = hashing(key);
if(isFull()) {
System.out.println("Hash table is full");
return;
}
for (int i = 0;i < hsize; i++) {
if(buckets[hash] == null || buckets[hash] == AVAILABLE) {
buckets[hash] = wrappedInt;
return;
}
if(hash + 1 < hsize) {
hash++;
} else {
hash = 0;
}
}
}
/**
* deletes a key from the hash map and adds an available placeholder
* @param key the desired key to be deleted
*/
public void deleteHash(int key) {
Integer wrappedInt = new Integer(key);
int hash = hashing(key);
if(isEmpty()) {
System.out.println("Table is empty");
return;
}
for(int i = 0;i < hsize; i++) {
if(buckets[hash] != null && buckets[hash].equals(wrappedInt)) {
buckets[hash] = AVAILABLE;
return;
}
if(hash + 1 < hsize) {
hash++;
} else {
hash = 0;
}
}
System.out.println("Key " + key + " not found");
}
/**
* Displays the hash table line by line
*/
public void displayHashtable() {
for (int i = 0; i < hsize; i++) {
if(buckets[i] == null || buckets[i] == AVAILABLE) {
System.out.println("Bucket " + i + ": Empty");
} else {
System.out.println("Bucket " + i + ": " + buckets[i].toString());
}
}
}
/**
* Finds the index of location based on an inputed key
* @param key the desired key to be found
* @return int the index where the key is located
*/
public int findHash(int key) {
Integer wrappedInt = new Integer(key);
int hash = hashing(key);
if(isEmpty()) {
System.out.println("Table is empty");
return -1;
}
for(int i = 0;i < hsize; i++) {
if(buckets[hash].equals(wrappedInt)) {
buckets[hash] = AVAILABLE;
return hash;
}
if(hash + 1 < hsize) {
hash++;
} else {
hash = 0;
}
}
System.out.println("Key " + key + " not found");
return -1;
}
/**
* isFull returns true if the hash map is full and false if not full
* @return boolean is Empty
*/
public boolean isFull() {
boolean response = true;
for(int i = 0; i< hsize;i++) {
if(buckets[i] == null || buckets[i] == AVAILABLE) {
response = false;
break;
}
}
return response;
}
/**
* isEmpty returns true if the hash map is empty and false if not empty
* @return boolean is Empty
*/
public boolean isEmpty() {
boolean response = true;
for(int i = 0; i< hsize;i++) {
if(buckets[i] != null) {
response = false;
break;
}
}
return response;
}
}