Hey all i am trying to write a Min Heap program using that allows insertion and deletion. I have the code for what i believe is insertion, but i am having trouble trying to figure out code to heapify and remove from the heap. If possible could anyone lend me a helping hand with the completion of this program.
#include <iostream>
#include <cstdlib>
using namespace std;
class node{
public:
node *left, *right;
int value;
void insert(int v);
void remove(int v);
void heapify();
};
node *first_node(int v){
node *p;
p = new node;
p -> value = v;
return p;
}
void insert(int);
void heapify();
void node::insert(int v){
node *p;
int value;
if(v > value){
if(right == NULL){
p = new node;
p -> value = v;
right = p;
}
else{
right -> insert(v);
}
}
}
void PreOrderTraversal(node *p, int value);
int main()
{
int choice, num;
while(choice != 0){
cout << "----Menu----" << endl;
cout << "(1) Add to the Heap" << endl;
cout << "-------------------" << endl;
cout << "(2) Remove from the Heap" << endl;
cout << "------------------------" << endl;
cout << "(3) Print the Heap" << endl;
cout << "------------------" << endl;
cout << "(0) Exit the Heap" << endl;
cin >> choice;
if( choice == 1){
cout<< "Enter an integer into the tree: ";
cin >> num;
insert(num);
}
else if (choice == 2){
}
}
system("pause");
return 0;
}
void PreOrderTraversal(node *p, int value){
if(p!= NULL){
cout << value << endl;
cout << left << endl;
cout << right << endl;
}
}
void node::remove(int v){
}
/*void node::heapify(){
int i;
i = 0;
int n, leftchild, rightchild, leftvalue, rightvalue, largest, j;
int data[];
while (i < n){
leftchild = left(i);
leftvalue = data[leftchild];
rightchild= right(i);
rightvalue= data[rightchild];
if (leftvalue > rightvalue){
largest = leftvalue;
j = leftchild;}
else{
largest = rightchild;
j = rightchild; }
if(largest > data[i])
{swap(data[i], data[j]);
i = j;
}
else
{ break; }
}*/