forked from UNN/2026-rff_mp
[1] fix bst for sorted records
This commit is contained in:
parent
d694f5ce0b
commit
d4fcd67dde
|
|
@ -76,14 +76,24 @@ def ht_list_all(buckets):
|
||||||
return sort_records(records)
|
return sort_records(records)
|
||||||
|
|
||||||
def bst_insert(root, name, phone):
|
def bst_insert(root, name, phone):
|
||||||
|
new_node = {"name": name, "phone": phone, "left": None, "right": None}
|
||||||
if root is None:
|
if root is None:
|
||||||
return {"name": name, "phone": phone, "left": None, "right": None}
|
return new_node
|
||||||
if name < root["name"]:
|
current = root
|
||||||
root["left"] = bst_insert(root["left"], name, phone)
|
while True:
|
||||||
elif name > root["name"]:
|
if name < current["name"]:
|
||||||
root["right"] = bst_insert(root["right"], name, phone)
|
if current["left"] is None:
|
||||||
else:
|
current["left"] = new_node
|
||||||
root["phone"] = phone
|
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
|
return root
|
||||||
|
|
||||||
def bst_find(root, name):
|
def bst_find(root, name):
|
||||||
|
|
@ -97,38 +107,48 @@ def bst_find(root, name):
|
||||||
current = current["right"]
|
current = current["right"]
|
||||||
return None
|
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):
|
def bst_delete(root, name):
|
||||||
if root is None:
|
parent = None
|
||||||
return None
|
current = root
|
||||||
if name < root["name"]:
|
while current is not None and current["name"] != name:
|
||||||
root["left"] = bst_delete(root["left"], name)
|
parent = current
|
||||||
|
if name < current["name"]:
|
||||||
|
current = current["left"]
|
||||||
|
else:
|
||||||
|
current = current["right"]
|
||||||
|
if current is None:
|
||||||
return root
|
return root
|
||||||
if name > root["name"]:
|
if current["left"] is not None and current["right"] is not None:
|
||||||
root["right"] = bst_delete(root["right"], name)
|
replacement_parent = current
|
||||||
return root
|
replacement = current["right"]
|
||||||
if root["left"] is None:
|
while replacement["left"] is not None:
|
||||||
return root["right"]
|
replacement_parent = replacement
|
||||||
if root["right"] is None:
|
replacement = replacement["left"]
|
||||||
return root["left"]
|
current["name"] = replacement["name"]
|
||||||
replacement = bst_min_node(root["right"])
|
current["phone"] = replacement["phone"]
|
||||||
root["name"] = replacement["name"]
|
parent = replacement_parent
|
||||||
root["phone"] = replacement["phone"]
|
current = replacement
|
||||||
root["right"] = bst_delete(root["right"], replacement["name"])
|
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
|
return root
|
||||||
|
|
||||||
def bst_list_all(root):
|
def bst_list_all(root):
|
||||||
records = []
|
records = []
|
||||||
def walk(node):
|
stack = []
|
||||||
if node is None:
|
current = root
|
||||||
return
|
while current is not None or stack:
|
||||||
walk(node["left"])
|
while current is not None:
|
||||||
records.append((node["name"], node["phone"]))
|
stack.append(current)
|
||||||
walk(node["right"])
|
current = current["left"]
|
||||||
walk(root)
|
current = stack.pop()
|
||||||
|
records.append((current["name"], current["phone"]))
|
||||||
|
current = current["right"]
|
||||||
return records
|
return records
|
||||||
Loading…
Reference in New Issue
Block a user