1 2006 74 sakai keiichikochitech ac jp http

  • Slides: 23
Download presentation
アルゴリズムとデータ構造 1 2006年 7月4日 酒居敬一(sakai. keiichi@kochi-tech. ac. jp) http: //www. info. kochi-tech. ac. jp/k

アルゴリズムとデータ構造 1 2006年 7月4日 酒居敬一(sakai. keiichi@kochi-tech. ac. jp) http: //www. info. kochi-tech. ac. jp/k 1 sakai/Lecture/ALG/2006/index. html

public class My. Hashtable { private My. Hashtable() { } public My. Hashtable(int a.

public class My. Hashtable { private My. Hashtable() { } public My. Hashtable(int a. Max. Size) { this. table = new Address. Data[a. Max. Size]; } public Address. Data get(String a. Key) { if(null == a. Key){ throw new Null. Pointer. Exception(); } return this. table[this. calculate. Hash. Code(a. Key)]; } public void print. All() { for(int count = 0; count < this. table. length; count++){ System. out. println(count+1 + "t" + this. table[count]); } System. out. println(); } private Address. Data[] table; }

public boolean put(Address. Data an. Address. Data) { if(null == an. Address. Data){ return

public boolean put(Address. Data an. Address. Data) { if(null == an. Address. Data){ return false; } this. table[this. calculate. Hash. Code(an. Address. Data. get. Name())] = an. Address. Data; return true; [sakai@star]$ java My. Hashtable. Test } 住所データを格納 public boolean remove(String a. Key) 1 null { 2 null if(null == a. Key){ 3 null return false; 4 杉山: 稲城, 東京 208 } 5 null this. table[this. calculate. Hash. Code(a. Key)] = null; 6 null return true; 7 ONGS Inc. : 渋谷, 東京 151 } 8 後藤: 川崎, 神奈川 214 private int calculate. Hash. Code(String a. Key) 9 null { 10 null if(null == a. Key){ 11 佐々木: 座間, 神奈川 228 throw new Null. Pointer. Exception(); 12 小澤: 多摩, 東京 206 } 13 null intkey = 0; null for(int count = 0; count < a. Key. length(); count++){ 14 15 null intkey += 0 x. FFFF & a. Key. char. At(count); 16 null } 17 null return intkey % this. table. length; 18 null } 19 null

public class Address. Data { public Address. Data(String a. Name, String a. Metropolice, String

public class Address. Data { public Address. Data(String a. Name, String a. Metropolice, String a. City, String a. Zipcode) { if((null == a. Name) || (null == a. Metropolice) || (null == a. City)|| (null == a. Zipcode)){ throw new Null. Pointer. Exception(); } this. name = a. Name; this. metropolice = a. Metropolice; 住所録として以下の項目を持つ this. city = a. City; • 名前 this. zipcode = a. Zipcode; • 市 } • 都道府県 public String get. Name() { • 郵便番号 return this. name; } public String get. Address() { return this. city + ", " + this. metropolice + " " + this. zipcode; } public String to. String() { return this. name + ": " + this. city + ", " + this. metropolice + “ " + this. zipcode; } private String city; private String metropolice; private String name; private String zipcode; }

public class Chain. Hashtable { private Chain. Hashtable() { } } public Chain. Hashtable(int

public class Chain. Hashtable { private Chain. Hashtable() { } } public Chain. Hashtable(int a. Max. Size) { this. table = new My. Linked. List[a. Max. Size]; } private My. Linked. List[] table; public boolean put(Address. Data an. Address. Data) { if(null == an. Address. Data){ return false; } int hash. Code = this. calculate. Hash. Code(an. Address. Data. get. Name()); if(null == this. table[hash. Code]){ this. table[hash. Code] = new My. Linked. List(); } this. table[hash. Code]. insert(an. Address. Data); return true; }

public Address. Data get(String a. Key) { if(null == a. Key){ throw new Null.

public Address. Data get(String a. Key) { if(null == a. Key){ throw new Null. Pointer. Exception(); } My. Linked. List list = this. table[this. calculate. Hash. Code(a. Key)]; if(null == list){ return null; } int limit = list. size(); int count = 1; Address. Data address = null; while(count <= limit){ address = (Address. Data)list. get(count); if(address. get. Name(). equals(a. Key)){ return address; } private int calculate. Hash. Code(String a. Key) ++count; { } if(null == a. Key){ return null; throw new Null. Pointer. Exception(); } } intkey = 0; for(int count = 0; count < a. Key. length(); count++){ intkey += 0 x. FFFF & a. Key. char. At(count); } return intkey % this. table. length; }

public boolean remove(String a. Key) { if(null == a. Key){ return false; } My.

public boolean remove(String a. Key) { if(null == a. Key){ return false; } My. Linked. List list = this. table[this. calculate. Hash. Code(a. Key)]; if(null == list){ return false; } int limit = list. size(); int count = 1; Address. Data address = null; while(count <= limit){ address = (Address. Data)list. get(count); if(address. get. Name(). equals(a. Key)){ return list. remove(count); } public void print. All() ++count; { } for(int count = 0; count < this. table. length; count++){ return false; if(null == this. table[count]){ } System. out. println(this. table[count]); }else{ this. table[count]. print. All(); } } System. out. println(); }

public class Open. Address. Hashtable { public Open. Address. Hashtable(int a. Max. Size) {

public class Open. Address. Hashtable { public Open. Address. Hashtable(int a. Max. Size) { this. table = new Address. Data[a. Max. Size]; } private final Address. Data removed. Data = new Address. Data("", "", ""); private Address. Data[] table; private int calculate. Hash. Code(String a. Key) } { 1回目のハッシュは剰余演算 2回目以降は一定間隔離す 距離は前の値から1~ 3 1固定の場合は線形走査法と同じ 固定値にしないときは二重ハッシュ法 } if(null == a. Key){throw new Null. Pointer. Exception(); } intkey = 0; for(int count = 0; count < a. Key. length(); count++){ intkey += 0 x. FFFF & a. Key. char. At(count); } return intkey % this. table. length; private int calculate. Hash. Code. Again(String a. Key, int a. Hash. Code) { if(null == a. Key){throw new Null. Pointer. Exception(); } intkey = 0; for(int count = 0; count < a. Key. length(); count++){ intkey += 0 x. FFFF & a. Key. char. At(count); } int rehash. Code = (a. Hash. Code + 3 - (intkey % 3)) % this. table. length; System. err. println("再ハッシュ: " + a. Key + " " + a. Hash. Code + “ → " + rehash. Code); return rehash. Code; }

public boolean put(Address. Data an. Address. Data) { if(null == an. Address. Data){ return

public boolean put(Address. Data an. Address. Data) { if(null == an. Address. Data){ return false; } int hash. Code = this. calculate. Hash. Code(an. Address. Data. get. Name()); if((null == this. table[hash. Code]) || (this. removed. Data == this. table[hash. Code])){ this. table[hash. Code] = an. Address. Data; return true; } int limit = this. table. length -1; String key = an. Address. Data. get. Name(); for(int count = 0; count < limit; count++){ hash. Code = this. calculate. Hash. Code. Again(key, hash. Code); if((null == this. table[hash. Code]) || (this. removed. Data == this. table[hash. Code])){ this. table[hash. Code] = an. Address. Data; return true; } } return false; } 1回目のハッシュで衝突が起きたときは 衝突が起きなくなるまで別のハッシュ関 数でハッシュしなおす。

public Address. Data get(String a. Key) { if(null == a. Key){ throw new Null.

public Address. Data get(String a. Key) { if(null == a. Key){ throw new Null. Pointer. Exception(); } int hash. Code = this. calculate. Hash. Code(a. Key); if(null == this. table[hash. Code]){ return null; } if(this. removed. Data != this. table[hash. Code]){ if(this. table[hash. Code]. get. Name(). equals(a. Key)){ return this. table[hash. Code]; } } int limit = this. table. length -1; for(int count = 0; count < limit; count++){ hash. Code = this. calculate. Hash. Code. Again(a. Key, hash. Code); if(null == this. table[hash. Code]){ return null; } if(this. removed. Data != this. table[hash. Code]){ if(this. table[hash. Code]. get. Name(). equals(a. Key)){ return this. table[hash. Code]; } } } return null; }

public boolean remove(String a. Key) { if(null == a. Key){ throw new Null. Pointer.

public boolean remove(String a. Key) { if(null == a. Key){ throw new Null. Pointer. Exception(); } int hash. Code = this. calculate. Hash. Code(a. Key); if(null == this. table[hash. Code]){ return false; } if(this. removed. Data != this. table[hash. Code]){ if(this. table[hash. Code]. get. Name(). equals(a. Key)){ this. table[hash. Code] = removed. Data; return true; } } int limit = this. table. length -1; for(int count = 0; count < limit; count++){ hash. Code = this. calculate. Hash. Code. Again(a. Key, hash. Code); if(null == this. table[hash. Code]){ return false; } if(this. removed. Data != this. table[hash. Code]){ if(this. table[hash. Code]. get. Name(). equals(a. Key)){ this. table[hash. Code] = removed. Data; return true; } } } return false; }