Skip to content
Merged
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
129 changes: 129 additions & 0 deletions hash_table_code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
public class HashTable<K, V> {
private class HTPair {
K key;
V value;

public HTPair(K key, V value) {
this.key = key;
this.value = value;
}

public boolean equals(Object other) {
HTPair op = (HTPair) other;
return this.key.equals(op.key);
}

public String toString() {
return "{" + this.key + "->" + this.value + "}";
}
}

private LinkedList<HTPair>[] bucketArray;
private int size;

public static final int DEFAULT_CAPACITY = 5;

public HashTable() {
this(DEFAULT_CAPACITY);
}

public HashTable(int capacity) {
this.bucketArray = (LinkedList<HTPair>[]) new LinkedList[capacity];
this.size = 0;
}

public int size() {
return this.size;
}

private int HashFunction(K key) {
int hc = key.hashCode();
hc = Math.abs(hc);
int bi = hc % this.bucketArray.length;
return bi;
}

public void put(K key, V value) throws Exception {
int bi = HashFunction(key);
HTPair data = new HTPair(key, value);
if (this.bucketArray[bi] == null) {
LinkedList<HTPair> bucket = new LinkedList<>();
bucket.addLast(data);
this.size++;
this.bucketArray[bi] = bucket;
} else {
int foundAt = this.bucketArray[bi].find(data);
if (foundAt == -1) {
this.bucketArray[bi].addLast(data);
this.size++;
} else {
HTPair obj = this.bucketArray[bi].getAt(foundAt);
obj.value = value;
this.size++;
}
}
double lambda = (this.size) * 1.0;
lambda = this.size / this.bucketArray.length;
if (lambda > 0.75) {
rehash();
}
}

public void display() {
for (LinkedList<HTPair> list : this.bucketArray) {
if (list != null && !list.isEmpty()) {
list.display();
} else {
System.out.println("NULL");
}
}
}

public V get(K key) throws Exception {
int index = this.HashFunction(key);
LinkedList<HTPair> list = this.bucketArray[index];
HTPair ptf = new HTPair(key, null);
if (list == null) {
return null;
} else {
int findAt = list.find(ptf);
if (findAt == -1) {
return null;
} else {
HTPair pair = list.getAt(findAt);
return pair.value;
}
}
}

public V remove(K key) throws Exception {
int index = this.HashFunction(key);
LinkedList<HTPair> list = this.bucketArray[index];
HTPair ptf = new HTPair(key, null);
if (list == null) {
return null;
} else {
int findAt = list.find(ptf);
if (findAt == -1) {
return null;
} else {
HTPair pair = list.getAt(findAt);
list.removeAt(findAt);
this.size--;
return pair.value;
}
}
}

public void rehash() throws Exception {
LinkedList<HTPair>[] oba = this.bucketArray;
this.bucketArray = (LinkedList<HTPair>[]) new LinkedList[2 * oba.length];
this.size = 0;
for (LinkedList<HTPair> ob : oba) {
while (ob != null && !ob.isEmpty()) {
HTPair pair = ob.removeFirst();
this.put(pair.key, pair.value);
}
}
}
}
99 changes: 99 additions & 0 deletions knight__tour.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.util.*;

public class KnightsTour {
private final static int base = 12;
private final static int[][] moves = {{1,-2},{2,-1},{2,1},{1,2},{-1,2},
{-2,1},{-2,-1},{-1,-2}};
private static int[][] grid;
private static int total;

public static void main(String[] args) {
grid = new int[base][base];
total = (base - 4) * (base - 4);

for (int r = 0; r < base; r++)
for (int c = 0; c < base; c++)
if (r < 2 || r > base - 3 || c < 2 || c > base - 3)
grid[r][c] = -1;

int row = 2 + (int) (Math.random() * (base - 4));
int col = 2 + (int) (Math.random() * (base - 4));

grid[row][col] = 1;

if (solve(row, col, 2))
printResult();
else System.out.println("no result");

}

private static boolean solve(int r, int c, int count) {
if (count > total)
return true;

List<int[]> nbrs = neighbors(r, c);

if (nbrs.isEmpty() && count != total)
return false;

Collections.sort(nbrs, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[2] - b[2];
}
});

for (int[] nb : nbrs) {
r = nb[0];
c = nb[1];
grid[r][c] = count;
if (!orphanDetected(count, r, c) && solve(r, c, count + 1))
return true;
grid[r][c] = 0;
}

return false;
}

private static List<int[]> neighbors(int r, int c) {
List<int[]> nbrs = new ArrayList<>();

for (int[] m : moves) {
int x = m[0];
int y = m[1];
if (grid[r + y][c + x] == 0) {
int num = countNeighbors(r + y, c + x);
nbrs.add(new int[]{r + y, c + x, num});
}
}
return nbrs;
}

private static int countNeighbors(int r, int c) {
int num = 0;
for (int[] m : moves)
if (grid[r + m[1]][c + m[0]] == 0)
num++;
return num;
}

private static boolean orphanDetected(int cnt, int r, int c) {
if (cnt < total - 1) {
List<int[]> nbrs = neighbors(r, c);
for (int[] nb : nbrs)
if (countNeighbors(nb[0], nb[1]) == 0)
return true;
}
return false;
}

private static void printResult() {
for (int[] row : grid) {
for (int i : row) {
if (i == -1)
continue;
System.out.printf("%2d ", i);
}
System.out.println();
}
}
}