2019-03-26 in class

This commit is contained in:
Claudio Maggioni 2019-03-27 23:01:13 +01:00
parent 8301104864
commit 9e6722a861
3 changed files with 183 additions and 18 deletions

77
\ Normal file
View file

@ -0,0 +1,77 @@
#!/usr/bin/env python3
import sys
def heap_extract_max(A):
m = A[0]
A[0] = A.pop()
size = len(A)
i = 0
exit = not size == 0
while exit:
left = heap_left(size, i)
print("left =", left)
if left == None:
break
right = heap_right(size, i)
print("right =", right)
if right == None:
if A[i] < A[left]:
A[i], A[left] = A[left], A[i]
break
ma = max_3(A[i], A[left], A[right])
if ma == A[i]:
break
elif ma == A[left]:
A[i], A[left] = A[left], A[i]
i = left
else:
A[i], A[right] = A[right], A[i]
i = right
return m
def max_3(a, b, c):
if a > b:
if a > c:
return a
else:
return c
else:
if b > c:
return b
else:
return c
def heap_parent(n):
n = n + 1
if n == 1:
return None
else:
return (n // 2) - 1
def heap_left(size, n):
n = n + 1
l = n * 2
if l > size:
return None
else:
return l - 1
def heap_right(size, n):
n = n + 1
l = n * 2 + 1
if l > size:
return None
else:
return l - 1
if __name__ == "__main__":
args = [int(x) for x in sys.argv[1:]]
print(args)
print(heap_extract_max(args))
print(args)

View file

@ -1,21 +1,38 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
import math
def heap_extract_max(A): def heap_sort(A):
m = A[0] heap_build_max_heap(A)
A[0] = A.pop()
size = len(A) print("Initial heap:")
heap_print(A, len(A))
print()
for i in range(len(A)-1, 0, -1):
A[0], A[i] = A[i], A[0]
heap_max_heapify(A, i)
print("\nNew heap: length =", i)
heap_print(A, i)
print()
def heap_max_heapify(A, size):
if size < 2:
return
print("size =", size)
i = 0 i = 0
exit = size == 0
while exit:
left = heap_left(i)
if left == None:
break
right = heap_right(i) left = heap_left(size, i)
if right == None: print("left =", left)
while left is not None:
right = heap_right(size, i)
print("right =", right)
if right is None:
if A[i] < A[left]: if A[i] < A[left]:
A[i], A[left] = A[left], A[i] A[i], A[left] = A[left], A[i]
break break
@ -30,7 +47,38 @@ def heap_extract_max(A):
A[i], A[right] = A[right], A[i] A[i], A[right] = A[right], A[i]
i = right i = right
return m left = heap_left(size, i)
print("left =", left)
def heap_build_max_heap(A):
if len(A) < 2:
return
for i in range(math.ceil(len(A)/2), 1, -1):
heap_max_heapify(A, i)
def heap_print(A, size):
i = 1
newline = False
while i <= size:
print(A[i-1], end='\t')
newline = False
i = i + 1
if is_power_2(i):
print()
newline = True
if not newline:
print()
def is_power_2(n):
while n > 1:
if n % 2 == 1:
return False
n = n // 2
return True
def max_3(a, b, c): def max_3(a, b, c):
if a > b: if a > b:
@ -44,29 +92,35 @@ def max_3(a, b, c):
else: else:
return c return c
def heap_parent(n): def heap_parent(n):
n = n + 1 n = n + 1
if n == 1: if n == 1:
return None return None
else else:
return (n // 2) - 1 return (n // 2) - 1
def heap_left(size, n): def heap_left(size, n):
n = n + 1 n = n + 1
l = n * 2 l = n * 2
if l > size if l > size:
return None return None
else else:
return l - 1 return l - 1
def heap_right(size, n): def heap_right(size, n):
n = n + 1 n = n + 1
l = n * 2 + 1 l = n * 2 + 1
if l > size if l > size:
return None return None
else else:
return l - 1 return l - 1
if __name__ == "__main__": if __name__ == "__main__":
args = [int(x) for x in sys.argv[1:]] args = [int(x) for x in sys.argv[1:]]
heap_sort(args)
print(args)

View file

@ -143,3 +143,37 @@ def right(x):
**Max heap property**: for all i > 1 A[parent(i)] >= A[i] **Max heap property**: for all i > 1 A[parent(i)] >= A[i]
# Data structures
Way to organize information
A data structure has an interface (functions to work with the DS)
A data structure has data and meta-data (like size, length).
## Stack (LIFO)
### Operations:
- `push(S, x)` (put one element, move TOS)
- `pop(S)` (remove element in TOS, move TOS)
- `stack-empty(S)` (returns TRUE if stack is empty)
## Queue (FIFO)
## Structure
- Based on array
- `length`
- `head`
- `tail`
Queue has always 1 cell free to avoid confusion with full/empty
## Dictionary
Data structure for fast search
## Direct-access table
Implementation of dictionary