-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab5.py
More file actions
48 lines (37 loc) · 1.17 KB
/
lab5.py
File metadata and controls
48 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Course: CS2302 Data Structures
Author: Javier Navarro
Assignment: Lab 5 Option B
Instructor: Diego Aguirre
TA: Manoj Saha
Last modified: 11/28/18
Purpose: Implement a heap and use heap sort
"""
import Heap
def main():
heap = Heap.Heap()
validInput = False
while(validInput == False):
try:
fileName = input("Enter file name:")
fill_heap(fileName, heap)
validInput = True
except FileNotFoundError:
print("File not found. Try again.")
print("\nHeap before heapsort:")
heap.printHeap()
heap.heap_sort()
print("\nHeap after heapsort:")
heap.printHeap()
#receives a heap and fills it with the given fileName
def fill_heap(fileName, heap):
with open(fileName, "r") as file: #opens file with given fileName
for line in file:
line = line.split(",")
for item in line:
try: #catch errors in the input file
if item != "":
heap.insert(int(item))
except ValueError:
pass
main()