From 0b015aa3243e632998ad096b0847cf3a1f1467ac Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Mon, 30 May 2022 14:41:17 -0700 Subject: [PATCH] alias methods --- src/heap.js | 32 ++++++++++++++++++++++++++++---- src/maxHeap.js | 32 ++++++++++++++++++++++++++++---- src/minHeap.js | 32 ++++++++++++++++++++++++++++---- 3 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/heap.js b/src/heap.js index ec40293..3c333af 100644 --- a/src/heap.js +++ b/src/heap.js @@ -17,10 +17,6 @@ class Heap { this._compare = compare; this._nodes = Array.isArray(_values) ? _values : []; this._leaf = _leaf || null; - - this.push = this.insert; // Alias of insert - this.pop = this.extractRoot; // Alias of extractRoot - this.top = this.root; // Alias of root } /** @@ -185,6 +181,16 @@ class Heap { return this; } + /** + * Inserts a new value into the heap + * @public + * @param {number|string|object} value + * @returns {Heap} + */ + push(value) { + return this.insert(value); + } + /** * Removes and returns the root node in the heap * @public @@ -207,6 +213,15 @@ class Heap { return root; } + /** + * Removes and returns the root node in the heap + * @public + * @returns {number|string|object} + */ + pop() { + return this.extractRoot(); + } + /** * Applies heap sort and return the values sorted by priority * @public @@ -286,6 +301,15 @@ class Heap { return this._nodes[0]; } + /** + * Returns the root node in the heap + * @public + * @returns {number|string|object} + */ + top() { + return this.root(); + } + /** * Returns a leaf node in the heap * @public diff --git a/src/maxHeap.js b/src/maxHeap.js index b7e962a..0aa7194 100644 --- a/src/maxHeap.js +++ b/src/maxHeap.js @@ -23,10 +23,6 @@ class MaxHeap { constructor(getCompareValue, _heap) { this._getCompareValue = getCompareValue; this._heap = _heap || new Heap(getMaxCompare(getCompareValue)); - - this.push = this.insert; // Alias of insert - this.pop = this.extractRoot; // Alias of extractRoot - this.top = this.root; // Alias of root } /** @@ -39,6 +35,16 @@ class MaxHeap { return this._heap.insert(value); } + /** + * Inserts a new value into the heap + * @public + * @param {number|string|object} value + * @returns {Heap} + */ + push(value) { + return this.insert(value); + } + /** * Removes and returns the root node in the heap * @public @@ -48,6 +54,15 @@ class MaxHeap { return this._heap.extractRoot(); } + /** + * Removes and returns the root node in the heap + * @public + * @returns {number|string|object} + */ + pop() { + return this.extractRoot(); + } + /** * Applies heap sort and return the values sorted by priority * @public @@ -84,6 +99,15 @@ class MaxHeap { return this._heap.root(); } + /** + * Returns the root node in the heap + * @public + * @returns {number|string|object} + */ + top() { + return this.root(); + } + /** * Returns a leaf node in the heap * @public diff --git a/src/minHeap.js b/src/minHeap.js index a72b215..503f768 100644 --- a/src/minHeap.js +++ b/src/minHeap.js @@ -23,10 +23,6 @@ class MinHeap { constructor(getCompareValue, _heap) { this._getCompareValue = getCompareValue; this._heap = _heap || new Heap(getMinCompare(getCompareValue)); - - this.push = this.insert; // Alias of insert - this.pop = this.extractRoot; // Alias of extractRoot - this.top = this.root; // Alias of root } /** @@ -39,6 +35,16 @@ class MinHeap { return this._heap.insert(value); } + /** + * Inserts a new value into the heap + * @public + * @param {number|string|object} value + * @returns {Heap} + */ + push(value) { + return this.insert(value); + } + /** * Removes and returns the root node in the heap * @public @@ -48,6 +54,15 @@ class MinHeap { return this._heap.extractRoot(); } + /** + * Removes and returns the root node in the heap + * @public + * @returns {number|string|object} + */ + pop() { + return this.extractRoot(); + } + /** * Applies heap sort and return the values sorted by priority * @public @@ -84,6 +99,15 @@ class MinHeap { return this._heap.root(); } + /** + * Returns the root node in the heap + * @public + * @returns {number|string|object} + */ + top() { + return this.root(); + } + /** * Returns a leaf node in the heap * @public