From 0f97a32fed32db9f4a18d2496e833cd587337039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=B8=D1=81=D0=BE=D0=B2=20=D0=9C=D0=B0?= =?UTF-8?q?=D1=82=D0=B2=D0=B5=D0=B9?= Date: Fri, 17 Apr 2026 20:05:08 +0300 Subject: [PATCH] =?UTF-8?q?[1]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=B5=D1=80=D0=B5=D0=B2=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BorisovMI/lab_1/docs/data/structures.py | 74 +++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/BorisovMI/lab_1/docs/data/structures.py b/BorisovMI/lab_1/docs/data/structures.py index de369ca..29b6307 100644 --- a/BorisovMI/lab_1/docs/data/structures.py +++ b/BorisovMI/lab_1/docs/data/structures.py @@ -101,4 +101,78 @@ def ht_list_all(buckets): records.append((current['name'], current['phone'])) current = current['next'] records.sort(key=lambda x: x[0]) + return records + +# Функции для дерева поиска + +def bst_insert(root, name, phone): + + if root is None: + return {'name': name, 'phone': phone, 'left': None, 'right': None} + + if name < root['name']: + root['left'] = bst_insert(root['left'], name, phone) + elif name > root['name']: + root['right'] = bst_insert(root['right'], name, phone) + else: + root['phone'] = phone + + return root + + +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_find_min(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'] + + min_node = bst_find_min(root['right']) + root['name'] = min_node['name'] + root['phone'] = min_node['phone'] + root['right'] = bst_delete(root['right'], min_node['name']) + + return root + + +def bst_list_all(root): + + records = [] + + def inorder_traversal(node): + if node is not None: + inorder_traversal(node['left']) + records.append((node['name'], node['phone'])) + inorder_traversal(node['right']) + + inorder_traversal(root) return records \ No newline at end of file