Adds linkedlist, growable array and chainin hash table

This commit is contained in:
Abhinav Sarkar 2019-06-26 21:47:56 +05:30
commit a75f826e1b
6 changed files with 333 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
*.iml

24
pom.xml Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.abhinavsarkar.java</groupId>
<artifactId>argorist</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,15 @@
package net.abhinavsarkar;
import net.abhinavsarkar.algorist.LinkedList;
public class Algorist
{
// BST with all ops
// Red-black tree
// Hash Table - open addressing and chaining
// Linked BST
// Ranked BST
}

View File

@ -0,0 +1,108 @@
package net.abhinavsarkar.algorist;
import java.util.Optional;
public class ChainingHashTable<K, V>
{
private Object[] store;
public ChainingHashTable(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException("Capacity too small");
}
this.store = new Object[capacity];
}
public Optional<V> get(K key) {
int index = calcIndex(key);
LinkedList<Entry<K, V>> entries = (LinkedList<Entry<K, V>>) store[index];
if (entries == null) {
return Optional.empty();
} else {
return entries.forEach(h -> {
if (h.key == key) {
return Optional.of(h.val);
}
return Optional.empty();
});
}
}
public void put(K key, V val) {
int index = calcIndex(key);
LinkedList.Node<Entry<K, V>> vNode = new LinkedList.Node<>(new Entry<>(key, val), null);
if (store[index] == null) {
store[index] = new LinkedList<>(vNode);
} else {
LinkedList<Entry<K, V>> entries = (LinkedList<Entry<K, V>>) store[index];
Optional<Integer> ret = entries.forEach(h -> {
if (h.key == key)
{
h.val = val;
return Optional.of(0);
}
return Optional.empty();
});
if (!ret.isPresent())
{
entries.prepend(vNode);
}
}
}
private int calcIndex(K key)
{
return key.hashCode() % store.length;
}
private static class Entry<K, V> {
private K key;
private V val;
public Entry(K key, V val) {
this.key = key;
this.val = val;
}
public String toString() {
return "<" + key + "," + val + ">";
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < store.length; i++) {
LinkedList<Entry<K, V>> entries = (LinkedList<Entry<K, V>>) store[i];
if (entries != null)
{
sb.append("<" + i + ": " + entries + ">, ");
}
}
sb.delete(sb.length()-2, sb.length());
return "{" + sb + "}";
}
public static void main(String[] args)
{
ChainingHashTable<String, String> table = new ChainingHashTable<>(20);
table.put("a", "abhinav");
table.put("b", "batman");
table.put("c", "carol");
table.put("c", "carly");
table.put("z", "zellman");
table.put("w", "walker");
System.out.println(table.get("a"));
System.out.println(table.get("b"));
System.out.println(table.get("c"));
System.out.println(table.get("A"));
System.out.println(table.get("B"));
System.out.println(table.get("C"));
System.out.println(table);
}
}

View File

@ -0,0 +1,70 @@
package net.abhinavsarkar.algorist;
public class GrowableArray<T>
{
private Object[] store;
public GrowableArray(int initialCapacity) {
if (initialCapacity <= 0) {
throw new IllegalArgumentException("capacity too small");
}
this.store = new Object[initialCapacity];
}
public T get(int index) {
if (index < 0) {
throw new IllegalArgumentException("index too small");
}
if (index >= store.length) {
throw new IllegalArgumentException("index too large");
}
return (T) this.store[index];
}
public void set(int index, T val) {
if (index < 0) {
throw new IllegalArgumentException("index too small");
}
if (index >= store.length) {
int factor = (int) Math.pow(2, Math.ceil(Math.log(index * 1.0 /store.length)/Math.log(2)));
Object[] nStore = new Object[store.length * factor];
System.arraycopy(store, 0, nStore, 0, store.length);
this.store = nStore;
}
this.store[index] = val;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < store.length; i++) {
T val = (T) this.store[i];
if (val == null)
{
sb.append("<NULL>, ");
} else {
sb.append(val + ", ");
}
}
sb.delete(sb.length()-2, sb.length());
return "[" + sb.toString() + "]";
}
public static void main(String[] arg) {
GrowableArray<String> array = new GrowableArray<>(2);
array.set(0, "a");
array.set(6, "b");
array.set(9, "c");
array.set(7, "x");
System.out.println(array);
System.out.println(array.get(0));
System.out.println(array.get(1));
}
}

View File

@ -0,0 +1,114 @@
package net.abhinavsarkar.algorist;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
public class LinkedList<T>
{
private Node<T> head;
public LinkedList(Node<T> head)
{
Objects.nonNull(head);
this.head = head;
}
public static class Node<T>
{
private T value;
private Node<T> next;
public Node(T value, Node<T> next)
{
this.value = value;
this.next = next;
}
}
public T head() {
return head.value;
}
public LinkedList<T> tail() {
return head.next != null ? new LinkedList<>(head.next) : null;
}
public void prepend(Node<T> vNode)
{
vNode.next = head;
head = vNode;
}
public <U> Optional<U> forEach(Function<T, Optional<U>> f) {
LinkedList<T> l = this;
LinkedList<T> t;
do {
t = l.tail();
T h = l.head();
Optional<U> r = f.apply(h);
if (r.isPresent()) {
return r;
}
l = t;
} while (t != null);
return Optional.empty();
}
public void reverse()
{
Node<T> curr = head;
if (curr == null)
{
return;
}
Node<T> prev = null;
Node<T> next = curr.next;
while (next != null)
{
curr.next = prev;
prev = curr;
curr = next;
next = curr.next;
}
curr.next = prev;
head = curr;
}
@Override
public String toString()
{
return "[" + toStringInternal(head) + "]";
}
private String toStringInternal(Node<T> node)
{
if (node == null)
{
return "";
}
else if (node.next == null)
{
return node.value.toString();
}
else
{
return node.value + ", " + toStringInternal(node.next);
}
}
public static void main(String[] args)
{
LinkedList<Integer> integers = new LinkedList<>(
new LinkedList.Node<>(1, new LinkedList.Node<>(2, new LinkedList.Node<>(3, null))));
System.out.println(integers);
integers.reverse();
System.out.println(integers);
}
}