0

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. The first output is dfs. The second output should be the first queue with highest frequency first and key after. The third output should be key done by priority queue less to greater.

Q7.h

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=0, int cat=1)
    : ct(cat), val(Pri),left(NULL), right(NULL) {}


    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";
    }

    priority_queue<node, vector<node>, greater<node> > pq;
    priority_queue<node, vector<node>, less<node> > pq1;


    void addnode(int v)
    {

        if(v==val){ct++;}
        pq1.emplace(node(v));

        if(v<val)
            {
                if(left==NULL)
                {left=new node(v);
                pq.emplace(left->set(v));

                }
                else{left->addnode(v);
                    }
            }

        else
        {
                if(right==NULL)
                {right = new node(v);
                pq.emplace(right->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;

};

#include <iostream>
#include <random>
#include <ctime>
#include <queue>
#include <set>
#include <functional>
#include <algorithm>
#include<list>
#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 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 = 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_Queue1();
    cout<<"-------------------"<<endl;

return 0;


}



OUtPUt:

how many values? 14
Binary Tree in a depth first manner with Duplicates removed!
Node 3
Node 9
Node 1
Node 13
Node 2
Node 0
Node 7
Node 12
Node 8
Node 4
-------------------
frequency  1 value0
frequency  1 value1
frequency  1 value2
frequency  1 value3
frequency  1 value4
frequency  1 value7
frequency  1 value8
frequency  3 value9
frequency  2 value9
frequency  1 value9
frequency  1 value12
frequency  3 value13
frequency  2 value13
frequency  1 value13
-------------------
-------------------
0. size: 13
Popping out elements from Pqueue...
my value = 13 occured 1 times
my value = 13 occured 1 times
my value = 13 occured 1 times
my value = 12 occured 1 times
my value = 9 occured 1 times
my value = 9 occured 1 times
my value = 9 occured 1 times
my value = 8 occured 1 times
my value = 7 occured 1 times
my value = 4 occured 1 times
my value = 2 occured 1 times
my value = 1 occured 1 times
my value = 0 occured 1 times

-------------------
Jimbo
  • 31
  • 1
  • 5
  • Does this compile? My compiler had an issue with the functions with no return statement. There are additional warnings, which you may want to look into. – Kenny Ostrom Apr 27 '17 at 18:50
  • it complied for me and the output is at the bottom. what was the compiler error? – Jimbo Apr 27 '17 at 19:09
  • node::set and node::display have return types, but do not have return statements. – Kenny Ostrom Apr 27 '17 at 19:11
  • I notice that every node has two priority queues. Are they supposed to have a list of all the children below this node? It does seem a little redundant with the binary tree itself. – Kenny Ostrom Apr 27 '17 at 19:13
  • using namespace std; should be added to the .h file – Jimbo Apr 27 '17 at 19:19
  • one priority queue should be for the second output which should be from greatest frequency to lowest without any duplicate nodes. The third output should be for a priority queue by nodes first and then by frequency but from lowest node to highest – Jimbo Apr 27 '17 at 19:23
  • I made display's return type void. I added "return *this;" to node::set. I just removed node::node() entirely. I run and get a similar output. Now ... what is the output you actually wanted? I recommend you ditch the random, provide a brace initializer for "first" with duplicates. Then post the actual AND expected outputs. – Kenny Ostrom Apr 27 '17 at 19:24
  • Ah, so you intended there to be exactly two priority queues, and they are supposed to have summary data for the entire tree. What you actually have is two priority queues at every node. – Kenny Ostrom Apr 27 '17 at 19:25
  • I am struggling to find a way to remove the duplicates. I can make the second and third output from lesser to greater but the duplicates nodes is what I cannot seem to find a way to get rid of. – Jimbo Apr 27 '17 at 19:26
  • yes that's right summary output for the tree, one pqueue by frequency order from highest to lowest and the other by node by lowest node to highest number node with no duplicates for either – Jimbo Apr 27 '17 at 19:30
  • Do you need the priority queue to be maintained and up-to-date at all times, or can you create it only for the display function? – Kenny Ostrom Apr 27 '17 at 19:32
  • I just need the output to the screen will be enough. – Jimbo Apr 27 '17 at 19:34
  • just need it for display only. – Jimbo Apr 27 '17 at 19:41
  • Notice that node::operator< sorts by value, not priority. – Kenny Ostrom Apr 27 '17 at 19:44
  • I know I have moved value and priority back and forth but if I could just get the priority queue to remove the duplicate nodes and just show the values the tree would display I could get the rest as It needs to be. – Jimbo Apr 27 '17 at 19:53
  • bug -- node::addnode adds all values to their own right. – Kenny Ostrom Apr 27 '17 at 19:56
  • So, after you fix the bugs, you can ditch unsortedRemoveDuplicates entirely. You must have duplicates in order to have frequency data. Next write an iterator so you can traverse the tree http://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators Now the print frequency function creates a temporary priority queue, and just std::copy into that priority queue, and just pop and print. – Kenny Ostrom Apr 27 '17 at 20:04
  • Although, you can write your own code to traverse the tree, instead of implementing an actual std style iterator. – Kenny Ostrom Apr 27 '17 at 20:05
  • so does that mean its extending the whole tree to the right hand side instead of using left and right? – Jimbo Apr 27 '17 at 20:05
  • no ... look at node::addnode. What happens if v == val? – Kenny Ostrom Apr 27 '17 at 20:07
  • I agree that's been my issue all along is that if(v==val){ct++;} will output the frequency for the root but I could never get it to output the frequency correctly for the priority queue. – Jimbo Apr 27 '17 at 20:13
  • but the tree is also wrong because ... set a breakpoint there and see what it does after ct++ – Kenny Ostrom Apr 27 '17 at 20:14
  • I finally found the bug you were right – Jimbo Apr 27 '17 at 22:57

0 Answers0