A heap is a tree-based data structure that satisfies the heap property. For questions about memory allocated from the system heap, use the [memory-management] tag.

learn more… | top users | synonyms

13
votes
2answers
893 views

TopCoder problem “Lottery” - SRM 144 (Division I Level Two)

The problem statement Summary: The rules of a lottery are defined by two integers (choices and blanks) and two boolean variables (sorted and unique). choices represents the highest valid number ...
12
votes
3answers
1k views

Implementation of binary min-heap data structure

What do you think is wrong with this code, and how can it be improved? What corner case have I overlooked, if any? Note: I do not want to use any STL features here, but I'm okay with anything else ...
11
votes
2answers
9k views

Implementation of binary heap in C++

Things I can think of include integer overflow, if the input type is int, etc. But other than that, what do you think is wrong with this code, design-wise, style-wise, and also in terms of other ...
8
votes
3answers
150 views

Dijkstra's algorithm without relaxation

I'm trying to implement Dijkstra's algorithm in Python, but the issue is that Python doesn't have support for key-based heaps out of the box, so applying relaxation step of the classical algorithm ...
8
votes
1answer
25k views

Implementation of Heap Sort

Is this the correct implementation of Heap Sort using Java? How can it be improved further? ...
7
votes
2answers
145 views

Implementation of a binary heap

I've read a little about binary heaps/priority queues and decided to try my hand at making my own implementation of one. I'm not the most experienced programmer and I would be really grateful if you ...
6
votes
2answers
252 views

Function to get top k words in a given String

This is a two-part job: to identify a word to find the top k words I've tried to use regex to split but it's taking 10x more time when string is really long. ...
6
votes
2answers
274 views

Simple binary heap in C#

I've written a simple binary heap in C# and I want to know if it has any problems or if I can make it better. ...
6
votes
1answer
2k views

MaxHeap implementation

I'd like this to be reviewed: ...
6
votes
1answer
63 views

D-ary max heap implementation

I implemented a D-ary max heap backed by a vector for resizing. I would like to know any possible improvements in performance, design, and in the code in general. ...
6
votes
1answer
410 views

Scala heap implementation

I'm a Scala beginner, so it'd be great if anyone would be so kind to give some feedback. ...
5
votes
2answers
1k views

MinHeap implementation

Please comment on this. ...
5
votes
2answers
119 views

Merge k Sorted Arrays

Please review my answer for this interview question: Merge k sorted arrays, using min heap. ...
5
votes
2answers
1k views

Constraint Programming: Map color problem

I've written some python code to solve the map coloring problem. In my code, I represent the problem using Territory and ...
5
votes
2answers
2k views

Min Heap implementation with Dijkstra's algorithm

I am implementing Dijkstra's Algorithm using Min Heap to speed up the code. For a small number of nodes, the code is really running very fast. But for a large number of nodes, my code is throwing ...
4
votes
4answers
156 views

Find Kth largest element in an array

I am preparing for interviews where my code would be judged based on optimizations, OOP concepts, code readability as well as my knowledge of JAVA and design patterns. What comments will you give in ...
4
votes
3answers
140 views

Time limit exceeded in Heap

I wrote a Heap, but the judge system reports "time limit exceeded" in some tests. I use a sift_down to build, so I don't know why. Maybe I can improve the build? ...
4
votes
2answers
278 views

Inefficient hash map operation provokes OutOfMemory: Java heap space error

I know I can increase the size of the heap but that seems like a poor solution. This program runs correctly on small files but when run on large data sets it crashes with the OutOfMemory: Java heap ...
4
votes
1answer
123 views

Tree heap Haskell code

I'd like a review of Haskell tree heap code in Turning a tree into a heap in Haskell. ...
4
votes
1answer
65 views

Heap selection sort in Java

See the next iteration. I designed and implemented this adaptive heap sort in Java. It works as follows: Copy the range to be sorted into auxiliary array \$A\$ Create an empty run heap \$H\$ Scan ...
4
votes
2answers
105 views

Heap implementation of the “median maintenance” algorithm

I have implemented a heap data structure for Coursera's algorithms course. The problem I have to solve is: The goal of this problem is to implement the "Median Maintenance" algorithm. The text ...
4
votes
1answer
156 views

