Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Lesson8/src/ChainingHashTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
public class ChainingHashTable<Key, Value> {
private int M = 3;
private int size = 0;
private Object[] st = new Object[M];

private class Node {
private Key key;
private Value value;
private Node next;

public Node(Key key, Value value, Node next) {
this.key = key;
this.value = value;
this.next = next;
}
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

private int hash(Key key) {
return (key.hashCode() & 0x7fffffff) % M;
}

public Value get(Key key) {
if (key == null) {
throw new IllegalArgumentException("Ключ не может равняться Null");
}
int i = hash(key);
Node x = (Node) st[i];
while (x != null) {
if (key.equals(x.key)) {
return x.value;
}
x = x.next;
}
return null;
}

public boolean contains(Key key) {
return get(key) != null;
}

public void put(Key key, Value value) {
if (key == null) {
throw new IllegalArgumentException("Ключ не может равняться Null");
}
int i = hash(key);
Node x = (Node) st[i];
while (x != null) {
if (key.equals(x.key)) {
x.value = value;
return;
}
x = x.next;
}
st[i] = new Node(key, value, (Node) st[i]);
size++;
}

}
89 changes: 89 additions & 0 deletions Lesson8/src/LinearProbingHashTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
public class LinearProbingHashTable<Key, Value> {
private int M = 3;
private int size = 0;
private Object[] keys = new Object[M];
private Object[] values = new Object[M];

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

private int hash(Key key) {
return (key.hashCode() & 0x7fffffff) % M;
}

public boolean contains(Key key) {
return get(key) != null;
}


public Value get(Key key) {
if (key == null) {
throw new IllegalArgumentException("Ключ не может равняться Null");
}
for (int i = hash(key); keys[i] != null; i = (i + 1) % M) {
if (((Key) keys[i]).equals(key)) {
return (Value) values[i];
}
}
return null;
}

public void put(Key key, Value value) {
if (key == null) {
throw new IllegalArgumentException("Ключ не может равняться Null");
}
int i;
for (i = hash(key); keys[i] != null; i = (i + 1) % M) {
if (((Key) keys[i]).equals(key)) {
values[i] = value;
return;
}
}
keys[i] = key;
values[i] = value;
size++;

if (size == M) {
dynamicResize();
}
}

private void dynamicResize() {
int oldM = M;
M = nextPrimeNumber(M * 2);
Object[] tmpKeys = new Object[M];
Object[] tmpValues = new Object[M];
// int tmpSize = 0;
for (int i = 0; i < oldM; i++) {
// if (keys[i] == null) {
// continue;
// }
int newHash = hash((Key) keys[i]);
tmpKeys[newHash] = keys[i];
tmpValues[newHash] = values[i];
// tmpSize++;
}
keys = tmpKeys;
values = tmpValues;
// size = tmpSize;
}

private int nextPrimeNumber(int n) {
while (!isPrime(++n)) {
}
return n;
}

private boolean isPrime(int testNumber) {
for (int i = 2; i < testNumber; i++)
if (testNumber % i == 0)
return false;
return true;
}

}
31 changes: 31 additions & 0 deletions Lesson8/src/Loader8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Loader8 {
public static void main(String[] args) {
ChainingHashTable<Character, Integer> chainingHashTable = new ChainingHashTable<>();
chainingHashTable.put('A', 10);
chainingHashTable.put('B', 11);
chainingHashTable.put('C', 12);
chainingHashTable.put('D', 13);
chainingHashTable.put('E', 14);
chainingHashTable.put('F', 15);
chainingHashTable.put('G', 16);
chainingHashTable.put('H', 17);
chainingHashTable.put('J', 18);
chainingHashTable.put('K', 19);

LinearProbingHashTable<Character, Integer> hashTable = new LinearProbingHashTable<>();
hashTable.put('A', 10);
hashTable.put('B', 11);
hashTable.put('C', 12);
hashTable.put('D', 13);
hashTable.put('E', 14);
hashTable.put('F', 15);
hashTable.put('G', 16);
hashTable.put('H', 17);
hashTable.put('J', 18);
hashTable.put('S', 19);
hashTable.put('A', 100);

System.out.println(hashTable.get('A'));
System.out.println(hashTable.get('S'));
}
}