# 선형조사
class LinearProbing:
def __init__(self, size): # 생성자
self.M = size
self.a = [None for x in range(size+1)] # 해시테이블
self.d = [None for x in range(size+1)] # key관련 데이터 저장
def hash(self, key):
return key % self.M # 나눗셈 함수
def put(self, key, data): # 삽입 연산
initial_position = self.hash(key) # 초기 위치
i = initial_position
j = 0
while True:
if self.a[i] == None or self.a[i] == '$': # 삽입 위치 발견
self.a[i] = key # key를 해시테이블에 저장
self.d[i] = data # key관련 데이터 저장
return
if self.a[i] == key: # 이미 key 존재하면
self.d[i] = data # 데이터만 갱신
return
j += 1
i = (initial_position + j) % self.M # i의 다음 위치
if i == initial_position: # i가 초기위치와 같으면 루프 종료
break
def get(self, key): # 탐색 연산
initial_position = self.hash(key)
i = initial_position
j = 1
while self.a[i] != None: # a[i]가 empty가 아니면
if self.a[i] == key:
return self.d[i] # 탐색 성공
i = (initial_position + j) % self.M # i의 다음 위치
j += 1
if i == initial_position: # i가 초기위치와 같으면 루프 종료
return None # 탐색 실패
return None # 탐색 실패
def delete(self, key): # 삭제 연산
initial_position = self.hash(key)
i = initial_position
j = 1
while self.a[i] != None: # a[i]가 empty가 아니면
if self.a[i] == key:
self.a[i] = '$'
self.d[i] = None
i = (initial_position + j) % self.M # i의 다음 위치
j += 1
if i == initial_position: # i가 초기위치와 같으면 루프 종료
return None # 삭제 실패
return None # 삭제 실패
def print_table(self):
for i in range(self.M):
print('{:4}'.format(str(i)), ' ', end='')
print()
for i in range(self.M):
print('{:4}'.format(str(self.a[i])), ' ', end='')
print()
#from linear_prob import LinearProbing
if __name__ == '__main__':
t = LinearProbing(13)
t.put(25, 'grape')
t.put(37, 'apple')
t.put(18, 'banana')
t.put(55, 'cherry')
t.put(22, 'mango')
t.put(35, 'lime')
t.put(50, 'orange')
t.put(63, 'watermelon')
print('탐색 결과:')
print('50의 data = ', t.get(50))
print('63의 data = ', t.get(63))
print('해시테이블:')
t.print_table()
t.delete(50)
t.print_table()
print('63의 data = ', t.get(63))
t.put(9, 'berry')
t.print_table()
class QuadProbing:
def __init__(self, size): # 생성자
self.M = size
self.a = [None for x in range(size+1)] # 해시테이블
self.d = [None for x in range(size+1)] # key관련 데이터 저장
self.N = 0 # 항목 수
def hash(self, key): # 나눗셈 함수
return key % self.M
def put(self, key, data): # 삽입 연산
initial_position = self.hash(key) # 초기 위치
i = initial_position
j = 0
while True:
if self.a[i] == None: # 삽입 위치 발견
self.a[i] = key # key를 해시테이블에 저장
self.d[i] = data # key관련 데이터 저장
self.N += 1
return
if self.a[i] == key: # 이미 key 존재하면
self.d[i] = data # 데이터만 갱신
return
j += 1
i = (initial_position + j*j) % self.M # i의 다음 위치
if self.N > self.M: # 테이블이 full이면
break
def get(self, key): # 탐색 연산
initial_position = self.hash(key)
i = initial_position
j = 1
while self.a[i] != None: # a[i]가 비어있지 않으면
if self.a[i] == key:
return self.d[i] # 탐색 성공
i = (initial_position + j*j) % self.M # i의 다음 위치
j += 1
return None # 탐색 실패
def print_table(self): # 해시테이블 출력
for i in range(self.M):
print('{:4}'.format(str(i)), ' ', end='')
print()
for i in range(self.M):
print('{:4}'.format(str(self.a[i])), ' ', end='')
print()
#from quad_prob import QuadProbing
if __name__ == '__main__':
t = QuadProbing(13)
t.put(25, 'grape')
t.put(37, 'apple')
t.put(18, 'banana')
t.put(55, 'cherry')
t.put(22, 'mango')
t.put(35, 'lime')
t.put(50, 'orange')
t.put(63, 'watermelon')
print('탐색 결과:')
print('50의 data = ', t.get(50))
print('63의 data = ', t.get(63))
print('해시테이블:')
t.print_table()
import random
class RandProbing:
def __init__(self, size):
self.M = size
self.a = [None for x in range(size+1)] # 해시테이블
self.d = [None for x in range(size+1)] # key관련 데이터 저장
self.N = 0 # 항목 수
def hash(self, key): # 나눗셈 함수
return key % self.M
def put(self, key, data): # 삽입 연산
initial_position = self.hash(key) # 초기 위치
i = initial_position
random.seed(1000)
while True:
if self.a[i] == None: # 삽입 위치 발견
self.a[i] = key # key를 해시테이블에 저장
self.d[i] = data # key관련 데이터 저장
self.N += 1
return
if self.a[i] == key: # 이미 key 존재하면
self.d[i] = data # 데이터만 갱신
return
j = random.randint(1, 99)
i = (initial_position + j) % self.M # i의 다음 위치
if self.N > self.M: # 테이블이 full이면
break
def get(self, key): # 탐색 연산
initial_position = self.hash(key)
i = initial_position
random.seed(1000)
while self.a[i] != None: # a[i]가 비어있지 않으면
if self.a[i] == key:
return self.d[i] # 탐색 성공
i = (initial_position + random.randint(1, 99)) % self.M # i의 다음 위치
return None # 탐색 실패
def print_table(self):
for i in range(self.M):
print('{:4}'.format(str(i)), ' ', end='')
print()
for i in range(self.M):
print('{:4}'.format(str(self.a[i])), ' ', end='')
print()
if __name__ == '__main__':
t = RandProbing(13)
t.put(25, 'grape')
t.put(37, 'apple')
t.put(18, 'banana')
t.put(55, 'cherry')
t.put(22, 'mango')
t.put(35, 'lime')
t.put(50, 'orange')
t.put(63, 'watermelon')
print('탐색 결과:')
print('50의 data = ', t.get(50))
print('63의 data = ', t.get(63))
print('해시테이블:')
t.print_table()
class Chaining:
class Node:
def __init__(self, key, data, link): # Node 생성자
self.key = key
self.data = data
self.next = link
def __init__(self, size):
self.M = size
self.a = [None for x in range(size+1)] # 해시테이블
def hash(self, key): # 나눗셈 함수
return key % self.M
def put(self, key, data): # 삽입 연산
i = self.hash(key)
p = self.a[i]
while p != None:
if key == p.key: # 이미 key 존재하면
p.data = data # 데이터만 갱신
return
p = p.next
self.a[i] = self.Node(key, data, self.a[i])
def get(self, key): # 탐색 연산
i = self.hash(key)
p = self.a[i]
while p != None:
if key == p.key:
return p.data # 탐색 성공
p = p.next
return None # 탐색 실패
def print_table(self): # 테이블 출력
for i in range(self.M):
print('%2d' % (i), end='')
p = self.a[i]
while p != None:
print('-->[', p.key,',', p.data, ']', end='')
p = p.next;
print()
if __name__ == '__main__':
t = Chaining(13)
t.put(25, 'grape')
t.put(37, 'apple')
t.put(18, 'banana')
t.put(55, 'cherry')
t.put(22, 'mango')
t.put(35, 'lime')
t.put(50, 'orange')
t.put(63, 'watermelon')
print('탐색 결과:')
print('50의 data = ', t.get(50))
print('63의 data = ', t.get(63))
print('해시테이블:')
t.print_table()
class DoubleHashing:
def __init__(self, size):
self.M = size
self.a = [None for x in range(size+1)] # 해시테이블
self.d = [None for x in range(size+1)] # key관련 데이터 저장
self.N = 0 # 항목 수
def hash(self, key): # 나눗셈 함수
return key % self.M
def put(self, key, data): # 삽입 연산
initial_position = self.hash(key) # 초기 위치
i = initial_position
d = 7 - (key % 7) # 2번? 해시 함수
j = 0
while True:
if self.a[i] == None: # 삽입 위치 발견
self.a[i] = key # key를 해시테이블에 저장
self.d[i] = data # key관련 데이터를 동일한 인덱스하에 저장
self.N += 1
return
if self.a[i] == key: # 이미 key 존재하면
self.d[i] = data # 데이터만 갱신
return
j += 1
i = (initial_position + j*d) % self.M # i의 다음 위치
if self.N > self.M: # 테이블이 full이면
break
def get(self, key): # 탐색 연산
initial_position = self.hash(key)
i = initial_position
d = 7 - (key % 7) # 2번? 해시 함수
j = 0
while self.a[i] != None: # a[i]가 비어있지 않으면
if self.a[i] == key:
return self.d[i] # 탐색 성공
j += 1
i = (initial_position + j*d) % self.M # i의 다음 위치
return None # 탐색 실패
def print_table(self):
for i in range(self.M):
print('{:4}'.format(str(i)), ' ', end='')
print()
for i in range(self.M):
print('{:4}'.format(str(self.a[i])), ' ', end='')
print()
if __name__ == '__main__':
t = DoubleHashing(13)
t.put(25, 'grape')
t.put(37, 'apple')
t.put(18, 'banana')
t.put(55, 'cherry')
t.put(22, 'mango')
t.put(35, 'lime')
t.put(50, 'orange')
t.put(63, 'watermelon')
print('탐색 결과:')
print('50의 data = ', t.get(50))
print('63의 data = ', t.get(63))
print('해시테이블:')
t.print_table()