diff --git a/raskatovia/docs/data/task1/spravochnik.py b/raskatovia/docs/data/task1/spravochnik.py index b0c62cb..30106e2 100644 --- a/raskatovia/docs/data/task1/spravochnik.py +++ b/raskatovia/docs/data/task1/spravochnik.py @@ -76,14 +76,24 @@ def ht_list_all(buckets): return sort_records(records) def bst_insert(root, name, phone): + new_node = {"name": name, "phone": phone, "left": None, "right": None} 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 new_node + current = root + while True: + if name < current["name"]: + if current["left"] is None: + current["left"] = new_node + break + current = current["left"] + elif name > current["name"]: + if current["right"] is None: + current["right"] = new_node + break + current = current["right"] + else: + current["phone"] = phone + break return root def bst_find(root, name): @@ -97,38 +107,48 @@ def bst_find(root, name): current = current["right"] return None -def bst_min_node(root): - current = root - 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) + parent = None + current = root + while current is not None and current["name"] != name: + parent = current + if name < current["name"]: + current = current["left"] + else: + current = current["right"] + if current is None: return root - if name > root["name"]: - root["right"] = bst_delete(root["right"], name) - return root - if root["left"] is None: - return root["right"] - if root["right"] is None: - return root["left"] - replacement = bst_min_node(root["right"]) - root["name"] = replacement["name"] - root["phone"] = replacement["phone"] - root["right"] = bst_delete(root["right"], replacement["name"]) + if current["left"] is not None and current["right"] is not None: + replacement_parent = current + replacement = current["right"] + while replacement["left"] is not None: + replacement_parent = replacement + replacement = replacement["left"] + current["name"] = replacement["name"] + current["phone"] = replacement["phone"] + parent = replacement_parent + current = replacement + if current["left"] is not None: + child = current["left"] + else: + child = current["right"] + if parent is None: + return child + if parent["left"] is current: + parent["left"] = child + else: + parent["right"] = child return root def bst_list_all(root): records = [] - def walk(node): - if node is None: - return - walk(node["left"]) - records.append((node["name"], node["phone"])) - walk(node["right"]) - walk(root) + stack = [] + current = root + while current is not None or stack: + while current is not None: + stack.append(current) + current = current["left"] + current = stack.pop() + records.append((current["name"], current["phone"])) + current = current["right"] return records \ No newline at end of file