From 30d416cfac2f5bf4211f1b713f238e72d8f419ae Mon Sep 17 00:00:00 2001 From: shalovsa Date: Mon, 11 May 2026 16:54:44 +0300 Subject: [PATCH] =?UTF-8?q?[1]=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83?= =?UTF-8?q?=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shalovsa/lab1/docs/data/phone_book.py | 168 ++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 shalovsa/lab1/docs/data/phone_book.py diff --git a/shalovsa/lab1/docs/data/phone_book.py b/shalovsa/lab1/docs/data/phone_book.py new file mode 100644 index 0000000..297f2c5 --- /dev/null +++ b/shalovsa/lab1/docs/data/phone_book.py @@ -0,0 +1,168 @@ +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