diff --git a/src/collections/HeapArray.ts b/src/collections/HeapArray.ts new file mode 100644 index 0000000..74dbe07 --- /dev/null +++ b/src/collections/HeapArray.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 HeapArray, 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: HeapArray = new HeapArray([a, b], HeapType.MAX); +console.log((h.get() as Person).getName());