Compare commits

...

2 Commits

2 changed files with 11 additions and 12 deletions

View File

@ -157,7 +157,7 @@ class BinaryTreeNode(MemoryCell):
def __str__(self):
return str(self.value)
class BinaryTree(BinaryTreeNode):
class BinaryTree:
def __init__(self):
self.root: BinaryTreeNode | None = None

View File

@ -1,7 +1,7 @@
from utils.memory_array import MemoryArray
from utils.memory_cell import MemoryCell
from utils.literal import Literal
from utils.constants import MAX_VALUE
from utils.constants import MIN_VALUE
from utils.memory_range import mrange
# Impl of MemoryArray says we cant add our own Datatypes beside Literal and List
@ -28,14 +28,14 @@ class HeapEntry:
return True
if isinstance(other, (int, float)):
return self.getPriority().value > other
return self.getPriority() > other.getPriority()
return self.getPriority() < other.getPriority()
def __gt__(self, other):
if other is None:
return False
if isinstance(other, (int, float)):
return self.getPriority().value < other
return self.getPriority() < other.getPriority()
return self.getPriority() > other.getPriority()
def __eq__(self, other):
return self.getPriority() == other.getPriority()
@ -49,7 +49,7 @@ class PriorityQueue:
# Add uninitialized HeapEntry Values so the Adds/Compares do not fail on emtpy stack.
# Would have to switch to MIN_VALUE if we switch what is a "Higher" Prio
for i in mrange(max_size.value):
self.heap[i].set([HeapEntry(MAX_VALUE, MAX_VALUE)])
self.heap[i].set([HeapEntry(MIN_VALUE, MIN_VALUE)])
self.size = MemoryCell(0)
def parent(self, i: Literal) -> Literal:
@ -124,7 +124,7 @@ class PriorityQueue:
entries = []
for i in mrange(self.size.value):
entry = self.heap[i].value[0]
if entry.getItem() != MAX_VALUE:
if entry.getItem() != MIN_VALUE:
entries.append(str(entry))
return "[" + ", ".join(entries) + "]"
@ -132,7 +132,7 @@ def testQueueRandom(number: int):
import random
import string
pq = PriorityQueue(Literal(101))
pq = PriorityQueue(Literal(number))
entries = []
for _ in range(number):
@ -144,13 +144,12 @@ def testQueueRandom(number: int):
print(pq)
for entry in entries:
print(entry)
print(f"Unprioritized: {entry}")
while not pq.isEmpty():
print(pq.pop())
if __name__ == '__main__':
# Proof of Concept
testEntry = HeapEntry("A", 2)
@ -174,13 +173,13 @@ if __name__ == '__main__':
assert(not pq.isEmpty())
assert(pq.pop() == HeapEntry("A", 1))
assert(pq.isEmpty())
pq.insert(HeapEntry("A", 1))
pq.insert(HeapEntry("C", 3))
pq.insert(HeapEntry("B", 2))
pq.insert(HeapEntry("A", 1))
assert(pq.size == Literal(3))
assert(pq.pop() == HeapEntry("A", 1))
assert(pq.pop() == HeapEntry("B", 2))
assert(pq.pop() == HeapEntry("C", 3))
assert(pq.pop() == HeapEntry("B", 2))
assert(pq.pop() == HeapEntry("A", 1))
pq.insert(HeapEntry("A", 1))
pq.insert(HeapEntry("C", 3))
pq.insert(HeapEntry("B", 2))