forked from UNN/2026-rff_mp
[1] 1-st-exercise #2
110
VolkovVA/cod.py
110
VolkovVA/cod.py
|
|
@ -101,4 +101,112 @@ def ht_list_all(buckets):
|
||||||
while current is not None:
|
while current is not None:
|
||||||
res += [(current['name'], current['phone'])]
|
res += [(current['name'], current['phone'])]
|
||||||
current = current['next']
|
current = current['next']
|
||||||
return sort_records(res)
|
return sort_records(res)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def bst_insert(root, name, phone):
|
||||||
|
new_node = {'name': name, 'phone': phone, 'left': None, 'right': None}
|
||||||
|
|
||||||
|
if root is None:
|
||||||
|
return new_node
|
||||||
|
|
||||||
|
current = root
|
||||||
|
while True:
|
||||||
|
if name == current['name']:
|
||||||
|
current['phone'] = phone
|
||||||
|
break
|
||||||
|
elif name < current['name']:
|
||||||
|
if current['left'] is None:
|
||||||
|
current['left'] = new_node
|
||||||
|
break
|
||||||
|
current = current['left']
|
||||||
|
else:
|
||||||
|
if current['right'] is None:
|
||||||
|
current['right'] = new_node
|
||||||
|
break
|
||||||
|
current = current['right']
|
||||||
|
|
||||||
|
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_delete(root, name):
|
||||||
|
if root is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
p_node = None
|
||||||
|
current = root
|
||||||
|
|
||||||
|
while current is not None and current['name'] != name:
|
||||||
|
p_node = current
|
||||||
|
if name < current['name']:
|
||||||
|
current = current['left']
|
||||||
|
else:
|
||||||
|
current = current['right']
|
||||||
|
|
||||||
|
if current is None:
|
||||||
|
return root
|
||||||
|
|
||||||
|
if current['left'] is None and current['right'] is None:
|
||||||
|
if p_node is None:
|
||||||
|
return None
|
||||||
|
if p_node['left'] is current:
|
||||||
|
p_node['left'] = None
|
||||||
|
else:
|
||||||
|
p_node['right'] = None
|
||||||
|
return root
|
||||||
|
|
||||||
|
if current['left'] is None:
|
||||||
|
c_node = current['right']
|
||||||
|
elif current['right'] is None:
|
||||||
|
c_node = current['left']
|
||||||
|
else:
|
||||||
|
succ_parent = current
|
||||||
|
succ = current['right']
|
||||||
|
while succ['left']:
|
||||||
|
succ_parent = succ
|
||||||
|
succ = succ['left']
|
||||||
|
|
||||||
|
current['name'], current['phone'] = succ['name'], succ['phone']
|
||||||
|
|
||||||
|
if succ_parent['left'] is succ:
|
||||||
|
succ_parent['left'] = succ['right']
|
||||||
|
else:
|
||||||
|
succ_parent['right'] = succ['right']
|
||||||
|
return root
|
||||||
|
|
||||||
|
if p_node is None:
|
||||||
|
return c_node
|
||||||
|
if p_node['left'] is current:
|
||||||
|
p_node['left'] = c_node
|
||||||
|
else:
|
||||||
|
p_node['right'] = c_node
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
|
def bst_list_all(root):
|
||||||
|
record = []
|
||||||
|
|
||||||
|
def helper(node):
|
||||||
|
if node is not None:
|
||||||
|
helper(node['left'])
|
||||||
|
nonlocal record
|
||||||
|
record += [(node['name'], node['phone'])]
|
||||||
|
helper(node['right'])
|
||||||
|
|
||||||
|
helper(root)
|
||||||
|
return record
|
||||||
Loading…
Reference in New Issue
Block a user