Skip to content

Commit

Permalink
alias methods
Browse files Browse the repository at this point in the history
  • Loading branch information
eyas-ranjous committed May 30, 2022
1 parent 72846f5 commit 0b015aa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
32 changes: 28 additions & 4 deletions src/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 28 additions & 4 deletions src/maxHeap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 28 additions & 4 deletions src/minHeap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0b015aa

Please sign in to comment.