-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLinearProbing.java
More file actions
136 lines (115 loc) · 4.27 KB
/
LinearProbing.java
File metadata and controls
136 lines (115 loc) · 4.27 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
// Program by UZAIR HUSSAIN
// Github: github.com/uzairhussain193
// LinkedIn: linkedin.com/in/uzairhussain19
public class LinearProbing {
private int size; // the number of key-value pairs currently stored
private int capacity; // the size of hashtable
private Object[] keys; // array to store keys
private Object[] values; // array to store values
public LinearProbing(int capacity){
this.capacity = capacity;
keys = new Object[capacity];
values = new Object[capacity];
} // end of constructor
public int size(){
return size;
} // end of size()
public boolean isEmpty(){
return (size == 0);
} // end of isEmpty
public void makeHashTableEmpty(){
size = 0;
keys = new Object[capacity];
values = new Object[capacity];
} // end of makeHashTableEmpty()
public boolean contains(Object key){
return (get(key) != null); // if no value is retreived, it means key doesn't exist
} // end of contains()
public int hash(Object key){
return (key.hashCode() & 0x7FFFFFFF) % capacity; // always gives +ve integer
// return Math.abs(key.hashCode())%capacity; // may give -ve integer
} // end of hash()
public void rehash(){
int newCap = capacity * 2;
Object [] oldKeys = keys;
Object [] oldValues = values;
// re intialize the arrays of key value pair, and size=0
keys = new Object[newCap];
values = new Object[newCap];
size = 0;
capacity = newCap;
for (int i=0; i<oldKeys.length; i++){
if (oldKeys[i]!=null){
put(oldKeys[i], oldValues[i]);
}
}
} // end of rehash()
public void put(Object key, Object value){
// checking load factor
if ((double)size / capacity > 0.75) {
rehash();
}
int index = hash(key);
while (keys[index]!=null && !keys[index].equals(key)){ // loop till end and until value associated with key is not found
index = (index + 1) % capacity; // Move to the next index using linear probing
}
if (keys[index] == null){
size++;
}
keys[index] = key;
values[index] = value;
} // end of put()
public Object get(Object key){
int index = hash(key);
while (keys[index] != null && !keys[index].equals(key)) { // loop till end and until value associated with key is not found
index = (index + 1) % capacity; // Move to the next index using linear probing
}
if (keys[index] == null) {
return null;
} else {
return values[index];
}
// we can use below line for above if conditions
// return keys[index] == null ? null : values[index]; // Return the value associated with the key, or null if the key is not found
} // end of get()
public void display() {
for (int i = 0; i < capacity; i++) {
if (keys[i] != null) {
System.out.println(keys[i] + " -> " + values[i]);
}
}
}
public void remove(Object key){
if (!contains(key)){
return;
}
int index = hash(key);
while (keys[index]!=null && !keys[index].equals(key)){
index = (index + 1) % capacity;
}
keys[index] = values[index] = null;
size--;
// for (int i= index ; keys[i]!=null; i = (index+1) % capacity ){
// // Object temp1 = keys[i], temp2 = values[i];
// keys[index] = keys[i];
// values[index] = values[i];
// keys[i] = values[i] = null;
// index = i;
// }
} // end of remove()
public static void main(String[] args) {
LinearProbing lp = new LinearProbing(6);
lp.put(5, "Asad");
lp.put(1, "AJ Styles");
lp.put(7, "Uzair Hussain");
lp.put(2, "Usman");
lp.put(5, "Ertugrul");
System.out.println(lp.get(5));
System.out.println(lp.contains(2));
System.out.println(lp.contains(3));
System.out.println(lp.size());
lp.remove(7);
System.out.println(lp.size());
lp.display();
}
}