Questions tagged [min-heap]

181 questions
3
votes
1 answer

What is wrong with my Heapsort algorithm for building a min heap?

I'm prepping for software developer interviews and have been working on algorithm problems. My book shows a Heapsort algorithm that can sort an unordered array in increasing order. I'm trying to modify it so it can sort with a min heap. But when I…
MNRC
  • 263
  • 1
  • 11
3
votes
1 answer

Is Min Heap Function

I want to write a function that tells me whether a given list is a min heap. What I have written so far: def is_min_heap(L): return _is_min_heap(L, 0) def _is_min_heap(L, i): if #base case else: return (L[i] <…
Mat.S
  • 1,814
  • 7
  • 24
  • 36
2
votes
1 answer

Constructing Min/Max Binary Heap

Given an inorder-traversal list, what's the best way to create a Binary Min/Max Heap? I'm trying to confine with the following constructs: No array to be used in the binary-heap. Implementation is node-based. BinaryNode { value, parent, l_child,…
gvaish
  • 9,374
  • 3
  • 38
  • 43
2
votes
1 answer

Find K Pairs with Smallest Sums

The question is this: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u, v) which consists of one element from the first array and one element from the second array. Return the k pairs…
user19089374
2
votes
1 answer

Is this logic possible? (concerns: binary min heap, priority queue, insert at index 0 but root is at index 1)

I have a working code (with the required efficiency) for a binary min heap priority queue that keeps index 0 empty and has the root node at index 1 (children are 2i and 2i+1). I insert the new value into the heap at the first available empty space…
2
votes
1 answer

Overloading comparison operators in custom class for priority_queue

I am trying to create a min heap of "number" using priority_queue. "number" is a class defined by me that has three private variables: int value; //This holds the value to be read in the heap vector list; //This holds the vector…
2
votes
1 answer

Why do some people override the comparator function for implementing minheap using PriorityQueue even though PQ in java is a min-heap by default?

I searched a bit and looks like a PriorityQueue work like a min-heap by default in Java. So, why override the comparator? I have seen people do it even for integers. Am I missing something here? I am new to java. I tried both of the below codes to…
Saikip
  • 21
  • 4
2
votes
1 answer

Min/Max Heap implementation in Python

This is my implementation of a MinHeap and MaxHeap in python. This uses a comparator to reverse the sequence of storage in the MaxHeap import heapq class MinHeap: def __init__(self): self.heap = [] def push(self, item): …
Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
2
votes
1 answer

MinHeap/MaxHeap implementation in Python

I scoured the web to come up with this implementation of MinHeap and Maxheap in Python. import heapq class MinHeap: def __init__(self): self.heap = [] def push(self, item): heapq.heappush(self.heap, item) def…
Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
2
votes
1 answer

Running top k elements in a sorted, fixed size list / python

I am trying to keep a list of top k elements of a large set of tuples. Since keeping it in the memory is impossible, I want to use a fixed size list to keep only the top k values (with the keys). I have tried to use min heap but python's heap is…
dusa
  • 840
  • 3
  • 14
  • 31
2
votes
0 answers

In-place algorithm for creating min-heap of a Binary search tree

I am trying to implement in-place algorithm of min-heap. I converted the BST to sorted linked list, and following is the code snippet public void inorder(Node root) { if (isEmpty(root)) return; inorder(root.left); …
2
votes
1 answer

Max and Min heap with the same elements

Consider the following example. I am adding random numbers to min heap and at the same time I am adding the same numbers in the same order to the max heap. So at the end those 2 heaps will have the same numbers with difference one being min heap and…
kvway
  • 494
  • 4
  • 16
2
votes
1 answer

Min-HeapSort does not sort after ~350 elements

I'm implementing a heapSort, using min-heap, and it sorts fine, below ~350 elements. Once the array has approximately 350 elements, it doesn't sort correctly. I'm not sure where the error lies, but I believe it might have to do with extractMin(), or…
2
votes
1 answer

What is the time complexity of finding the minimum element in a min heap?

I was reading Problem1 (Part-b) mentioned in this document: Finding the minimum element of a binary min heap takes logarithmic time: T/F? It should be true, because when we construct a max-heap, the time complexity is O(logn)as mentioned here.…
John Lui
  • 1,434
  • 3
  • 23
  • 37
2
votes
2 answers

Use PriorityQueue as a minHeap

When trying to solve this problem https://www.hackerrank.com/challenges/cut-the-tree I was trying to always pick and cut a leaf and then combine its weight to the node that it connects. I was using a PriorityQueue to store all the nodes, and using…
1736964698
  • 310
  • 1
  • 5
1 2
3
12 13