[1] task 1 #180
106
YanyaevAA/[1].py
106
YanyaevAA/[1].py
|
|
@ -1,6 +1,3 @@
|
|||
def ll_create_node(name, phone):
|
||||
return {'name': name, 'phone': phone, 'next': None}
|
||||
|
||||
def ll_insert(head, name, phone):
|
||||
current = head
|
||||
while current:
|
||||
|
|
@ -8,7 +5,7 @@ def ll_insert(head, name, phone):
|
|||
current['phone'] = phone
|
||||
return head
|
||||
current = current['next']
|
||||
new_node = ll_create_node(name, phone)
|
||||
new_node = {'name': name, 'phone': phone, 'next': None}
|
||||
new_node['next'] = head
|
||||
return new_node
|
||||
|
||||
|
|
@ -31,10 +28,105 @@ def ll_delete(head, name):
|
|||
current = current['next']
|
||||
return head
|
||||
|
||||
def quick_sort(arr):
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
left = []
|
||||
middle = []
|
||||
right = []
|
||||
pivot = arr[len(arr) // 2][0]
|
||||
for x in arr:
|
||||
if x[0]<pivot:
|
||||
left.append(x)
|
||||
elif x[0]==pivot:
|
||||
middle.append(x)
|
||||
else:
|
||||
right.append(x)
|
||||
return quick_sort(left) + middle + quick_sort(right)
|
||||
|
||||
def ll_list_all(head):
|
||||
items = []
|
||||
data= []
|
||||
current = head
|
||||
while current:
|
||||
items.append((current['name'], current['phone']))
|
||||
data.append((current['name'], current['phone']))
|
||||
current = current['next']
|
||||
return sorted(items) # Сортировка O(N log N)
|
||||
return quick_sort(data)
|
||||
|
||||
#хэш-таблица
|
||||
def ht_insert(buckets, name, phone):
|
||||
id=hash(name)%len(buckets)
|
||||
buckets[id] = ll_insert(buckets[id], name, phone)
|
||||
|
||||
def ht_find(buckets, name):
|
||||
id= hash(name)%len(buckets)
|
||||
return ll_find(buckets[id], name)
|
||||
|
||||
def ht_delete(buckets, name):
|
||||
id= hash(name)%len(buckets)
|
||||
buckets[id] = ll_delete(buckets[id], name)
|
||||
|
||||
def ht_list_all(buckets):
|
||||
data = []
|
||||
for head in buckets:
|
||||
current = head
|
||||
while current:
|
||||
data.append((current['name'], current['phone']))
|
||||
current = current['next']
|
||||
return quick_sort(data)
|
||||
|
||||
|
||||
|
||||
#Двоичное дерево поиска
|
||||
def bst_insert(root, name, phone):
|
||||
if root is None:
|
||||
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
||||
if root['name'] == name:
|
||||
root['phone'] = phone
|
||||
elif name<root['name']:
|
||||
root['left'] = bst_insert(root['left'], name, phone)
|
||||
else:
|
||||
root['right'] = bst_insert(root['right'], name, phone)
|
||||
return root
|
||||
|
||||
def bst_find(root, name):
|
||||
if root is None:
|
||||
return None
|
||||
if root['name'] == name:
|
||||
return root['phone']
|
||||
elif name<root['name']:
|
||||
return bst_find(root['left'], name)
|
||||
else:
|
||||
return bst_find(root['right'], name)
|
||||
|
||||
def minimum(node):
|
||||
current = node
|
||||
while current['left'] is True:
|
||||
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=minimum(root['right'])
|
||||
root['name']=min['name']
|
||||
root['phone']=min['phone']
|
||||
root['right']=bst_delete(root['right'], min['name'])
|
||||
return root
|
||||
|
||||
def bst_list_all(root):
|
||||
result=[]
|
||||
if root:
|
||||
result.extend(bst_list_all(root['left']))
|
||||
result.append((root['name'], root['phone']))
|
||||
result.extend(bst_list_all(root['right']))
|
||||
return result
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user