C++ 数据结构学习 ---- 优先级队列

目录

1 头文件

1.1 优先级队列头文件

1.2 完全二叉堆头文件

1.3 左式堆头文件

1.4 Dice头文件

2. 完整代码

3. 运行结果及截图

​编辑


1 头文件

1.1 优先级队列头文件

 //优先级队列PQ模板类
template <typename T> struct PQ {
	virtual void insert(T) = 0; //按照比较器确定的优先级次序插入词条
	virtual T getMax() = 0; //取出优先级最高的词条
	virtual T delMax() = 0; //删除优先级最高的词条
  
};
//判断大小
template <typename T>bool lt(T a, T b) {
	if (a < b) return true;
	else return false;
}

1.2 完全二叉堆头文件

C++ 数据结构学习 ---- 完全二叉堆_孤城寻欢的博客-CSDN博客

1.3 左式堆头文件

C++ 数据结构学习 ---- 左式堆_孤城寻欢的博客-CSDN博客

1.4 Dice头文件

 
#include <ctime>
#include <Stdlib.h>
 
static int dice(int range) { return rand() % range; } //取[0, range)中的随机整数
static int dice(int lo, int hi) { return lo + rand() % (hi - lo); } //取[lo, hi)中的随机整数
static float dice(float range) { return rand() % (1000 * (int)range) / (float)1000.; }
static double dice(double range) { return rand() % (1000 * (int)range) / (double)1000.; }
static char dice() { return (char)(32 + rand() % 96); }

2. 完整代码

#include <iostream>
#include "PQ_ComplHeap.h"
#include "PQ_LeftHeap.h"
//#include "PQ_List.h"
#include "Dice.h"



using Rank = int; //秩
using namespace std;


/******************************************************************************************
 * 针对基于列表、向量以及左式堆实现的优先级队列,做过程统一的测试
 ******************************************************************************************/
            //堆类型、词条类型
template <typename PQ, typename T> void  testHeap(int n) {
    T* A = new T[2 * n / 3]; //创建容量为2*n/3的数组,并
    for (T i = 0; i < 2 * n / 3; i++)
        A[i] = dice((T)3 * n); //在其中随机生成2*n/3个词条
    cout << "随机生成:" << 2 * n / 3 << "个词条!:---->" ;//printf("%d random keys created:\n", 2 * n / 3);
    for (int i = 0; i < 2 * n / 3; i++)
        cout << A[i]<<" ";// print(A[i]);
    cout << endl;// printf("\n");
    PQ heap(A , 2 *n / 3 ); //批量建堆(PQ_ComplHeap实现了Robert Floyd算法)
    delete[] A;
  //cout << heap;// print(heap);
    while (heap.size() < n) { //随机测试
        if (dice(100) < 70) { //70%概率插入新词条
            T e = dice((T)3 * n);//printf("Inserting"); print(e); printf(" ...\n");
            cout << "插入:" << e << "   ";
            heap.insert(e); //printf("Insertion done\n");
            cout << "插入成功!"<<endl;
        }
        else { //30%概率摘除最大词条
            if (!heap.empty()) {
                cout << "删除最大值:" ;//printf("Deleting max ...\n");
                T e = heap.delMax();//printf("Deletion done with"); print(e); printf("\n");
                cout << "删除:" << e << endl;
            }
        }
        //cout << heap;//print(heap);
    }
    while (!heap.empty()) { //清空
        T e = heap.delMax();
        cout << "成功删除:" << e << endl;//printf("Deletion done with"); print(e); printf("\n");
        
        //cout << heap;//print(heap);
    }
}

int main( ) {


    srand((unsigned int)time(NULL));
    int i = rand() % 10;
    //srand( 31415926 ); //固定种子
#if defined(DSA_PQ_LEFTHEAP)
    testHeap<PQ_LeftHeap<int>, int>(i); //词条类型可以在这里任意选择
#elif defined(DSA_PQ_COMPLHEAP)
    testHeap<PQ_ComplHeap<int>, int>(i); //词条类型可以在这里任意选择
#elif defined(DSA_PQ_LIST)
    testHeap<PQ_List<int>, int>(i); //词条类型可以在这里任意选择
#else
    printf("优先级对列的类型还未定义(PQ type not defined yet)\n");
#endif
    testHeap<PQ_ComplHeap<int>, int>(i);//完全二叉堆优先级队列
    cout << endl;
    testHeap<PQ_LeftHeap<int>, int>(i); //左式堆优先级的队列
    system("pause");
    return 0;
}

3. 运行结果及截图

猜你喜欢

转载自blog.csdn.net/qq_58240448/article/details/128115832