Skip to main content

Data Structures: Removing from a Heap and Heapsort

Think about this MaxHeap:
0
1
2
3
4
5
6
23
12
19
7
10
18
14

Remember, children are found with this formula:
Left child: 2i + 1
Right child: 2i + 2

Parents are found with this formula:
Parent: (i - 1) / 2

If we have a maxheap, the children are smaller than the parents.  If we have a minheap, the children are larger than the parents.  This is a maxheap, so all parents are larger than their children.

To add to a maxheap, we put the new value at the next available position in the array then swap up.  In the file for your assignment, we call this process reheapUp.

If we add the value 20 to the maxheap above, we get this:
0
1
2
3
4
5
6
7
23
12
19
7
10
18
14
20

We swap 20 (index 7) with its parent, if the parent is smaller.  The parent value is 7 (index 3) so we swap up.
0
1
2
3
4
5
6
7
23
12
19
20
10
18
14
7

We swap 20 (index 3) with its parent, if the parent is smaller.  The parent value is 12 (index 1) so we swap up.
0
1
2
3
4
5
6
7
23
20
19
12
10
18
14
7

We swap 20 (index 1) with its parent, if the parent is smaller.  Here it’s not so we’re done.

To remove from the maxheap, we could just remove the top element and swap elements upward.  However, this would leave a gap.  In the heap at the top of the page, we’d take off 23 (index 0), put 19 into the empty spot (into index 0 from index 2), then put 18 into the empty spot (into index 2 from index 5).  What do we put into index 5?  We can’t just move an adjacent value into that spot because it might not result in a valid maxheap.

We’ll prevent this problem by swapping the first & last elements in our maxheap.

To remove 23, we swap index 0 and 7.  We also decrement “nextPosition” so that while 23 is still in our array, it isn’t part of our maxheap.
0
1
2
3
4
5
6
7
7
20
19
12
10
18
14
23


This is not a valid heap (because 7 is smaller than both of its children: 20 and 19), so as long as it’s not valid, we swap the element with the larger of its two children.  Here we swap 7 and 20:
0
1
2
3
4
5
6
7
20
7
19
12
10
18
14
23

It’s still not valid (7 < 12 and 7 < 10) so we swap 7 and 12:
0
1
2
3
4
5
6
7
20
12
19
7
10
18
14
23

Now 7 is at index 3; it doesn’t have any children.  This process of moving an invalid top value from the top to closer to the bottom is handled in the code in the reheapDown method.

Notice now that 23 - the largest thing that was in our maxheap - is now in the last index.  If we were to remove 20, we’d swap 20 (index 0) with 14 (index 6), then swap 14 (index 0) with 19 (index 2), then swap 14 (index 2) with 18 (index 5).  Our new maxheap would look like this:
0
1
2
3
4
5
6
7
19
12
18
7
10
14
20
23

Notice now that the end of our array seems to be sorted.

This is the way the heapsort algorithm works - it’s basically just removing a bunch of things from our maxheap.

So, to complete a heapsort, first we create a maxheap from our array:

MaxHeap heap = new MaxHeap(array);

Then, all we have to do is remove everything from it.  The remove() method takes care of all of the sorting and your original array should now be sorted.











Comments

Popular posts from this blog

The Gobbler from Arby's

Stop.  Stop what you're doing and go to Arby's. Right. Now.  Have them make you a Gobbler .  This is not something you'll regret. Go. Eat this thing. Look at that bacon. Go. Arby's has a new sandwich.  It's called "The Gobbler" and as far as I can tell it's two things: a vehicle for their new deep fried turkey, and an attempt at a Thanksgiving themed sandwich.  It's also a third thing: magically delicious. move over Lucky, there's a new holiday mascot on the block Unwrapping: this actually looks like a sandwich.  It looks appetizing.  It looks like something I want to eat.  It doesn't look like the promo photo above, but it doesn't look like someone was flailing around and accidentally smashed up a sandwich, either. sexy Instagram caption goes here First bite: Wow.  I mean, "WOW."  Holy h*ck this is good.  The turkey has a really bold, meaty flavor.  It tastes a lot like turkey sliced fresh from your...

Get Go Sandwich Standoff: "The General" vs "The Rogie Hoagie"

Get Go has been KILLING it lately with crazy sandwiches that are great for advertising on the radio but I've been wondering if they're actually great for eating. The new one I've been hearing about is "The General" which is like Chinese take-out on a sesame sub roll.  I hear ads for it every morning on my commute, and I see a giant billboard for it too.  It's basically chicken tenders with General Tso's sauce and egg rolls on a sesame bun.  I'm guessing they were inspired by Primanti's and decided to try to apply it to a different cuisine (I'm looking forward to The Russian Borscht sub which I'm sure is planned for later this summer). I ventured out to my local GetGo to try one of these out, only to be greeted by "The Rogie Hoagie" on the screen in addition to "The General."  What a great surprise (and additional gastronomic challenge)!  I decided to try them both and report back.  "The General" only comes...

Sheetz Sandwich Standoff: El Gringo vs Twisted Swiss

My wife left me alone for dinner tonight so I decided to check out the latest GetGo offerings... but to my great chagrin, they have no promotional subs. My travels led me to the local Sheetz, where I'd be able to keep eating the best gas station sandwiches around. To keep tradition alive, I picked the two most outrageous "Burgerz" on the menu: El Gringo and Twisted Swiss. The ingredient list is promising: Twisted Swiss is the burger with topped with swiss cheese, cole slaw, pickles, bacon, and whatever "Boom Boom Sauce" is on a pretzel bun.  El Gringo is the burger topped with pepper jack cheese, chili, Doritos, and BBQ sauce on a regular old bun. I unwrapped them both and stood back to admire the majesty before me. They're not pretty, but they do look a lot better out of the wrapper than many fast food burgers I've eaten. Twisted Swiss I expected this sandwich to be an awful mess.  It just seemed like a bunch ...