heap

Does python have a built in min-heap data structure?

雨燕双飞 提交于 2019-12-14 02:27:10
问题 Does python have a built in min-heap data structure in 2.7.3? I don't want to import code. I want something like myheap = minheap(key=lambda x: x[1]) myheap.add(obj) o = myheap.pop() Is this possible? 回答1: Like everybody says, heapq is it -- but, as nobody's mentioned yet, it doesn't support a key= ! So you need to fall back to the good old DSU (decorate-sort-undecorate) idiom that key= uses internally wherever it's supported (alas not in heapq , except for the functions nlargest and

Tracking a node inside an heap

十年热恋 提交于 2019-12-13 21:21:25
问题 I have this problem - i'm keeping a data structure that contains two different heaps , a minimum heap and a maximum heap that does not contain the same data. My goal is to keep some kind of record for each node location in either of the heaps and have it updated with the heaps action. Bottom line - i'm trying to figure out how can i have a delete(p) function that works in lg(n) complexity. p being a pointer data object that can hold any data. Thanks, Ned. 回答1: If your heap is implemented as

Can the stack grow into the heap?

半腔热情 提交于 2019-12-13 20:41:38
问题 I'm currently learning about operating systems and I learned that the stack is located between the kernel and the heap. What confuses me is that in most implementations, since the stack tends to grow downwards while the heap grows to higher memory addresses what is stopping the stack from growing into the heap? If it were possible what would happen if it did grow into the heap? 回答1: This is not necessarily correct: I'm currently learning about operating systems and I learned that the stack is

HeapCreate and HeapAlloc Confuse

戏子无情 提交于 2019-12-13 18:25:31
问题 I am doing a project on dynamic memory management. I run into a confuse about the HeapCreate and HeapAlloc functions. For the HeapCreate() function, we can create a heap and the function will return a HANDLE. We can initialize the size of heap. Let's say winHandle = HeapCreate( 0, 2 * 1024, 0); Then, I can the HeapAlloc function to allocate on this heap. But I am confuse about the size of the heap. I try an example, I call the HeapAlloc( winHandle, 0, 1024) twice on this heap, so the total

2-dimensional array on heap, which version is faster?

亡梦爱人 提交于 2019-12-13 17:01:31
问题 double **array = new double* [X]; for (int i=0; i<X; i++) array[i] = new double [Y]; array[x][y] = n; or double *array = new double [X*Y]; array[x*Y+y] = n; Second version is created faster, but access is in first version faster (e.g. image processing using convolution), isn't it? Or is it all negligible? 回答1: In theory the second version should be faster, because the entire array is allocated contiguouslly, so its more cache-friendly than the first. But in practice, profile it and see what

Is Min Heap Function

流过昼夜 提交于 2019-12-13 14:07:48
问题 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] < L[2*i+1] and _is_min_heap(L, 2*i+1)) and (L[i] < L[2*i+2] and _is_min_heap(L, 2*1+2)) I am not sure what the base case should be and is my recursive calls correct? Also how can you control that the indexes are not eventually out of range? 回答1: You have three different cases for a given

Java: check if array is heap

老子叫甜甜 提交于 2019-12-13 11:30:47
问题 I am trying to implement that checks whether a given array is a heap. public static boolean Heap(int[] A) { for (int i = 1; i <= (A.length - 2) / 2; i++) { if (A[i] < A[2 * i] || A[i] < A[2 * i + 1]) { return false; } } return true; } A = {50, 45, 40, 35, 20, 25, 20}; B = {45, 50, 40, 35, 20, 25, 20}; if (Heap(A)) { System.out.println("Heap"); } else { System.out.println("Not a heap"); } When I call the function for the arrays above, they both return true, while B should have been caught in

Where are static members stored in memory? stack/ heap in C# .Net [duplicate]

旧时模样 提交于 2019-12-13 11:13:20
问题 This question already has answers here : Fields of class, are they stored in the stack or heap? (3 answers) Closed 3 years ago . The earlier post dealt with the value and reference types and their memory allocation. Here I'm trying to understand the memory allocation of static members. I have a simple class which has got both static and non-static integers like one shown below. class Sample { public int nonStaticInt = 0; public static int staticInt = 0; } My question here is, where do static

Build Heap Procedure.

匆匆过客 提交于 2019-12-13 10:25:59
问题 An array of integers of size n can be converted into a heap by adjusting the heaps rooted at each internal node of the complete binary tree starting at the node ⌊(n−1)/2⌋ and doing this adjustment up to the root node (root node is at index 0) in the order ⌊(n−1)/2, ⌊(n−3)/2⌋, ....., 0. ========================================================================== I know, it's a Build Heap procedure and takes O(n) time, but can someone please make me visualise by taking a array with small value of

C++ Dynamic vs Stack Objects and How to Use Them [closed]

对着背影说爱祢 提交于 2019-12-13 09:47:27
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Follow-Up: Please head over to this question where there are actually some useful answers. 回答1: Instead of objects, think of "simpler"