当我尝试通过调用加载优先级队列时,我一直收到此错误 在我的二叉树函数中。如果我取出pq.emplace(node(v,ct ++))并只放{ct ++}它就可以了。我只是想让我的pq加载我的二叉树,所以我可以对它进行排序并打印到屏幕上。
class node
{
public:
node(){left=NULL; right=NULL; ct = 1;}
node (int v){val = v; left=NULL; right=NULL; ct=1;}
node (int Pri=0, int cat=1)
: val(Pri), ct(cat), left(NULL), right(NULL) {}
friend bool operator>(//sorts queue by greatest Priority
const node& x, const node& y) {
return x.ct > y.ct;
}
friend std::ostream&//prints out queue later
operator<<(std::ostream& os, const node& Pri) {
return os <<"my value = "<<Pri.val<<" occured "<<Pri.ct<<" times";
}
std::priority_queue<node, std::vector<node>, std::greater<node> > pq;
void addnode(int v)
{
if(v==val){pq.emplace(node(v, ct++));}///changed for {ct+++}
else if(v<val)
{
if(left==NULL){left=new node(v);}///error here!!
else{left->addnode(v);}
}
else
{
if(right==NULL){right = new node(v);}///error here!!
else{right->addnode(v);}
}
}
void display()
{
if(left!=NULL){left->display();}
std::cout<<"my value = "<<val<<" occured "<<ct<<" times"<<std::endl;
if(right!=NULL){right->display();}
}
void display_Queue()
{
std::cout << "0. size: " << pq.size() << '\n';
std::cout << "Popping out elements from Pqueue..."<<'\n';
while (!pq.empty())
{
std::cout << pq.top() << std::endl;
pq.pop();
}
std::cout << '\n';
}
private:
int val; ///value in that node
int ct; ///ct = count of that value
node * left;
node * right;
};
#include <iostream>
#include <random>
#include <ctime>
#include <queue>
#include <set>
#include <functional>
#include <algorithm>
#include "Q7.h"
using namespace std;
int unsortedRemoveDuplicates(vector<int>& numbers)
{
set<int> seenNums; //log(n) existence check
auto itr = begin(numbers);
while(itr != end(numbers))
{
if(seenNums.find(*itr) != end(seenNums)) //seen? erase it
itr = numbers.erase(itr); //itr now points to next element
else
{
seenNums.insert(*itr);
itr++;
}
}
return seenNums.size();
}
int main()
{
node * root=NULL;
int v, n;
vector<int> first;
vector<int>::iterator fi;
default_random_engine gen(time(NULL));
cout<<"how many values? "; cin>>n;
for(int i=0; i<n; i++)
{ (v=gen()%n);
first.push_back(v);
cout<<"value "<<v<<" generated"<<endl;
if(root==NULL){root = new node(v);}
else{
root->addnode(v);
}
}
unsortedRemoveDuplicates(first);
cout<<"Binary Tree in a depth first manner with Duplicates
removed!"<<endl;
for ( fi = first.begin() ; fi != first.end(); ++fi)
{cout<<"Node "<<*fi<<endl;}
cout<<"-------------------"<<endl;
root->display();
cout<<"-------------------"<<endl;
cout<<"-------------------"<<endl;
root->display_Queue();
cout<<"-------------------"<<endl;
return 0;
}