Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2012-03-18T15:39:42+00:00 → 2012-03-30T11:17:14+00:00
<language>Java</language> | |
| − | <popup> |
| − | <code id="4f65fe9cec0c7">public Object HashTable { |
| − | private DataItem[] hashArray; |
| − | |
| − | private int arraySize; |
| − | |
| − | // private DataItem bufItem; // for deleted items |
| − | |
| − | public HashTable(int size) { |
| − | arraySize = size; |
| − | hashArray = new DataItem[arraySize]; |
| − | // bufItem = new DataItem(-1); // deleted item key is -1 |
| + | <popup name="Klassen-Definitionen"> |
| + | <code id="4f6ca2b733235"> |
| + | public Object Elem(int d) { |
| + | int data = d; |
| + | int getKey() { |
| + | return data; |
} | |
| + | return this; |
| + | } |
| + | public Object HashTable(int size) { |
| + | int arraySize = size; |
| + | int steps; |
| − | public void displayTable() { |
| − | System.out.print("Hash Table: "); |
| + | Object[] hashArray = new Object[arraySize]; |
| + | for(i = 0; i < arraySize; i++) { |
| + | hashArray[i] = null; |
| + | } |
| + | |
| + | void displayTable() { |
for (int i = 0; i < arraySize; i++) { | |
if (hashArray[i] != null) | |
System.out.print(hashArray[i].getKey() + " "); | |
else | |
System.out.print("# "); | |
} | |
System.out.println(""); | |
} | |
| − | public int hashFunction(int key) { |
| + | int hashFunction(int key) { |
return key % arraySize; | |
} | |
| − | public void insert(DataItem item) { |
| − | int key = item.getKey(); |
| − | int hashVal = hashFunction(key); // hash the key |
| − | // until empty cell or -1, |
| − | while (hashArray[hashVal] != null/* && hashArray[hashVal].getKey() != -1 */) { |
| − | ++hashVal; // go to next cell |
| − | hashVal %= arraySize; // wraparound if necessary |
| + | void insert(elem) { |
| + | steps = 0; |
| + | int key = elem.getKey(); |
| + | int hashVal = hashFunction(key); |
| + | int temp = hashVal; |
| + | |
| + | while (hashArray[hashVal] != null) { |
| + | steps++; |
| + | hashVal = temp + steps*steps; |
| + | hashVal %= arraySize; |
} | |
| − | hashArray[hashVal] = item; // insert item |
| + | hashArray[hashVal] = elem; |
| + | System.out.println(steps + " Sondierungen für " + key); |
} | |
| − | public DataItem delete(int key) { |
| + | Object remove(int key) { |
| + | steps = 0; |
| + | tempElem = null; |
int hashVal = hashFunction(key); | |
| + | int temp = hashVal; |
| − | while (hashArray[hashVal] != null) // until empty cell, |
| − | { |
| + | while (hashArray[hashVal] != null) { |
if (hashArray[hashVal].getKey() == key) { | |
| − | // DataItem temp = hashArray[hashVal]; // save item |
| − | // hashArray[hashVal] = bufItem; // delete item |
| − | return hashArray[hashVal] = null; |
| − | // return temp; |
| + | System.out.println("Sondierungen: " + steps); |
| + | tempElem = hashArray[hashVal]; |
| + | hashArray[hashVal] = null; |
| + | return tempElem; |
} | |
| − | ++hashVal; // go to next cell |
| − | hashVal %= arraySize; // wraparound if necessary |
| + | steps++; |
| + | hashVal = temp + steps*steps; |
| + | hashVal %= arraySize; |
} | |
| − | return null; // can't find item |
| + | System.out.println("Sondierungen: " + steps); |
| + | return null; |
} | |
| − | public DataItem find(int key) // find item with key |
| − | { |
| − | int hashVal = hashFunction(key); // hash the key |
| + | Object find(int key) { |
| + | steps = 0; |
| + | int hashVal = hashFunction(key); |
| + | int temp = hashVal; |
| − | while (hashArray[hashVal] != null) // until empty cell, |
| − | { |
| − | if (hashArray[hashVal].getKey() == key) |
| − | return hashArray[hashVal]; // found, return item |
| − | ++hashVal; // go to next cell |
| − | hashVal %= arraySize; // wraparound if necessary |
| + | while (hashArray[hashVal] != null) { |
| + | if (hashArray[hashVal].getKey() == key) { |
| + | System.out.println("Sondierungen: " + steps); |
| + | return hashArray[hashVal]; |
| + | } |
| + | steps++; |
| + | hashVal = temp + steps*steps; |
| + | hashVal %= arraySize; |
} | |
| − | return null; // can't find item |
| + | System.out.println("Sondierungen: " + steps); |
| + | return null; |
} | |
| + | return this; |
} | |
| − | class Object { |
| − | private int data; |
| + | void fillHashTable(Object hashTable, int numberOfKeys) { |
| − | public DataItem(int d) { |
| − | data = d; |
| − | } |
| + | for (int i = 0; i < numberOfKeys; i++) { |
| + | key = (int) (Math.random() * 10 * size); |
| + | elem = Elem(key); |
| + | hashTable.insert(elem); |
| + | } |
| + | } |
| + | </code></popup> |
| + | <run id="4f6ca2b73358e"> |
| + | size = 17; |
| + | n = 10; |
| − | public int getKey() { |
| − | return data; |
| − | } |
| − | }</code> |
| − | </popup> |
| + | hashTable = HashTable(size); |
| + | fillHashTable(hashTable, n); |
| + | hashTable.displayTable(); |
| + | </run> |
| − | <run id="4f6600fd36bb6">DataItem aDataItem; |
| − | int aKey, size; |
| + | <run id="4f74a205a5d00"> |
| + | size = 17; |
| + | n = 10; |
| − | size = 15; |
| − | HashTable theHashTable = new HashTable(size); |
| + | hashTable = HashTable(size); |
| + | fillHashTable(hashTable, n); |
| − | aKey = 1; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| − | |
| − | aKey = 3; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| − | |
| − | aKey = 4; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| − | |
| − | aKey = 8; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| − | |
| − | aKey = 9; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| − | |
| − | aKey = 12; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| − | |
| − | aKey = 13; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| + | hashTable.displayTable(); |
| − | theHashTable.displayTable(); |
| − | |
| − | aKey = 23; |
| − | aDataItem = new DataItem(aKey); |
| − | theHashTable.insert(aDataItem); |
| − | theHashTable.displayTable(); |
| + | key = (int) (Math.random() * 10 * size); |
| + | dataItem = hashTable.find(key); |
| − | theHashTable.delete(9); |
| − | theHashTable.displayTable(); |
| − | |
| − | aDataItem = theHashTable.find(aKey); |
| − | if (aDataItem != null) { |
| − | System.out.println(aKey + " wurde gefunden!"); |
| − | } else { |
| − | System.out.println(aKey + " wurde nicht gefunden!"); |
| − | }</run> |
| + | if (dataItem != null) { |
| + | System.out.println(key + " wurde gefunden!"); |
| + | } else { |
| + | System.out.println(key + " konnte nicht gefunden werden!"); |
| + | } |
| + | </run> |
| + | |
| + | <run id="4f74a7ce5891c"> |
| + | size = 17; |
| + | n = 10; |
| + | |
| + | hashTable = HashTable(size); |
| + | fillHashTable(hashTable, n); |
| + | |
| + | hashTable.displayTable(); |
| + | |
| + | key = (int) (Math.random() * 10 * size); |
| + | dataItem = hashTable.remove(key); |
| + | |
| + | if (dataItem != null) { |
| + | System.out.println(key + " wurde gelöscht!"); |
| + | } else { |
| + | System.out.println(key + " konnte nicht gelöscht werden!"); |
| + | } |
| + | |
| + | hashTable.displayTable(); |
| + | </run> |