169 lines
4.0 KiB
Python
169 lines
4.0 KiB
Python
def ll_make_node(name, phone):
|
|
return {'name': name, 'phone': phone, 'next': None}
|
|
|
|
|
|
def ll_insert(head, name, phone):
|
|
if head is None:
|
|
return ll_make_node(name, phone)
|
|
|
|
current = head
|
|
while current is not None:
|
|
if current['name'] == name:
|
|
current['phone'] = phone
|
|
return head
|
|
current = current['next']
|
|
|
|
new_node = ll_make_node(name, phone)
|
|
new_node['next'] = head
|
|
return new_node
|
|
|
|
|
|
def ll_find(head, name):
|
|
current = head
|
|
while current is not None:
|
|
if current['name'] == name:
|
|
return current['phone']
|
|
current = current['next']
|
|
return None
|
|
|
|
|
|
def ll_delete(head, name):
|
|
if head is None:
|
|
return None
|
|
|
|
if head['name'] == name:
|
|
return head['next']
|
|
|
|
current = head
|
|
while current['next'] is not None:
|
|
if current['next']['name'] == name:
|
|
current['next'] = current['next']['next']
|
|
return head
|
|
current = current['next']
|
|
|
|
return head
|
|
|
|
|
|
def ll_list_all(head):
|
|
result = []
|
|
current = head
|
|
while current is not None:
|
|
result.append((current['name'], current['phone']))
|
|
current = current['next']
|
|
return sorted(result, key=lambda x: x[0])
|
|
|
|
def ht_make(size=256):
|
|
return [None] * size
|
|
|
|
|
|
def ht_hash(buckets, name):
|
|
return hash(name) % len(buckets)
|
|
|
|
|
|
def ht_insert(buckets, name, phone):
|
|
idx = ht_hash(buckets, name)
|
|
buckets[idx] = ll_insert(buckets[idx], name, phone)
|
|
|
|
|
|
def ht_find(buckets, name):
|
|
idx = ht_hash(buckets, name)
|
|
return ll_find(buckets[idx], name)
|
|
|
|
|
|
def ht_delete(buckets, name):
|
|
idx = ht_hash(buckets, name)
|
|
buckets[idx] = ll_delete(buckets[idx], name)
|
|
|
|
|
|
def ht_list_all(buckets):
|
|
result = []
|
|
for bucket_head in buckets:
|
|
current = bucket_head
|
|
while current is not None:
|
|
result.append((current['name'], current['phone']))
|
|
current = current['next']
|
|
return sorted(result, key=lambda x: x[0])
|
|
|
|
def bst_make_node(name, phone):
|
|
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
|
|
|
|
|
def bst_insert(root, name, phone):
|
|
new_node = bst_make_node(name, phone)
|
|
|
|
if root is None:
|
|
return new_node
|
|
|
|
current = root
|
|
while True:
|
|
if name == current['name']:
|
|
current['phone'] = phone
|
|
return root
|
|
elif name < current['name']:
|
|
if current['left'] is None:
|
|
current['left'] = new_node
|
|
return root
|
|
current = current['left']
|
|
else:
|
|
if current['right'] is None:
|
|
current['right'] = new_node
|
|
return root
|
|
current = current['right']
|
|
|
|
|
|
def bst_find(root, name):
|
|
current = root
|
|
while current is not None:
|
|
if name == current['name']:
|
|
return current['phone']
|
|
elif name < current['name']:
|
|
current = current['left']
|
|
else:
|
|
current = current['right']
|
|
return None
|
|
|
|
|
|
def _bst_min_node(node):
|
|
current = node
|
|
while current['left'] is not None:
|
|
current = current['left']
|
|
return current
|
|
|
|
|
|
def bst_delete(root, name):
|
|
if root is None:
|
|
return None
|
|
|
|
if name < root['name']:
|
|
root['left'] = bst_delete(root['left'], name)
|
|
elif name > root['name']:
|
|
root['right'] = bst_delete(root['right'], name)
|
|
else:
|
|
if root['left'] is None:
|
|
return root['right']
|
|
elif root['right'] is None:
|
|
return root['left']
|
|
else:
|
|
successor = _bst_min_node(root['right'])
|
|
root['name'] = successor['name']
|
|
root['phone'] = successor['phone']
|
|
root['right'] = bst_delete(root['right'], successor['name'])
|
|
|
|
return root
|
|
|
|
|
|
def bst_list_all(root):
|
|
result = []
|
|
stack = []
|
|
current = root
|
|
|
|
while current is not None or stack:
|
|
while current is not None:
|
|
stack.append(current)
|
|
current = current['left']
|
|
current = stack.pop()
|
|
result.append((current['name'], current['phone']))
|
|
current = current['right']
|
|
|
|
return result
|