The initialization doesn't determine how the priority queue operates. If you want it to sort a particular way, you have two options.
The first option is to define the <
operator on your Node
objects to compare them the way you want.
struct Node {
string data;
int weight;
Node *left, *right;
bool operator<(const Node& n) const {
return weight < n.weight;
// or "weight > n.weight" if you want the smallest weight at the top
}
};
std::priority_queue<Node> myQueue;
The second option is to define a custom comparator type and specify it as a template argument:
struct NodeComp {
bool operator()(const Node& n1, const Node& n2) const {
return n1.weight < n2.weight;
// or "n1.weight > n2.weight" if you want the smallest weight at the top
}
};
std::priority_queue<Node, std::vector<Node>, NodeComp> myQueue;