# 21.2 Heaps

{% embed url="<https://www.youtube.com/watch?v=4WHrtiKTkA4>" %}
Introducing the Heap
{% endembed %}

We previously saw that our known data structures with the best runtime for our PQ operations was the *binary search tree*. Modifying its structure and the constraints, we can further improve the runtime and efficiency of these operations.

We will define our binary min-heap as being **complete** and obeying **min-heap** property:

* Min-heap: Every node is less than or equal to both of its children
* Complete: Missing items only at the bottom level (if any), all nodes are as far left as possible.

![](https://joshhug.gitbooks.io/hug61b/content/assets/heap-13.2.1.png)

As we can see in the figures above, the green colored heaps are valid and the red ones aren't. The last two aren't because they violate at least one of the properties that we defined above.

Now let's consider how this structure lends itself to the abstract data type we described in the previous chapter. We will do this through analyzing our desired operations.

**Exercise 13.2.1.** Determine how each method of our Priority Queue interface will be implemented given this heap structure. Don't write actual code, just pseudocode!

### Heap Operations <a href="#heap-operations" id="heap-operations"></a>

{% embed url="<https://www.youtube.com/watch?v=NNS9srVjnOw>" %}
Heap Operations
{% endembed %}

The three methods we care about for the PriorityQueue ADT are `add`, `getSmallest`, and `removeSmallest`. We will start by conceptually describing how these methods can be implemented given our given schema of a heap.

* `add`: Add to the end of heap temporarily. Swim up the hierarchy to the proper place.
  * Swimming involves swapping nodes if child < parent
* `getSmallest`: Return the root of the heap (This is guaranteed to be the minimum by our *min-heap* property
* `removeSmallest`: Swap the last item in the heap into the root. Sink down the hierarchy to the proper place.
  * Sinking involves swapping nodes if parent > child. Swap with the smallest child to preserve *min-heap* property.

Great! We have determined how we will approach the operations specified by the PriorityQueue interface in an efficient way. But how do we actually code this?

**Exercise 13.2.2.** Give the runtime for each of the methods specified above. Worst cases and best cases.

### Tree Representation <a href="#tree-representation" id="tree-representation"></a>

{% embed url="<https://www.youtube.com/watch?v=yCrjolcI5Wg>" %}
Tree representation
{% endembed %}

There are many approaches we can take to representing trees.

#### Approach 1a, 1b, and 1c <a href="#approach-1a-1b-and-1c" id="approach-1a-1b-and-1c"></a>

Let us consider the most intuitive and previously used representation for trees. We will create mappings between nodes and their children. There are several ways to do this which we will explore right now.

* In approach **Tree1A**, we consider creating pointers to our children and storing the value inside of the node object. These are hardwired links that give us fixed-width nodes. We can observe the code:

```java
public class Tree1A<Key> {
  Key k;
  Tree1A left;
  Tree1A middle;
  Tree1A right;
  ...
}
```

The visualization of this type of structure is shown below.

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FGPQICfSMvLaxRltFOxMF%2Fimage.png?alt=media&#x26;token=e3f459a8-363c-4601-920d-819d966e7115" alt=""><figcaption><p>1a: Fixed-Width Nodes (BSTMap used this approach)</p></figcaption></figure>

* Alternatively, in **Tree1B**, we explore the use of arrays as representing the mapping between children and nodes. This would give us variable-width nodes, but also awkward traversals and performance will be worse.

```java
public class Tree1B<Key> {
  Key k;
  Tree1B[] children;
  ...

```

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2Fl1NhbH8IuDKlHe9ji7rV%2Fimage.png?alt=media&#x26;token=43d3d27e-3946-4924-95e9-20af802a8494" alt=""><figcaption><p>1b: Variable-Width Nodes</p></figcaption></figure>

* Lastly, we can use the approach for **Tree1C**. This will be slightly different from the usual approaches that we've seen. Instead of only representing a node's children, we say that nodes can also maintain a reference to their siblings.

```java
public class Tree1C<Key> {
  Key k;
  Tree1C favoredChild;
  Tree1C sibling;
  ...
}
```

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FWSjeCjdRvb3jzTlV6wz2%2Fimage.png?alt=media&#x26;token=fda06ad4-4535-47bd-9c7f-b2d23d521609" alt=""><figcaption><p>1c: Sibling Tree</p></figcaption></figure>

In all of these approaches, we store explicit references to who is below us. These explicit references take the form of pointers to the actual Tree objects that are our children. Let's think of more exotic approaches that don't store explicit references to children.

#### Approach 2 <a href="#approach-2" id="approach-2"></a>

Recall the Disjoint Sets ADT. The way that we represented this Weighted Quick Union structure was through arrays. For representing a tree, we can store the *keys* array as well as a *parents* array. The keys array represent which index maps to which key, and the parents array represents which key is a child of another key.

```java
public class Tree2<Key> {
  Key[] keys;
  int[] parents;
  ...
}
```

Take some time to ensure that the tree on the left corresponds to the representation in the arrays on the right.

It's time to make a very important observation! Based on the structure of the tree and the relationship between the array representations and the diagram of the tree, we can see:

1. The tree is **complete**. This is a property we have defined earlier.
2. The parents array has a sort of redundant pattern where elements are just doubled.
3. Reading the level-order of the tree, we see that it matches exactly the order of the keys in the *keys* array.

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2Fi99NVIANpmuo1ZNdpp0Z%2Fimage.png?alt=media&#x26;token=ddcc934c-d7e7-436b-9053-9a60d8e9cf12" alt=""><figcaption><p>Approach 2</p></figcaption></figure>

What does this all mean? We know the parents array is redundant so we can ignore it and we know that a tree can be represented by level order in an array.

#### Approach 3 <a href="#approach-3" id="approach-3"></a>

In this approach, we assume that our tree is complete. This is to ensure that there are no "gaps" inside of our array representation. Thus, we will take this complex 2D structure of the tree and flatten it into an array.

```java
public class TreeC<Key> {
  Key[] keys;
  ...
}
```

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FgBdGBgTxIlozZMklfxkm%2Fimage.png?alt=media&#x26;token=c3728dc4-5dea-4111-aa35-0591f331daf6" alt=""><figcaption></figcaption></figure>

#### Swim <a href="#swim" id="swim"></a>

Given this implementation, we define the following code for the "swim" described in the Heap Operations section.

```java
public void swim(int k) {
    if (keys[parent(k)] ≻ keys[k]) {
       swap(k, parent(k));
       swim(parent(k));              
    }
}
```

What does the parent method do? It returns the parent of the given k using the representation in Approach 3.

**Exercise 13.2.3.** Write the parent method. For extra challenge, try to write the methods for finding the left child and right child of a given item.
