Двоичное дерево поиска
This commit is contained in:
parent
951190aa68
commit
da179c5dde
|
|
@ -84,3 +84,97 @@ def ht_collect_all(buckets):
|
||||||
current = current['next']
|
current = current['next']
|
||||||
all_records.sort(key=lambda x: x[0])
|
all_records.sort(key=lambda x: x[0])
|
||||||
return all_records
|
return all_records
|
||||||
|
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']:
|
||||||
|
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):
|
||||||
|
current = root
|
||||||
|
while current is not None:
|
||||||
|
if name < current['name']:
|
||||||
|
current = current['left']
|
||||||
|
elif name > current['name']:
|
||||||
|
current = current['right']
|
||||||
|
else:
|
||||||
|
return current['phone']
|
||||||
|
return None
|
||||||
|
|
||||||
|
def bst_find_min(node):
|
||||||
|
while node['left'] is not None:
|
||||||
|
node = node['left']
|
||||||
|
return node
|
||||||
|
|
||||||
|
def bst_delete(root, 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 current['left'] is None and current['right'] is None:
|
||||||
|
if parent is None:
|
||||||
|
return None
|
||||||
|
if parent['left'] is current:
|
||||||
|
parent['left'] = None
|
||||||
|
else:
|
||||||
|
parent['right'] = None
|
||||||
|
return root
|
||||||
|
if current['left'] is None:
|
||||||
|
if parent is None:
|
||||||
|
return current['right']
|
||||||
|
if parent['left'] is current:
|
||||||
|
parent['left'] = current['right']
|
||||||
|
else:
|
||||||
|
parent['right'] = current['right']
|
||||||
|
return root
|
||||||
|
if current['right'] is None:
|
||||||
|
if parent is None:
|
||||||
|
return current['left']
|
||||||
|
if parent['left'] is current:
|
||||||
|
parent['left'] = current['left']
|
||||||
|
else:
|
||||||
|
parent['right'] = current['left']
|
||||||
|
return root
|
||||||
|
succ_parent = current
|
||||||
|
succ = current['right']
|
||||||
|
while succ['left'] is not None:
|
||||||
|
succ_parent = succ
|
||||||
|
succ = succ['left']
|
||||||
|
current['name'] = succ['name']
|
||||||
|
current['phone'] = succ['phone']
|
||||||
|
if succ_parent['left'] is succ:
|
||||||
|
succ_parent['left'] = succ['right']
|
||||||
|
else:
|
||||||
|
succ_parent['right'] = succ['right']
|
||||||
|
return root
|
||||||
|
|
||||||
|
def bst_inorder_collect(root, records=None):
|
||||||
|
if records is None:
|
||||||
|
records = []
|
||||||
|
if root is not None:
|
||||||
|
bst_inorder_collect(root['left'], records)
|
||||||
|
records.append((root['name'], root['phone']))
|
||||||
|
bst_inorder_collect(root['right'], records)
|
||||||
|
return records
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user