Questions tagged [heap]

A heap (data structure) is a tree that is ordered with respect to depth. When the question concerns process memory set aside for dynamic allocation, tag with heap-memory instead.

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent node of B then key(A) is ordered with respect to key(B) with the same ordering applying across the heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node (this kind of heap is called max heap) or the keys of parent nodes are less than or equal to those of the children (min heap).

There are several variants of this data structure, and when this is relevant for the question, add the more specific tag as well: , , , , , ,

For questions on the heap-sort algorithm, add the tag.

Don't use this tag for the following:

1845 questions
0
votes
1 answer

heapq.merge default key?

Refer to the following code copied from solution 4 of this page - https://discuss.leetcode.com/topic/50450/slow-1-liner-to-fast-solutions/2: streams = map(lambda u: ([u+v, u, v] for v in nums2), nums1) stream = heapq.merge(*streams) nums2,…
Jobs
  • 3,317
  • 6
  • 26
  • 52
0
votes
2 answers

How to implement heap traversal in Python

I just made a heap class in python and am still working in Tree traversal. When I invoked inoder function, I got error said None is not in the list. In my three traversal functions, they all need left and right function. I assume that the problem is…
Allen
  • 59
  • 9
0
votes
3 answers

Create an unsorted heap (left to right) using given array

I've been trying to solve this problem for two days and I can't figure out how to represent an array to a heap(left to right). I tried looking for answer on other websites but I can't find one. The problem is there is a given array. For example…
Johji
  • 240
  • 1
  • 19
0
votes
1 answer

How to takes a filename from the command line & outputs that file heap sorted?

I'm new to java and would appreciate it if someone could give me some help. Thanks in advance. I am trying to take a filename from the command line and output that file sorted (from my Heap.java) and print it out one word per line. Here is what I…
user6523889
0
votes
1 answer

Heap insertion and deletion

Firstly, I have to delete 7 from the heap and after that add 17 and 14. The problem is I dont know what that heap is. Is it a min heap? or a binomial heap? Can any of you explain me how to do it (or/and) draft each operation? Thanks
Nubzor
  • 522
  • 1
  • 5
  • 14
0
votes
0 answers

Solution of the longest consecutive subsequence without using a hash structure

When solving the longest consecutive subsequence problem (in linear time without sorting), the solution generally involves using a set library which normally internally uses hashing. For example, one typical solution: import java.io.*; import…
Tyler Durden
  • 11,156
  • 9
  • 64
  • 126
0
votes
0 answers

I am trying to create a minheap having 3 queries. I am stuck with this code and my testcases are not running

1- Insert 2 - delete an element 3- Min element It's a hackerrank problem named QHEAP1. Here's the link- https://www.hackerrank.com/challenges/qheap1 It is working for integer values but it's not working correctly for long values. #include…
yamit01
  • 1
  • 2
0
votes
1 answer

How does python order values in a heap?

For example, a list of random numbers: >>> x = [1,24,5,1,5,1,23,6] >>> heapq.heapify(x) [1, 1, 1, 6, 5, 5, 23, 24] Why does it arbitrarily do 6,5,5 and not 5,5,6 or 5,6,5?
Kevin
  • 977
  • 1
  • 10
  • 17
0
votes
1 answer

How to write a template?

i need to write a template with Nodes containing data with 2 data structures : a map and a minimum heap, both got the same nodes in it and every 2 same nodes are connected. the problem is that i need the heap to know the node fields for the heapify…
Roy Gavrielov
  • 607
  • 4
  • 10
  • 22
0
votes
2 answers

Max Heap is not working as expected

My Java code for Max Heap is not working as expected. Also it is not returning 90 as max when I remove from heap. /** * Created by mona on 5/25/16. */ public class Heap { private int[] heap; private int size; private int maxSize; …
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
0
votes
1 answer

Why `heapq.merge(some_lists_of_iterators)` does not return an iterator of iterators?

In the following code: import heapq nums = [1,2,3] result = [set([])] bases = [] for n in nums: bases.append(set([n])) # Now bases is [set([1]), set([2]), set([3])] def gen (base): for r in result: if base != r: …
Jay Wang
  • 2,650
  • 4
  • 25
  • 51
0
votes
0 answers

Increase values in MinHeap

I have a MinHeap of Integer For Example. I would like to ask if there is way to make increase for some value from the middle of the heap and still save the heap Sorted correctly . Thanks
0
votes
1 answer

Implementing a min heap in C

I am trying to implement a min heap in C but I am struggling with the sift down function. What I have so far is : static void sift_down(t_heap *h, int cur) { int min; if (!h->nodes[cur * 2] && !h->nodes[cur * 2 + 1]) …
Henry
  • 149
  • 2
  • 8
0
votes
1 answer

What is the equivalent for heapq of Python in Java?

I would like to know if there is any api available for Java which is just like heapq in Python.
Emil
  • 13,577
  • 18
  • 69
  • 108
0
votes
1 answer

heapq.nlargest produces inconsistent results

The following code produces inconsistent results if executed multiple times. I ran it on Debian 8.4 (jessie) with Python-3.5.1. from heapq import nlargest from operator import itemgetter dd = dict([('41', 768.0), ('2', 15275.0), ('9', 1728.0),…
zyxue
  • 7,904
  • 5
  • 48
  • 74