def linked_list_add(head, name, phone): curr = head while curr is not None: if curr['name'] == name: curr['phone'] = phone return head curr = curr['next'] new_node = {'name': name, 'phone': phone, 'next': None} if head is None: return new_node curr = head while curr['next'] is not None: curr = curr['next'] curr['next'] = new_node return head def linked_list_find(head, name): curr = head while curr is not None: if curr['name'] == name: return curr['phone'] curr = curr['next'] return None def linked_list_remove(head, name): if head is None: return None if head['name'] == name: return head['next'] prev = head curr = head['next'] while curr is not None: if curr['name'] == name: prev['next'] = curr['next'] return head prev = curr curr = curr['next'] return head def linked_list_collect_all(head): records = [] curr = head while curr is not None: records.append((curr['name'], curr['phone'])) curr = curr['next'] records.sort(key=lambda pair: pair[0]) return records #HASH def _hash_bucket_index(key, table_size): return hash(key) % table_size def hash_table_create(bucket_count=10): return [None] * bucket_count def hash_table_put(table, name, phone): idx = _hash_bucket_index(name, len(table)) table[idx] = linked_list_add(table[idx], name, phone) return table def hash_table_get(table, name): idx = _hash_bucket_index(name, len(table)) return linked_list_find(table[idx], name) def hash_table_remove(table, name): idx = _hash_bucket_index(name, len(table)) table[idx] = linked_list_remove(table[idx], name) return table def hash_table_collect_all(table): all_records = [] for head in table: curr = head while curr is not None: all_records.append((curr['name'], curr['phone'])) curr = curr['next'] all_records.sort(key=lambda pair: pair[0]) return all_records #BST def _bst_new_node(name, phone): return {'name': name, 'phone': phone, 'left': None, 'right': None} def bst_add(root, name, phone): """Insert or update. Returns (possibly new) root.""" if root is None: return _bst_new_node(name, phone) if name == root['name']: root['phone'] = phone elif name < root['name']: root['left'] = bst_add(root['left'], name, phone) else: root['right'] = bst_add(root['right'], name, phone) return root def bst_find(root, name): if root is None: return None if name == root['name']: return root['phone'] elif name < root['name']: return bst_find(root['left'], name) else: return bst_find(root['right'], name) def _bst_find_minimum(node): while node['left'] is not None: node = node['left'] return node def bst_remove(root, name): if root is None: return None if name < root['name']: root['left'] = bst_remove(root['left'], name) elif name > root['name']: root['right'] = bst_remove(root['right'], name) else: if root['left'] is None: return root['right'] if root['right'] is None: return root['left'] successor = _bst_find_minimum(root['right']) root['name'] = successor['name'] root['phone'] = successor['phone'] root['right'] = bst_remove(root['right'], successor['name']) return root def bst_collect_inorder(root): result = [] def inorder(node): if node is None: return inorder(node['left']) result.append((node['name'], node['phone'])) inorder(node['right']) inorder(root) return result if __name__ == '__main__': tree = None tree = bst_add(tree, 'Zoe', '111') tree = bst_add(tree, 'Alice', '222') tree = bst_add(tree, 'Bob', '333') tree = bst_add(tree, 'Alice', '444') print(bst_find(tree, 'Alice')) # 444 tree = bst_remove(tree, 'Bob') print(bst_collect_inorder(tree)) # [('Alice','444'), ('Zoe','111')]