Heap implementation using pointer I know heaps are commonly implemented by an array. But, I wanted to share my pointer implementation and have your feedback(s) :)

General idea:


I convert the index to its binary value and then trace the heap (0 = leftChild &amp; 1 = rightChild)] Note: The first 1 is for entering the root.
I do the push at the same time.


Example: for the 12th element, 12 = 1100. The first 1 is for entering the root. Now I trace the heap by following 100.

    struct Node{
         Node *leftChild;
         Node *rightChild;
        int data;
    };

    bool insertHeap(Node **heap, int data, short index){
        Node *newNode, *temp, *parent;
        int tempData;
        short mask = 0x80;

    try{
        newNode = new Node;
    }
    catch (bad_alloc&amp; e) {
        cerr &lt;&lt; "Memory allocation error: " &lt;&lt; e.what() &lt;&lt; endl;
        return false;
    }

    while (!(index &amp; mask)) index &lt;&lt;= 1;

    index &lt;&lt;= 1;
    parent = NULL;
    temp = *heap;

    while (temp){
        if (data &gt; temp-&gt;data){
            tempData = temp-&gt;data;
            temp-&gt;data = data;
            data = tempData;
        }       
        parent = temp;

        if (!(index &amp; mask)) temp = temp-&gt;leftChild; 
        else temp = temp-&gt;rightChild;

        if (temp) index &lt;&lt;= 1;
    }

    newNode-&gt;leftChild = NULL;
    newNode-&gt;rightChild = NULL;
    newNode-&gt;data = data;

    if(!parent){
        *heap = newNode;
        return true;
    }

    if (!(index &amp; mask)) parent-&gt;leftChild = newNode;
    else parent-&gt;rightChild = newNode;

    return true;
}


This is my main():

#include &lt;iostream&gt;
using namespace std;

int main (){
    Node *heap = NULL;
    int arr[13] = {3,6,8,1,5,9,4,2,0,7,11,14,13}; 

    for (int i=0; i&lt;13; i++) insertHeap(&amp;heap, arr[i], i+1);   
    return 0;
}
You could also use a map, associative array, or dictionary to implement a heap (like the following):

class Heap(dict):

    "Heap() -&gt; Heap instance"

    def retrieve(self, address):
        "Get the virtual value stored at the address."
        return self.get(address, 0)

    def store(self, value, address):
        "Set the virtual value meant for the address."
        if value:
            self[address] = value
        else:
            self.pop(address, None)