binary-heap

How to preserve the order of elements of the same priority in a priority queue implemented as binary heap?

瘦欲@ 提交于 2019-11-30 01:08:26
问题 I have created a binary heap, which represents a priority queue. It's just classical well known algorithm. This heap schedules a chronological sequence of different events ( the sort key is time ). It supports 2 operation: Insert and Remove. Each node's key of the heap is greater than or equal to each of its children. However, adding events with the same key doesn't preserve the order they were added, because each time after Remove or Insert were called, the heap-up and the heap-down

What is the time complexity of constructing a PriorityQueue from a collection?

微笑、不失礼 提交于 2019-11-29 10:48:52
What is the complexity of Java's PriorityQueue constructor with a Collection ? I used the constructor: PriorityQueue(Collection<? extends E> c) Is the complexity O(n) or O(n*log(n))? Stuart Marks The time complexity to initialize a PriorityQueue from a collection, even an unsorted one, is O(n). Internally this uses a procedure called siftDown() to "heapify" an array in-place. (This is also called pushdown in the literature.) This is counterintuitive. It seems like inserting an element into a heap is O(log n) so inserting n elements results in O(n log n) complexity. This is true if you insert

What is the difference between binary heaps and binomial heaps?

纵然是瞬间 提交于 2019-11-28 19:35:09
I need to know the main difference between binary and binomial heaps regardless of the their structure difference that binary heaps can have only two child (tree representation) and binomial heaps can have any number of children. I am actually just wondering that what so special in organizing the binomial tree structure in such a way that the first child have on one node second have two third have four and so on? What if, if we use some normal tree for heaps without restriction of two child and then apply the union procedure and just make one heap the left child of the other heaps? The key

Real world applications of Binary heaps and Fibonacci Heaps [closed]

杀马特。学长 韩版系。学妹 提交于 2019-11-28 17:51:26
What are the real world applications of Fibonacci heaps and binary heaps? It'd be great if you could share some instance when you used it to solve a problem. Edit: Added binary heaps also. Curious to know. You would rarely use one in real life. I believe the purpose of the Fibonacci heap was to improve the asymptotic running time of Dijkstra's algorithm. It might give you an improvement for very, very large inputs, but most of the time, a simple binary heap is all you need. From Wiki: Although the total running time of a sequence of operations starting with an empty structure is bounded by the

How do I create a BinaryHeap that pops the smallest value, not the largest?

Deadly 提交于 2019-11-28 13:34:51
I can use the std::collections::BinaryHeap to iterate over a collection of a struct in the greatest to least order with pop , but my goal is to iterate over the collection from least to greatest. I have succeeded by reversing the Ord implementation: impl Ord for Item { fn cmp(&self, other: &Self) -> Ordering { match self.offset { b if b > other.offset => Ordering::Less, b if b < other.offset => Ordering::Greater, b if b == other.offset => Ordering::Equal, _ => Ordering::Equal, // ?not sure why compiler needs this } } } Now the BinaryHeap returns the Item s in least to greatest. Seeing as how

Finding last element of a binary heap

会有一股神秘感。 提交于 2019-11-28 07:37:05
quoting Wikipedia : It is perfectly acceptable to use a traditional binary tree data structure to implement a binary heap. There is an issue with finding the adjacent element on the last level on the binary heap when adding an element which can be resolved algorithmically ... Any ideas on how such an algorithm might work? I was not able to find any information about this issue, for most binary heaps are implemented using arrays. Any help appreciated. Recently, I have registered an OpenID account and am not able to edit my initial post nor comment answers. That's why I am responding via this

how to determine if the kth largest element of the heap is greater than x

隐身守侯 提交于 2019-11-27 18:12:42
Consider a binary heap containing n numbers (the root stores the greatest number). You are given a positive integer k < n and a number x. You have to determine whether the kth largest element of the heap is greater than x or not. Your algorithm must take O(k) time. You may use O(k) extra storage Simple dfs can do the job. We have a counter set to zero. Start from the root and in each iteration check the value of current node; if it is greater than x, then increase the counter and continue the algorithm for one of the child nodes. The algorithm terminates if counter is bigger than equal k or if

Real world applications of Binary heaps and Fibonacci Heaps [closed]

自作多情 提交于 2019-11-27 10:48:59
问题 What are the real world applications of Fibonacci heaps and binary heaps? It'd be great if you could share some instance when you used it to solve a problem. Edit: Added binary heaps also. Curious to know. 回答1: You would rarely use one in real life. I believe the purpose of the Fibonacci heap was to improve the asymptotic running time of Dijkstra's algorithm. It might give you an improvement for very, very large inputs, but most of the time, a simple binary heap is all you need. From Wiki:

How do I create a BinaryHeap that pops the smallest value, not the largest?

狂风中的少年 提交于 2019-11-27 07:44:28
问题 I can use the std::collections::BinaryHeap to iterate over a collection of a struct in the greatest to least order with pop , but my goal is to iterate over the collection from least to greatest. I have succeeded by reversing the Ord implementation: impl Ord for Item { fn cmp(&self, other: &Self) -> Ordering { match self.offset { b if b > other.offset => Ordering::Less, b if b < other.offset => Ordering::Greater, b if b == other.offset => Ordering::Equal, _ => Ordering::Equal, // ?not sure

Finding last element of a binary heap

£可爱£侵袭症+ 提交于 2019-11-27 05:45:17
问题 quoting Wikipedia: It is perfectly acceptable to use a traditional binary tree data structure to implement a binary heap. There is an issue with finding the adjacent element on the last level on the binary heap when adding an element which can be resolved algorithmically ... Any ideas on how such an algorithm might work? I was not able to find any information about this issue, for most binary heaps are implemented using arrays. Any help appreciated. Recently, I have registered an OpenID