diff --git a/Lesson8/src/ChainingHashTable.java b/Lesson8/src/ChainingHashTable.java new file mode 100644 index 0000000..7e3cf49 --- /dev/null +++ b/Lesson8/src/ChainingHashTable.java @@ -0,0 +1,66 @@ +public class ChainingHashTable { + 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++; + } + +} diff --git a/Lesson8/src/LinearProbingHashTable.java b/Lesson8/src/LinearProbingHashTable.java new file mode 100644 index 0000000..6e95147 --- /dev/null +++ b/Lesson8/src/LinearProbingHashTable.java @@ -0,0 +1,89 @@ +public class LinearProbingHashTable { + 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; + } + +} diff --git a/Lesson8/src/Loader8.java b/Lesson8/src/Loader8.java new file mode 100644 index 0000000..575e9c8 --- /dev/null +++ b/Lesson8/src/Loader8.java @@ -0,0 +1,31 @@ +public class Loader8 { + public static void main(String[] args) { + ChainingHashTable 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 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')); + } +}