The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Need help with C++ min and max heaps.

I've no clue how to build them exactly. Well I kinda do, thanks to code in book. However, I'm not getting the changePriority() thing the teacher recommended. Yes, a heap is a priority queue. The min and max heap should store the same data.

The min and max heaps are supposed to contain the same data. How I'd get it to work if it had a value inserted into a max heap and how I'd get it to update the arrays differently and maintain the pointers correctly.

I have the TreeNodes in the MinHeap corresponding to the TreeNodes in the MaxHeap.

Another thought I'm having is that the way I'm going about this is overkill for implementation and I could possibly do it a simpler way, though I can't think of how.

Note: I do realize that I'm allowed to use vectors and might change it to such, though with my last issue with generics, I'm a bit scared to. However, I'd still need to have two vectors of floats.

Note:: The exam is the 17th. That was a typo. Also, deleteMax should remove the maximum . That also was a typo.

The one below should be for a min heap. I'm assuming I can just change the < signs to > signs to have that method for a maxheap, correct?

Ok, the part where I find the kth_smallest(int value). I'm confused on that. Please help with that part first.

Actually, to be frank, I'm rather a data structure, algorithm, and recursion washout. I can get the regular OO stuff well, or well enough, but this stuff takes about 2 to 5 times longer to figure out. Sometimes I don't even have that time to spend on it.

Re: Need help with C++ min and max heaps.

I've got a more specific question. I was wondering, how do you get a pointer to both refer to a value and also to an address?

What I'd love to just do is simply, since my pointers will refer to each other, have it set the value at that address to null or remove the value at that address. However, it would also need to know the value itself, presumably, though maybe not. If that address were to be changed, what would stop it from suddenly pointing to the wrong value?

Also, I've gotten the idea of making copy constructors for the two different types of heaps. Then I perform remove several times, though I'd have to figure out, in a way that I still have yet to figure out, on the tree that has the root, (Min or Max) closest to the value until I reach that value. Then I remove that from the copy tree and return it (remove actually removes AND returns a value, if you read the assignment specifications.)

But how to destroy the copied tree to free up memory afterward. Not quite sure either. Very new at pointers.

Is there a better way I could go about kth_smallest() then that implementation I suggested above?

Re: Need help with C++ min and max heaps.

As I said earlier, I have an idea how to implement this code, from some stuff the book provided, but I don't know how to use the pointers to change the values when a max is deleted from a min heap. My pointer could, if I knew how to work them correctly, point to the value where that max is at in the min heap. However, I want to go to that address and then then delete it's value and then alter the tree.

It seems that I'd only need to percolate up from that spot. And the book already has a nice method that does it by index.

Re: Need help with C++ min and max heaps.

Ok, fixed all the bugs so far but now am wondering how to go about insert since I have it as a float but also need to make it call the insert on the corresponding Node so I don't have to call two inserts. That's one of the uses of the TreeNode pointer.

Re: Need help with C++ min and max heaps.

I have to turn it in. Once again, another project where nobody replied. Thanks everyone for helping me.

I wasn't trolling, just letting out frustration and being sarcastic in the line above.

I think I didn't do that bad on this one actually. Still, I tried politely and tried an SSCCE even, I specified on some specific questions a few times, and no replies. Very discouraging.

Nice attitude. Here you have yet again done exactly what you've been told not to do, dozens (or more?) times by now. You have a link to asking questions the smart way in your signature, yet you follow none of the rules it lays out. Then you have the audacity to complain about not getting a reply in a time frame that suits your needs?

Also, this is a Java forum. While we might be able to answer questions about other programming languages, our main focus remains Java. Why don't you post on a C++ forum?

But honestly, with the attitude you've shown, both now and in the past, and your apparent inability to understand simple directions from instructors, assignments, and potential helpers, I seriously think you should reconsider whether programming is really for you.