-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeperateChainingHashST.java
More file actions
69 lines (60 loc) · 1.88 KB
/
SeperateChainingHashST.java
File metadata and controls
69 lines (60 loc) · 1.88 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
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
public class SeperateChainingHashST<Key,Value>{
private int N;
private int M;
private BinarySearchST bst;
public SeperateChainingHashST(){SeperateChainingHashST(97);}
public SeperateChainingHashST(int M){
this.M=M;
bst=(BinarySearchST<Key,Value>[]) new BinarySearchST[M];
for (int i=0;i<M;i++){
bst[i]=new BinarySearchST();
}
}
public int hash(Key key){
return (key.hashCode() & 0x7fffffff) % M;
}
public void put(Key key,Value val){
int hash=hash(key);
bst[hash].put(key, val);
}
public Value get(Key key){
return (Value) bst[hash(key)].get(key);
}
public Iterable<Key> keys(){
return Iterable<Key> keys(0,M);
}
public Iterable<Key> keys(int low, int high) {
List<Key> keys=new ArrayList<Key>(M);
for (int i = 0; i <M; i++) {
lst=bst[i];
keys.add(lst.keys());
}
return keys;
}
public void show(){
for (int i=0;i<M;i++){
System.out.println(keys[i]+":"+values[i]);
}
}
public static void main(String[] args){
int N=97;
SeperateChainingHashST schst=new SeperateChainingHashST<>(N);
Random r = new Random();
//Integer[] a=new Integer [N];
String[] str={"a","b","c","d","e","f","g","h","i","j","k",
"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int pivot=0;
for(int i=0 ; i<N ; i++){
int ran1 = r.nextInt(100);
pivot=ran1;
int ran2 = r.nextInt(26);
String strran=str[ran2];
schst.put(ran1, strran);
}
schst.show();
System.out.println(schst.get(pivot));
}
}