From 0b1d256d711955f7571c24f258087bdb7c7b9f05 Mon Sep 17 00:00:00 2001 From: Simone Spaccarotella Date: Sun, 2 Jan 2022 06:37:59 +0000 Subject: [PATCH 1/2] Add generic Heap --- src/collections/Heap.ts | 96 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/collections/Heap.ts diff --git a/src/collections/Heap.ts b/src/collections/Heap.ts new file mode 100644 index 0000000..5f9ef82 --- /dev/null +++ b/src/collections/Heap.ts @@ -0,0 +1,96 @@ +enum HeapType { MIN, MAX }; + +abstract class Comparable { + private value: X; + + constructor(value: X) { + this.value = value; + } + + public getValue(): X { + return this.value; + } + + /** + * Return true if this object value is strictly greater than the specified item's value. + * + * @param item the "comparable" object. + */ + public abstract gt(item: Comparable): boolean; + /** + * Return true if this object value is strictly less than the specified item's value. + * + * @param item the "comparable" object. + */ + public abstract lt(item: Comparable): boolean; +} + +class TheNumber extends Comparable { + gt(item: Comparable): boolean { + return this.getValue() > item.getValue(); + } + + lt(item: Comparable): boolean { + return this.getValue() < item.getValue(); + } +} + +class Person extends Comparable { + private name: string; + + constructor(name: string, age: number = 0) { + super(age); + this.name = name; + } + + public getName(): string { + return this.name; + } + + public gt(item: Comparable): boolean { + return this.getValue() > item.getValue(); + } + + public lt(item: Comparable): boolean { + return this.getValue() < item.getValue(); + } +} + +class Heap, X> { + private heap: Array; + private heapType: HeapType; + + constructor(heap: Array = [], heapType: HeapType = HeapType.MAX) { + this.heap = heap; + this.heapType = heapType; + } + + public get(): Comparable { + if (this.heap.length === 0) return undefined; + + let mu = this.heap[0]; + for (let i = 1; i < this.heap.length; i++) { + if (this.heapType === HeapType.MAX && this.heap[i].gt(mu)) { + mu = this.heap[i]; + } + + if (this.heapType === HeapType.MIN && this.heap[i].lt(mu)) { + mu = this.heap[i]; + } + } + + return mu; + } +} + +// const a = new TheNumber(3); +// const b = new TheNumber(11); + +// const h: Heap = new Heap([a, b], HeapType.MAX); +// console.log(h.get()); + +const a = new Person('Simone', 38); +const b = new Person('Lalla', 40); + +const h: Heap = new Heap([a, b], HeapType.MAX); +console.log((h.get() as Person).getName()); From 17225a9b4485bd3d7cb44b00e874053e87e41a3d Mon Sep 17 00:00:00 2001 From: Simone Spaccarotella Date: Mon, 3 Jan 2022 19:40:03 +0000 Subject: [PATCH 2/2] Rename Heap in HeapArray --- src/collections/{Heap.ts => HeapArray.ts} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/collections/{Heap.ts => HeapArray.ts} (94%) diff --git a/src/collections/Heap.ts b/src/collections/HeapArray.ts similarity index 94% rename from src/collections/Heap.ts rename to src/collections/HeapArray.ts index 5f9ef82..74dbe07 100644 --- a/src/collections/Heap.ts +++ b/src/collections/HeapArray.ts @@ -56,7 +56,7 @@ class Person extends Comparable { } } -class Heap, X> { +class HeapArray, X> { private heap: Array; private heapType: HeapType; @@ -92,5 +92,5 @@ class Heap, X> { const a = new Person('Simone', 38); const b = new Person('Lalla', 40); -const h: Heap = new Heap([a, b], HeapType.MAX); +const h: HeapArray = new HeapArray([a, b], HeapType.MAX); console.log((h.get() as Person).getName());