Definition: A hash table is a data structure. It refers to a table in which the head of a linked list is stored by an array. We map the data according to the hash function, so that the data is distributed to different linked lists of this table. The purpose of this is because the search efficiency of a single linked list is very low, you can only search one by one. After allocating to different linked lists through the hash function, you only need to search from the smaller linked list. Can greatly improve the search efficiency. Therefore, judging whether the hash function is good or bad mainly depends on whether the data can be evenly distributed to different linked lists, and as far as possible, a large amount of data cannot be aggregated into the same linked list.
schematic diagram
Hash table (also called hash table) is a data structure that is accessed directly according to the key value. In other words, it accesses records by mapping key code values to a location in the table to speed up the search. This mapping function is called a hash function, and the array that stores the records is called a hash table. This is another way of saying it is actually the same thing.
Specific questions: A
Google computer on-board question:
There is a company that asks to add the employee ’s information (id, gender, age, name, address…) when a new employee comes to report. When entering the employee ’s id, it asks to find the employee? .
Claim:
- Without using a database, the faster the better => hash table (hash)
- When adding, ensure to insert from low to high according to id [Thinking: If the id is not inserted from low to high, but the linked lists are still from low to high, how to solve it?]
- Use a linked list to implement a hash table without the header
Code:
#哈希表保存雇员信息
'''
1、一个数组,里面存储着链表,链表存储雇员信息
2、也就是说数组里面实际存储着链表的头节点
3、定义雇员结构{id,name,address},即链表的结点
4、链表[]
5、HashTab[]
6、散列函数
'''
class Emp(object):
def __init__(self,ID,name):
self.ID = ID
self.name=name
self.next=None
def show(self):
print('雇员信息:'+str(self.ID)+' '+self.name)
class LinkedList(object):
def __init__(self):
self.head=None
#添加雇员
def add(self,emp):
if self.head==None:
self.head=emp
return
curEmp = self.head
while True:
if curEmp.next==None:
break
curEmp=curEmp.next
curEmp.next=emp
#寻找雇员,找到后返回emp,否则返回None
def finEmp(self,ID):
if self.head == None:
return None
curEmp = self.head
while(True):
if curEmp.ID == ID:
break
if curEmp.next == None:
curEmp=None
break
curEmp = curEmp.next
return curEmp
#显示雇员
def showlist(self,no):
if self.head==None:
print("链表{}为空!".format(no))
return
print('链表{}的信息为:'.format(no))
curEmp = self.head
while True:
print('=> id={} name={} '.format(curEmp.ID,curEmp.name))
if curEmp.next==None:
break
curEmp = curEmp.next#后移遍历
class HashTab(object):
def __init__(self,size):
self.arr = [LinkedList() for i in range(size)]
self.size = size
def hashFun(self,ID):
return ID % self.size
def add(self,emp):
#根据雇员的id进行哈希,进而分配到不同的链表
arrNo = self.hashFun(emp.ID)
#将emp添加到对应的链表中
self.arr[arrNo].add(emp)
def lookAll(self):
for i in range(self.size):
self.arr[i].showlist(i+1)
#根据输入遍历hashTab
def findEmp(self,ID):
empNo = self.hashFun(ID)
emp = self.arr[empNo].finEmp(ID)
if emp!=None:
print('在第{}条链表中找到该雇员 name = {}'.format(empNo+1,emp.name))
return emp
else:
print('在哈希表中没有找到该成员~')
emp1 = Emp(11,'肖茵')
emp2= Emp(672,'刘伟')
emp3= Emp(23,'王五')
emp4= Emp(42,'李四')
emp5= Emp(45,'张三')
haxi = HashTab(3)
while(True):
print()
key = input('add:添加雇员\nfind:查找雇员\nlist:显示雇员\nexit:退出系统\n')
if key=='add':
ID = int(input('ID = '))
name = input('name = ')
emp = Emp(ID,name)
haxi.add(emp)
elif key=='list':
haxi.lookAll()
elif key=='find':
ID = int(input("ID = "))
haxi.findEmp(ID)
elif key=='exit':
print('退出成功')
break