Skip to content

Add Double Ended Heap - #341

Closed
JeonSeongBae wants to merge 2 commits into
TheAlgorithms:masterfrom
JeonSeongBae:master
Closed

JeonSeongBae wants to merge 2 commits into
TheAlgorithms:masterfrom
JeonSeongBae:master

Conversation

@JeonSeongBae

Copy link
Copy Markdown
Contributor

A double-ended priority queue (DEPQ) or double-ended heap is a data structure similar to a priority queue or heap, but allows for efficient removal of both the maximum and minimum, according to some ordering on the keys (items) stored in the structure. Every element in a DEPQ has a priority or value. In a DEPQ, it is possible to remove the elements in both ascending as well as descending order.

@christianbender christianbender left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Work! I have some requests

Double exponent = Math.floor(Math.log(pos + 1) / Math.log(2)) - 1;
int temp = (int) (pos + Math.pow(2, exponent));
// Return the parent of the index location if there is no partner
if (temp > elements)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use curly brackets. For example

if (temp > elements) {
   // something
}

see http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html#449


// Insert key at index at position in min-heap
private void minInsert(int at, int key) {
for (int parent; (parent = (at - 1) / 2) != 0 && key < deap[parent]; deap[at] = deap[parent], at = parent)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you simplify this for-loop ? Write a while loop with a certain condition.


// Insert key at index at position in max-heap
private void maxInsert(int at, int key) {
for (int parent; (parent = (at - 1) / 2) != 0 && key > deap[parent]; deap[at] = deap[parent], at = parent)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing above.

* the larger value in the leaf node and store the temp in the larger value.
*/
int bigminPartner = 0;
if (deap[minPartner(i)] > deap[2 * minPartner(i) + 1] && deap[minPartner(i)] > deap[2 * minPartner(i) + 2])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break this line of max. 80 columns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants