0

I have a midterm exam next week and am having difficulty drawing out a binary heap. The invariant for a minimum binary heap is: the value of the item stored at a parent node is always less than (greater than) the values of the items stored at its child nodes. The part that I don't understand is when I am inserting values into the heap, how do I know whether to go left or right? I'd really like to see step by step solutions, because I just don't understand how I would know whether to go left or right.

Say I have the values: 5, 8, 13, 15, 1, 2, 12, 4 it would start like

  5 then I insert 8
 / \ 
8? 13? is this going in the right direction?

I know for binary search trees the invariant is left< parent < right, but I am just really confused on how to determine whether to go left or right.

thatsnifty
  • 191
  • 4
  • 14
  • This is covered in the heap operations section of the Wikipedia binary heap page. New items are always inserted at the bottom of the heap and then trickled-up. See https://en.m.wikipedia.org/wiki/Binary_heap#Heap_operations for details – Austin Apr 18 '16 at 02:08
  • @AustinD this is very helpful, however i am still confused on how to know whether to go right or left. – thatsnifty Apr 18 '16 at 02:19

1 Answers1

1

You choose the direction to ensure the underlying tree is a complete binary tree:

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible

Let's apply the trickle-up method to an example. Suppose you want to add a new element e to a heap. If the heap looks like this:

     5                                    5
   /   \                                /   \    
  6     9    add as right-child-->     6     9     then trickle-up
 / \   / \     to complete the        / \   / \
7                    tree            7   e

Then you add the new element as the right-child of the 6 and trickle-up (i.e. repeatedly swap the new element with its parent until the heap invariant is restored).

On the other hand, if the heap looks like this:

     5                                   5
   /   \                               /   \    
  6     9   add as left-child -->     6     9     then trickle-up
 / \   / \   to complete the         / \   / \
7  10              tree             7  10 e

Then you add the newest element as the left-child of the 9 and trickle up.

As for your example, the add sequence is 5, 8, 13, 15, 1, 2, 12, 4. The following code snippet shows the insert/trickle-up step-by-step:

    Add 5:

     5

    No change due to trickle, So add 8:
     
       5
      /
     8
     
    No change due to trickle-up. So, add 13:

          5
      / \
     8  13
     
    No change due to trickle-up. So, add 15:
         5
        / \
       8  13
      / 
     15

    No change due to trickle-up. So, add 1:
     
         5
        / \
       8  13
      / \
     15  1
     
    Then trickle-up:
     
         1
        / \
       5  13
      / \
     15  8

    Next, add 2:
     
           1
        /     \
       5      13
      / \    /
     15  8  2
     
    Then trickle-up
     
           1
        /     \
       5       2
      / \     /
     15  8   13
     
    Next, add 12:

           1
        /     \
       5       2
      / \     / \
     15  8   13 12
     
    No change due to trickle-up. So, add 4:

                1
          /     \
         5       2
        / \     / \
       15  8   13 12
      /
     4
     
    Then trickle-up:
     
             1
          /     \
         4       2
        / \     / \
       5  8   13 12
      /
     15
Austin
  • 8,018
  • 2
  • 31
  • 37
  • ahhh okay! The code snippet really makes sense to me. I edited my post with what my answer was for my example, and I know what I am doing wrong now. I need to add both childs to the node before I add again. – thatsnifty Apr 18 '16 at 02:44
  • No no... add one child, then trickle-up. Then add the next and trickle-up. And so forth. You need to handle each new element the same way: 1. insert into tree to complete the tree, then 2. trickle-up to restore the heap invariant. – Austin Apr 18 '16 at 02:46
  • yeah, I understand where I was going wrong now, thank you! I will delete that part of my post. – thatsnifty Apr 18 '16 at 02:47
  • so the example in the code snippet, does it satisfy the heap property, as well as is a complete tree? – thatsnifty Apr 18 '16 at 02:50
  • Yes--after each trickle-up operation, the heap satisfies the min-heap property (since the smallest element is at the head of the heap) and is a complete tree. – Austin Apr 18 '16 at 03:28