如何从二叉树转移到优先级队列而不重复?

时间:2017-04-27 16:03:55

标签: c++

我正在尝试创建一个程序,该程序从用户输入中获取N个随机节点,并创建一个随机整数,该整数被放入二叉树中,然后复制到优先级队列中。 整数成为每个节点的密钥,另一个整数计算密钥的频率。 当我复制到优先级队列时遇到问题,因为我得到重复,我需要删除它们。第一个输出是dfs。第二个输出应该是 第一个队列,其中频率最高,后面是密钥。第三个输出应该是优先级队列少到更高的密钥。

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

-------------------

0 个答案:

没有答案