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 & 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& e) {
cerr << "Memory allocation error: " << e.what() << endl;
return false;
}
while (!(index & mask)) index <<= 1;
index <<= 1;
parent = NULL;
temp = *heap;
while (temp){
if (data > temp->data){
tempData = temp->data;
temp->data = data;
data = tempData;
}
parent = temp;
if (!(index & mask)) temp = temp->leftChild;
else temp = temp->rightChild;
if (temp) index <<= 1;
}
newNode->leftChild = NULL;
newNode->rightChild = NULL;
newNode->data = data;
if(!parent){
*heap = newNode;
return true;
}
if (!(index & mask)) parent->leftChild = newNode;
else parent->rightChild = newNode;
return true;
}
This is my main()
:
#include <iostream>
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<13; i++) insertHeap(&heap, arr[i], i+1);
return 0;
}