Created
April 22, 2013 17:12
-
-
Save emreyh/5436814 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashTableCS: | |
def __init__(self,size): | |
self.slots=[None]*size | |
def store(self,key,value): | |
hashvalue=self.hashfunction(key,len(self.slots)) | |
if self.slots[hashvalue]==None: | |
self.slots[hashvalue]=(key,value) | |
else: | |
nextslot=self.rehash(hashvalue,len(self.slots)) | |
while self.slots[nextslot] !=None: | |
nextslot=self.rehash(hashvalue,len(self.slots)) | |
self.slots[nextslot]=(key,value) | |
def hashfunction(self,key,size): | |
toplam=0 | |
for i in range(len(key)): | |
toplam+=ord(key[i])*(i+1) | |
return toplam%size | |
def rehash(self,oldhash,size): | |
return (oldhash+1)%size | |
def search(self,key): | |
startslot=self.hashfunction(key,len(self.slots)) | |
data=None | |
stop = False | |
found = False | |
position=startslot | |
while self.slots[position] != None and \ | |
not found and not stop: | |
if self.slots[position][0]==key: | |
found=True | |
data=self.slots[position] | |
else: | |
position=self.rehash(position,len(self.slots)) | |
if position == startslot: | |
stop=True | |
return data | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment