2

I am trying to create a program that takes N random nodes from user input and creates a random integer that is put into a binary tree and then copied into a priority queue. The integer becomes the key for each node and another integer counts the frequency of the key. I run into issues when I copy into the priority queue because I get duplicates and I need to remove them. I tried to create a set through the node constructor but I get the error above in the .cpp file.

#include <iostream>
#include <random>
#include <ctime>
#include <queue>
#include <set>
#include <functional>
#include <algorithm>
#include<list>
#include "Q7.h"

using namespace std;

int main()
{
node * root=NULL;
node z;
int n,v;

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);

    if(root==NULL){root = node(set(v));}///This is where I get the error!!
    else{
      root->addnode(v);
       }
}

z.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_Queue1();
cout<<"-------------------"<<endl;

return 0;
 }





my .h file

class node
{
public:
node(){left=NULL; right=NULL; ct = 1;}
node set(int v) {val = v; left=NULL; right=NULL; ct=1;}
node (int Pri, int cat)
: val(Pri), ct(cat) {}

friend bool operator<(//sorts queue by lowest Priority
const  node& x, const  node& y)  {
    return x.val < y.val;
}

friend bool operator>(//sorts queue by greatest Priority
const node& x, const node& y) {
    return x.ct > y.ct;
}

friend ostream&//prints out queue later
operator<<(ostream& os, const node& Pri) {
return os  <<"my value = "<<Pri.val<<" occured "<<Pri.ct<<" times";
}

int unsortedRemoveDuplicates(vector<int>& numbers)
{
node 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();
}
priority_queue<node, vector<node>, greater<node> > pq;
priority_queue<node, vector<node>, less<node> > pq1;

void addnode(int v)
{
   if(v==val){ct++;}
   pq.emplace(node (set (v)));///No error here for set with constructor why??
   pq.emplace(node (set (v)));
    if(v<val)
        {
        if(left==NULL){left=new node(set(v));
            }
        else{left->addnode(v);
            }
        }
    else
        {
        if(right==NULL){right = new node (set(v));
            }
        else{right->addnode(v);
            }
        }
}

int display()
    {
        if(left!=NULL){left->display();}

        cout<<"frequency  "<<ct<<" value"<<val<<endl;

        if(right!=NULL){right->display();}
    }

void display_Queue()
    {
        cout << "0. size: " << pq.size() << '\n';
        cout << "Popping out elements from Pqueue..."<<'\n';
        while (!pq.empty())
        {
        cout << pq.top() <<  endl;
        pq.pop();
        }
        cout << '\n';
    }
        void display_Queue1()
    {
        cout << "0. size: " << pq1.size() << '\n';
        cout << "Popping out elements from Pqueue..."<<'\n';
        while (!pq1.empty())
        {
        cout << pq1.top() <<  endl;
        pq1.pop();
        }
        cout << '\n';
    }

private:
int val;      ///value in that node
int ct;
///ct = count of that value
node * left;
node * right;
};
Jimbo
  • 31
  • 1
  • 5

1 Answers1

4

Congratulations, with this line:

root = node(set(v));

You have discovered why people here often say to avoid using using namespace std;. This is being interpreted as:

root = static_cast<node>(std::set(v));

Instead of what you want, which might be:

root = new node();
root->set(v);

First, note that we need to use new as we are creating a new node, not trying to cast a node to a node, which would have also given another compiler error about trying to assign a value to a pointer.

Next, note that you don't get the error in the header file as there is no using namespace std; there, and since it is in a member function, the line:

void node::addnode(int v)
{
    //...
    pq.emplace(node (set (v)));///No error here for set with constructor why??
    //...
}

Is interpreted as:

pq.emplace(static_cast<node>(this->set(v)));

However, is this what you really want to do?


Furthermore, I would change the constructors to be:

public:
    node (int Pri = 0, int cat = 1)
        : val(Pri), ct(cat), left(NULL), right(NULL) {}
    // DELETED node (int Pri, int cat)

Thus you can do:

root = new node(v);

And it will work as I think you expect it to.

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • Thanks for your suggestion I had no idea about the issue using namespace in that instance. I was able to get the code to work but I ended up with the same result as before which is that I get duplicates from my pq and I want to remove all the lower frequency duplicates. if node 8 gets visited 4 times I get a print out of 8 4, 8 3, 8 2, 8 1. is their any way to remove the duplicates from the priority queue? – Jimbo Apr 27 '17 at 01:56
  • May I suggest posting that as a new question? I'd need to see the modifications you've made to the code, and please add a fuller description of the expected and actual results, as it will encourage others to answer if the question is clear. – Ken Y-N Apr 27 '17 at 03:45
  • Okay I reposted it as a new question. – Jimbo Apr 27 '17 at 16:34
  • http://stackoverflow.com/questions/43662724/how-do-i-transfer-from-a-binary-tree-to-a-priority-queue-without-duplicates – Jimbo Apr 27 '17 at 16:35