Binary heap with O(1) lookup and O(log n) change key

I needed a priority queue that allowed efficient searching and changing of keys. I tried std::priority_queue, but the interface was extremely limiting. Not being ...
4
votes
1answer
167 views

A functional binary heap implementation

I've implemented a binary heap in F#. It's pure and uses zippers for tree modification. To test it out I have implemented heap sort using it but it takes 10 seconds to sort a list of 100 000. Regular ...
4
votes
2answers
2k views

Heap implementation using pointer

I know heaps are commonly implemented by an array. But, I wanted to share my pointer implementation and have your feedback(s) :) General idea: I convert the index to its binary value and then trace ...
3
votes
4answers
305 views

MinHeap implementation in Java

I have made my own MinHeap in Java. I would like to get review comments on the same. ...
3
votes
1answer
37 views

d-ary heap in C

Now I have this d-ary heap data structure. Note that for d = 2 this is a binary heap. The client programmer specifies the value ...
3
votes
0answers
100 views

Binomial Heap in Haskell

Here's a partial binomial heap implementation in Haskell (just merge and insert): ...
2
votes
1answer
99 views

A different implementation of Heap Sort

The following implementation is a bit different form the standard implementations I have read. Is it correct? ...
2
votes
1answer
63 views

Sorting an integer array using heap sort

I have written this program to sort an array using heap sort. Please review it for further improvements. ...
2
votes
2answers
980 views

Merge N sorted list

Given n sorted lists, merge them. This is a fairly common question, attributed to plenty of interview question websites. I'm looking for code-review, optimizations and best practices. I'm also ...
2
votes
1answer
366 views

Binomial heap in Java

I have this implementation of a binomial heap providing insert, decrease-key and extract in logarithmic time. MinPriorityQueue.java: ...
2
votes
1answer
321 views

Binary HeapSort and Ternary HeapSort implementation

This is my take on binary and ternary heapsort implementation for a university assignment. The code works but I wonder if there are any mistakes or things to improve. ...
2
votes
1answer
657 views

Max heap implementation

I have implemented a max-Heap data structure backed by an array. I wanted to know whether this is an acceptable implementation or not. Any improvements or suggestions are welcome. ...
2
votes
1answer
884 views

Maxheap code review in Java

Just looking for some feedback on my implementation of a maxheap mostly relating to style. Trying to create good habits as i learn to program. Constructive criticism is appreciated! Thanks. Note: ...
2
votes
1answer
2k views

Fibonacci heap implementation

I'd like this reviewed. ...
1
vote
1answer
1k views

Heap memory preallocation (intended for games)

Currently, in my games, I'm using new and delete to create entities and components. It works fine, but there are slowdowns when ...
1
vote
1answer
68 views

HeapSort Efficiency

For heap (array based) sort, we need to start at index 1, ignoring the 0th index. If I want to make a static function like ...
1
vote
1answer
81 views

Max-heap implementation with List

I am trying to create max-heap for practice. Are there any bugs? Are there ways that I can improve the code quality? ...
1
vote
1answer
25 views

Heapsort for arbitrary ranges in Java

I plan to work on Introsort, which requires a working implementation of heap sort. Also, the heap sort implementation must be able to sort arbitrary ranges within an array, which, in turn, requires ...
1
vote
0answers
35 views

Heap selection sort - follow-up

See the previous and initial iteration. I got rid of Run objects and just maintain two integer arrays in the RunHeap: one for ...
1
vote
0answers
21 views

Building a correct d-max heap bottom up

I want to implement a d-max heap using a bottom-up approach. This way I will start from the last parent in the tree and build myself up to the root of the heap. We will compare children and parents in ...
1
vote
0answers
154 views

Binary heap implementation in Golang

I have implemented a binary heap in Go to get familiar with the language. GitHub ...
0
votes
2answers
2k views

Optimizing Dijkstra implementation using PriorityQueue in Java

I am trying to make Dijkstra implementation more efficient. Below is the code for class Node which implements Comparable so it ...
-2
votes
1answer
64 views

kth smallest element in min heap [closed]

I am working on to find the kth smallest element in min heap. I have code for this whose complexity is \$O(k \log k)\$. I tried to improve it to \$O(k)\$. ...