Etiketler

,

package Lab9;

public class HashTable {

HashEntry [] table;

HashTable(int size){
table = new HashEntry[size];

 //hashtable size ini giriyoruz burda 
}

//SELECTION DIGIT İLE
void put(int key, String value){

  // hastable nedir ,hash
//key ve value dan olusan şeylerin dizisidir
//key interger ya da integera cevrilcek bişey olcak burda
//selectıon digit ile   
// key : 1238669629 23 69
String temp = String.valueOf(key);

//keydan bir string ürettim
//ya modla alcaktık ya da stringe cevirmek daha kolay yaptık
temp = temp.substring(1,3) + temp.substring(5, 7);
int hash = Integer.parseInt(temp);
hash = hash % table.length;
table[hash] = new HashEntry(key, value);
}

String get(int key){

//o key a sahip string i döndürüyoruz burda
// key : 1238669629 23 69
String aranan = “tabloda bulunamadı”;
String temp = String.valueOf(key);
temp = temp.substring(1,3) + temp.substring(5, 7);

//iki stringi yanyana yazcak
int hash = Integer.parseInt(temp);

// 23 ve 69
hash = hash % table.length;

 //table ın neresine bu key ve stringi atcagımızı bu algoritmayla hesaplıyoruz
if(table[hash] != null)
aranan = table[hash].value;
return aranan;
}

void printTable(){

//hash table ımızda ne var bi görelim diye metod yazdık 
for (int i = 0; i < table.length; i++) {
System.out.print(“[” + i + “] “);
if( table[i] != null)//bos deilse yador
System.out.println( table[i].key + ” : ” + table[i].value);
else
System.out.println(“”);
}
}

public static void main(String[] args) {
HashTable table = new HashTable(7);
table.put(123456789, “Ahmet”);
table.put(456655656, “Mehmet”);
table.put(565767887, “Hasan”);
table.printTable();//o keye sahip kişiyi verdi
System.out.println(“aranan: ” + table.get(123353455));
}

//HASH FONKSİYONLARI

//1- SELECTING DIGITS – RAKAM SEÇME
//Anahtarın belli indekslerdeki (3. 5. gibi) rakamları seçilir ve modu alınır.

//2- FOLDING – KATLAMA
//Anahtar üç parçaya bölünüp, parçalar toplanıp modu alınır.
//Yada üç parçadan ortadakini ters çevirip, parçaları toplayıp, mod alınır.

//3- DIVISION – BÖLME
//% mod bölmesi yapılır.

//4- MİD-SQUARE – ORTA KARE
//Anahtarın karesi alınıp, ortadaki rakamlarını alınır.

//5- EXTRACTION
//Anahtarın belli bir kısımları random olarak seçilir //DOLUYSA TEKRAR ÇALIŞTIRILABİLİR

//6- RADIX TRANSFORMATION
//Anahtar başka bir tabana dönüştürülür, tablo boyutuna göre mod alınır.

class HashEntry{

//numara ve nuamra sahibi gibi bu hash entry ,
//hash tableın her bir satırına denilen addır
//key ve value var ,,int da olabilir stringde ..
//hash tabel sözlük,rehber gibi bieşy ,telefon rehberi ,şu numaara kime ait gibi hani şeyler tutuyo
//hash tableın
int key;
String value;

HashEntry(int key, String value){
this.key = key;
this.value = value;
}

}
}

—————————————————————————
package Lab9;
public class ListHashTable {

LinkedList [] table;

ListHashTable(int size){
table = new LinkedList[size];
for (int i = 0; i < size; i++) {
table[i] = new LinkedList();
}
}

void put(int key, String value){

String temp = String.valueOf(key);
temp = temp.substring(1,3) + temp.substring(5, 7);
int hash = Integer.parseInt(temp);
hash = hash % table.length;
table[hash].addFirst(key, value);
}

String get(int key){

String temp = String.valueOf(key);
temp = temp.substring(1,3) + temp.substring(5, 7);
int hash = Integer.parseInt(temp);
hash = hash % table.length;

//hash hangiindexte oldugunu gösteriyo
// o indexte bir linkedlist var key de var value da var içerde
// biz keyy vercez stringi döndürcez

String bulunan = “tabloda bulunamadı”;
Node current = table[hash].head;
while( current != null){
if( current.key == key){
bulunan = current.value;
break;
}
current = current.next;
}
return bulunan;
}

void printTable(){

for (int i = 0; i < table.length; i++) { System.out.print(“[” + i + “] “); table[i].printList(); System.out.println(“”); } } public static void main(String[] args) { ListHashTable table = new ListHashTable(10); table.put(1123446, “Ali”); table.put(12212112, “Veli”); table.put(12123334, “Hasan”); table.put(23444545, “Mehmet”); table.put(33443432, “Furkan”); table.printTable(); System.out.println(“aranan: ” + table.get(1212343334)); } class LinkedList{ Node head; void addFirst(int key, String value){ Node n = new Node(key, value); n.next = head; head = n; } Node removeFirst(){ Node temp = head; head = head.next; return temp; } void printList(){ Node temp = head; while( temp != null){ System.out.print( “[” + temp.key + ” : ” + temp.value + “] -> “);
temp = temp.next;
}
}
}

class Node{
int key;
String value;
Node next;

Node(int key, String value){
this.key = key;
this.value = value;
}
}
}