I have made a Priority Queue implemented as a Binary Search Tree. I would appreciate comments related to optimization and proper functionality, but any comments/critiques are welcome.
PQ.h:
#ifndef PQ_H
#define PQ_H
#include <iostream>
struct Node
{
int value;
Node * left;
Node * right;
Node(int value)
: value(value), left(NULL), right(NULL)
{
}
};
class PQ
{
private:
Node * root;
void Push(int value, Node * node);
int Pop(Node * node, Node * parent);
void DeletePQ(Node * node);
public:
PQ();
~PQ();
void Push(int value);
int Pop();
void DeletePQ();
bool IsEmpty();
};
#endif
PQ.C:
#include "PQ.h"
PQ::PQ()
{
root = NULL;
}
PQ::~PQ()
{
DeletePQ();
}
void PQ::DeletePQ()
{
DeletePQ(root);
}
void PQ::DeletePQ(Node * node)
{
if(node != NULL)
{
DeletePQ(node->left);
DeletePQ(node->right);
delete node;
}
}
void PQ::Push(int value)
{
if(root != NULL)
{
Push(value, root);
}
else
{
root = new Node(value);
}
}
void PQ::Push(int value, Node * node)
{
if(value < node->value)
{
if(node->left != NULL)
{
Push(value, node->left);
}
else
{
node->left = new Node(value);
}
}
else
{
if(node->right != NULL)
{
Push(value, node->right);
}
else
{
node->right = new Node(value);
}
}
}
int PQ::Pop()
{
int value;
if(root != NULL)
{
if(root->right != NULL)
{
value = Pop(root->right, root);
}
else
{
value = root->value;
if(root->left != NULL)
{
Node * temp = root;
root = root->left;
delete temp;
}
else
{
delete root;
root = NULL;
}
}
}
else
{
value = -1;
}
return value;
}
int PQ::Pop(Node * node, Node * parent)
{
int value;
if(node->right != NULL)
{
value = Pop(node->right, node);
}
else
{
value = node->value;
if(node->left != NULL)
{
parent->right = node->left;
}
else
{
parent->right = NULL;
}
delete node;
}
return value;
}
bool PQ::IsEmpty()
{
bool isEmpty;
if(root == NULL)
{
isEmpty = true;
}
else
{
isEmpty = false;
}
return isEmpty;
}