《数据结构、算法与应用C++语言描述》- 最小输者树模板的C++实现

输者树

完整可编译运行代码见:Github::Data-Structures-Algorithms-and-Applications/_31loserTree

输者树:每一个内部节点所记录的都是比赛的输者,晋级的节点记录在边上。本文中,赢者是分数较低的那个,输者是分数高的那个。教材的举例是这样的。

在这里插入图片描述

更好理解的解释是,参考地址。

  • a与b比赛,输者是b,赢者是a,将b放到内部节点,将a放到边上
  • c与d比赛,输者是d,赢者是c,将d放到内部节点,将c放到边上
  • e与f比赛,输者是e,赢者是f,将e放到内部节点,将f放到边上
  • g与h比赛,输者是h,赢者是g,将h放到内部节点,将g放到边上
  • a与c比赛,输者是c,赢者是a,将c放到内部节点,将a放到边上
  • f与g比赛,输者是g,赢者是f,将g放到内部节点,将f放到边上
  • a与f比赛,输者是a,赢者是f,将a放到内部节点,将f放到边上
  • 将最终赢者放到数组的第0个元素

在loserTree的模板实现中,可以使用一个数组来存储输者、另一个数组存储赢者。

在这里插入图片描述

输者树比赢者树的优势

当改变的元素是上一场比赛的最终赢家的话,内部节点存储了所有该赢家的输者。在重新组织比赛时,只需要与父亲节点进行比较,而不需要获取到父亲节点的另外一个节点,然后与其比较,可以减少访存的时间。

输者树的实现

main.cpp

/*
Project name :			_30winnerTree
Last modified Date:		2023年12月19日16点48分
Last Version:			V1.0
Descriptions:			最小输者树——main函数
*/
#include "MinimumLoserTree.h"
int main(){MinimumLoserTreeTest();return 0;
}

MinimumLoserTree.h

/*
Project name :			_30winnerTree
Last modified Date:		2023年12月19日16点48分
Last Version:			V1.0
Descriptions:			最小输者树——模板类
*/#ifndef _31LOSERTREE_MINIMUMLOSERTREE_H
#define _31LOSERTREE_MINIMUMLOSERTREE_H
#include<iostream>
#include "loserTree.h"
#include "myExceptions.h"
using namespace std;void MinimumLoserTreeTest();template<class T>
class MinimumLoserTree : public loserTree<T> {
public:/*构造函数*/explicit MinimumLoserTree(T *thePlayer = nullptr, int theNumberOfPlayers = 0) {tree = nullptr;advance = nullptr;initialize(thePlayer, theNumberOfPlayers);}/*析构函数*/~MinimumLoserTree() {delete[] tree;delete[] advance;}void initialize(T *thePlayer, int theNumberOfPlayers);//初始化[[nodiscard]] int getTheWinner() const { return tree[0]; };//输出当前的赢者void rePlay(int thePlayer, T newvalue);//重构void output() const;
private:int numberOfPlayers{};int *tree;// 记录内部结点,tree[0]是最终的赢者下标,不使用二叉树结点,因为父子关系都是通过计算得出int *advance;// 记录比赛晋级的成员T *player;//参与比赛的元素int lowExt{};//最底层外部结点的个数,2*(n-s)int offset{};//2*s-1void play(int, int, int);int winner(int x, int y) { return player[x] <= player[y] ? x : y; };//返回更小的元素下标int loser(int x, int y) { return player[x] <= player[y] ? y : x; };//返回更大的元素下标
};template<class T>
void MinimumLoserTree<T>::initialize(T *thePlayer, int theNumberOfPlayers) {int n = theNumberOfPlayers;if (n < 2) {throw illegalParameterValue("must have at least 2 players");}player = thePlayer;numberOfPlayers = n;// 删除原来初始化的内存空间,初始化新的内存空间delete[] tree;delete[] advance;tree = new int[n + 1];advance = new int[n + 1];// 计算sint s;for (s = 1; 2 * s <= n - 1; s += s);//s=2^log(n-1)-1(常数优化速度更快),s是最底层最左端的内部结点lowExt = 2 * (n - s);offset = 2 * s - 1;for (int i = 2; i <= lowExt; i += 2)//最下面一层开始比赛play((i + offset) / 2, i - 1, i);//父结点计算公式第一条int temp = 0;if (n % 2 == 1) {//如果有奇数个结点,一定会存在特殊情况,需要内部节点和外部节点的比赛play(n / 2, advance[n - 1], lowExt + 1);temp = lowExt + 3;} else temp = lowExt + 2;//偶数个结点,直接处理次下层for (int i = temp; i <= n; i += 2)//经过这个循环,所有的外部结点都处理完毕play((i - lowExt + n - 1) / 2, i - 1, i);tree[0] = advance[1];//tree[0]是最终的赢者,也就是决赛的赢者}template<class T>
void MinimumLoserTree<T>::play(int p, int leftChild, int rightChild) {// tree结点存储相对较大的值,也就是这场比赛的输者tree[p] = loser(leftChild, rightChild);// advance结点存储相对较小的值,也就是这场比赛的晋级者advance[p] = winner(leftChild, rightChild);// 如果p是右孩子while (p % 2 == 1 && p > 1) {tree[p / 2] = loser(advance[p - 1], advance[p]);advance[p / 2] = winner(advance[p - 1], advance[p]);p /= 2;//向上搜索}
}template<class T>
void MinimumLoserTree<T>::rePlay(int thePlayer, T newvalue) {int n = numberOfPlayers;if (thePlayer <= 0 || thePlayer > n) {throw illegalParameterValue("Player index is illegal");}player[thePlayer] = newvalue;int matchNode,//将要比赛的场次leftChild,//比赛结点的左孩子rightChild;//比赛结点的右孩子if (thePlayer <= lowExt) {//如果要比赛的结点在最下层matchNode = (offset + thePlayer) / 2;leftChild = 2 * matchNode - offset;rightChild = leftChild + 1;} else {//要比赛的结点在次下层matchNode = (thePlayer - lowExt + n - 1) / 2;if (2 * matchNode == n - 1) {//特殊情况,比赛的一方是晋级leftChild = advance[2 * matchNode];rightChild = thePlayer;} else {leftChild = 2 * matchNode - n + 1 + lowExt;//这个操作是因为上面matchNode计算中/2取整了rightChild = leftChild + 1;}}//到目前位置,我们已经确定了要比赛的场次以及比赛的选手//下面进行比赛重构,也就是和赢者树最大的不同,分两种情况if (thePlayer == tree[0]) {//当你要重构的点是上一场比赛的赢家的话,过程比赢者树要简化,简化之后只需要和父亲比较,不需要和兄弟比较for (; matchNode >= 1; matchNode /= 2) {int oldLoserNode = tree[matchNode];//上一场比赛的输者tree[matchNode] = loser(oldLoserNode, thePlayer);advance[matchNode] = winner(oldLoserNode, thePlayer);thePlayer = advance[matchNode];}} else {//其他情况重构和赢者树相同tree[matchNode] = loser(leftChild, rightChild);advance[matchNode] = winner(leftChild, rightChild);if (matchNode == n - 1 && n % 2 == 1) {//特殊情况// 特殊在matchNode/2后,左孩子是内部节点,右孩子是外部节点matchNode /= 2;tree[matchNode] = loser(advance[n - 1], lowExt + 1);advance[matchNode] = winner(advance[n - 1], lowExt + 1);}matchNode /= 2;for (; matchNode >= 1; matchNode /= 2) {tree[matchNode] = loser(advance[matchNode * 2], advance[matchNode * 2 + 1]);advance[matchNode] = winner(advance[matchNode * 2], advance[matchNode * 2 + 1]);}}tree[0] = advance[1];//最终胜者
}template<class T>
void MinimumLoserTree<T>::output() const
{cout << "number of players = " << numberOfPlayers<< " lowExt = " << lowExt<< " offset = " << offset << endl;cout << "complete loser tree pointers are" << endl;for (int i = 1; i < numberOfPlayers; i++)cout << tree[i] << ' ';cout << endl;
}#endif //_31LOSERTREE_MINIMUMLOSERTREE_H

MinimumLoserTree.cpp

/*
Project name :			_30winnerTree
Last modified Date:		2023年12月19日16点48分
Last Version:			V1.0
Descriptions:			最小输者树——测试函数
*/
#include "MinimumLoserTree.h"void MinimumLoserTreeTest(){int n;cout << "Enter number of players, >= 2" << endl;cin >> n;if (n < 2){cout << "Bad input" << endl;exit(1);}int *thePlayer = new int[n + 1];cout << "Enter player values" << endl;for (int i = 1; i <= n; i++){cin >> thePlayer[i];}MinimumLoserTree<int> *w =new MinimumLoserTree<int>(thePlayer, n);cout << "The loser tree is" << endl;w->output();w->rePlay(2, 0);cout << "Changed player 2 to zero, new tree is" << endl;w->output();w->rePlay(3, -1);cout << "Changed player 3 to -1, new tree is" << endl;w->output();w->rePlay(7, 2);cout << "Changed player 7 to 2, new tree is" << endl;w->output();delete [] thePlayer;delete w;
}

loserTree.h

/*
Project name :			_30winnerTree
Last modified Date:		2023年12月19日16点48分
Last Version:			V1.0
Descriptions:			最小输者树——虚基类
*/#ifndef _31LOSERTREE_LOSERTREE_H
#define _31LOSERTREE_LOSERTREE_Htemplate<class T>
class loserTree {
public:virtual ~loserTree() {}virtual void initialize(T *thePlayer, int number) = 0;virtual int getTheWinner() const = 0;virtual void rePlay(int thePLayer, T newvalue) = 0;
};#endif //_31LOSERTREE_LOSERTREE_H

myExceptions.h

/*
Project name :			_30winnerTree
Last modified Date:		2023年12月18日16点28分
Last Version:			V1.0
Descriptions:			异常汇总
*/
#pragma once
#ifndef _MYEXCEPTIONS_H_
#define _MYEXCEPTIONS_H_
#include <string>
#include<iostream>
#include <utility>using namespace std;// illegal parameter value
class illegalParameterValue : public std::exception
{
public:explicit illegalParameterValue(string theMessage = "Illegal parameter value"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// illegal input data
class illegalInputData : public std::exception
{
public:explicit illegalInputData(string theMessage = "Illegal data input"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// illegal index
class illegalIndex : public std::exception
{
public:explicit illegalIndex(string theMessage = "Illegal index"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// matrix index out of bounds
class matrixIndexOutOfBounds : public std::exception
{
public:explicit matrixIndexOutOfBounds(string theMessage = "Matrix index out of bounds"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// matrix size mismatch
class matrixSizeMismatch : public std::exception
{
public:explicit matrixSizeMismatch(string theMessage ="The size of the two matrics doesn't match"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// stack is empty
class stackEmpty : public std::exception
{
public:explicit stackEmpty(string theMessage ="Invalid operation on empty stack"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// queue is empty
class queueEmpty : public std::exception
{
public:explicit queueEmpty(string theMessage ="Invalid operation on empty queue"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// hash table is full
class hashTableFull : public std::exception
{
public:explicit hashTableFull(string theMessage ="The hash table is full"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// edge weight undefined
class undefinedEdgeWeight : public std::exception
{
public:explicit undefinedEdgeWeight(string theMessage ="No edge weights defined"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};// method undefined
class undefinedMethod : public std::exception
{
public:explicit undefinedMethod(string theMessage ="This method is undefined"){message = std::move(theMessage);}void outputMessage() {cout << message << endl;}
private:string message;
};
#endif

运行结果

"C:\Users\15495\Documents\Jasmine\prj\_Algorithm\Data Structures, Algorithms and Applications in C++\_31loserTree\cmake-build-debug\_31loserTree.exe"
Enter number of players, >= 2
8
Enter player values
4
6
5
9
8
2
3
7
The loser tree is
number of players  = 8 lowExt = 8 offset = 7
complete winner tree pointers are
1 3 7 2 4 5 8
Changed player 2 to zero, new tree is
number of players  = 8 lowExt = 8 offset = 7
complete winner tree pointers are
6 3 7 1 4 5 8
Changed player 3 to -1, new tree is
number of players  = 8 lowExt = 8 offset = 7
complete winner tree pointers are
6 2 7 1 4 5 8
Changed player 7 to 2, new tree is
number of players  = 8 lowExt = 8 offset = 7
complete winner tree pointers are
6 2 7 1 4 5 8Process finished with exit code 0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/234530.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

虾皮电商申请:一站式开店指南

随着跨境电商的快速发展&#xff0c;越来越多的商家开始意识到东南亚市场的潜力。虾皮电商&#xff08;Shopee&#xff09;作为东南亚地区最大的电商平台之一&#xff0c;为商家提供了一个开拓市场的机会。本文将详细介绍如何在虾皮电商平台上开店&#xff0c;并给出一些建议来…

STM32/STM8资源节约主义编程方式

STM32/STM8资源节约主义编程方式 在小资源芯片进行代码设计时&#xff0c;如STM32C0系列&#xff0c;STM8系列&#xff0c;因为官方库本身要包含各种场景应用特征的支持&#xff0c;所以会有一些冗余的代码占用更多FLASH空间。当需要实现资源占用最简化设计方式时&#xff0c;…

uniapp 导入ucharts图表插件 H5项目, 使用echarts eopts配置

先下载ucharts H5示例源码&#xff1a; uCharts: 高性能跨平台图表库&#xff0c;支持H5、APP、小程序&#xff08;微信小程序、支付宝小程序、钉钉小程序、百度小程序、头条小程序、QQ小程序、快手小程序、360小程序&#xff09;、Vue、Taro等更多支持canvas的框架平台&#…

如何在公网环境下使用Potplayer访问本地群晖webdav中的影视资源

文章目录 本教程解决的问题是&#xff1a;按照本教程方法操作后&#xff0c;达到的效果是&#xff1a;1 使用环境要求&#xff1a;2 配置webdav3 测试局域网使用potplayer访问webdav3 内网穿透&#xff0c;映射至公网4 使用固定地址在potplayer访问webdav ​ 国内流媒体平台的内…

硬件基础与实施运维工程师的介绍

目录 一、实施与运维 1.2 实施运维一般做什么 1.1.1实施工程师 1.1.2运维工程师 1.3 实施运维需要具备哪些技能 三、基础硬件 四、操作系统 4.1 Windows 4.2 Linux 4.3 macOS 4.4 Unix 五、总结 一、实施与运维 1.1 实施运维是干什么的 1、运维工程师负责服务的稳…

第二周:AI产品经理全局学习

一、AI产品架构全景 二、 AI产品岗位分析和了解 三、 AI产品能力模型 四、AI产品经理工作流 五、AI产品经理学习路径和规划 六、本周市场动态

力扣 面试经典150算法题

1合并两个有序数组88. 合并两个有序数组-CSDN博客简单23

如何在Ubuntu系统中安装VNC并结合内网穿透实现远程访问桌面

文章目录 前言1. ubuntu安装VNC2. 设置vnc开机启动3. windows 安装VNC viewer连接工具4. 内网穿透4.1 安装cpolar【支持使用一键脚本命令安装】4.2 创建隧道映射4.3 测试公网远程访问 5. 配置固定TCP地址5.1 保留一个固定的公网TCP端口地址5.2 配置固定公网TCP端口地址5.3 测试…

相机基础概念介绍

一.概念 Camera的成像原理 景物通过镜头&#xff08;LENS&#xff09;生成的光学图像投射到图像传感器(Sensor)表面上&#xff0c;然后转为模拟的电信号&#xff0c;经过 A/D&#xff08;模数转换&#xff09;转换后变为数字图像信号&#xff0c;再送到数字信号处理芯片&…

去掉乘法运算的加法移位神经网络架构

[CVPR 2020] AdderNet: Do We Really Need Multiplications in Deep Learning? 代码&#xff1a;https://github.com/huawei-noah/AdderNet/tree/master 核心贡献 用filter与input feature之间的L1-范数距离作为“卷积层”的输出为了提升模型性能&#xff0c;提出全精度梯度…

【六大排序详解】开篇 :插入排序 与 希尔排序

插入排序 与 希尔排序 六大排序之二 插入排序 与 希尔排序1 排序1.1排序的概念 2 插入排序2.1 插入排序原理2.2 排序步骤2.3 代码实现 3 希尔排序3.1 希尔排序原理3.2 排序步骤3.3 代码实现 4 时间复杂度分析 Thanks♪(&#xff65;ω&#xff65;)&#xff89;下一篇文章见&am…

HIve安装配置(超详细)

文章目录 Hive安装配置一、Hive安装地址二、Hive安装部署1. 把 apache-hive-3.1.2-bin.tar.gz上传到Linux的/export/software目录下2. 解压apache-hive-3.1.2-bin.tar.gz到/export/servers/目录下面3. 修改apache-hive-3.1.2-bin.tar.gz的名称为hive4. 修改/etc/profile&#x…

【Axure高保真原型】中继器表格——移入显示详情卡片

今天和大家分享中继器表格——移入显示详情卡片的原型模板&#xff0c;鼠标移入员工姓名&#xff0c;会显示对应员工的详细卡片&#xff0c;那这个原型是用中继器制作的&#xff0c;所以使用也很方便&#xff0c;在中继器表格里维护对应的信息即可。预览时即可生成交互效果&…

MapReduce综合应用案例 — 电信数据清洗

文章目录 第1关&#xff1a;数据清洗 第1关&#xff1a;数据清洗 测试说明 平台会对你编写的代码进行测试&#xff1a; 评测之前先在命令行启动hadoop&#xff1a;start-all.sh&#xff1b; 点击测评后MySQL所需的数据库和表会自动创建好。 PhoneLog&#xff1a;封装对象 L…

用ThreeJS写了一个圣诞树

使用什么技术写 一开始我准备用htmlcss去写&#xff0c;后来感觉使用html和css写就太low了&#xff0c;没有一点点心意。就打算用three.js写一个3d版本的。 简单介绍一下threejs Three.js是一个基于原生WebGL封装运行的三维引擎&#xff0c;是最著名的3D WebGL JavaScriptTh…

SpringBoot3知识总结

SpringBoot3 1、简介 1. 前置知识 Java17Spring、SpringMVC、MyBatisMaven、IDEA 2. 环境要求 环境&工具版本&#xff08;or later&#xff09;SpringBoot3.0.5IDEA2022Java17Maven3.5 3. SpringBoot是什么 Spring Boot是Spring项目中的一个子工程&#xff0c;与我们…

第二百一十五回 如何创建单例模式

文章目录 1. 概念介绍2. 思路与方法2.1 实现思路2.2 实现方法 3. 示例代码4. 内容总结 我们在上一章回中介绍了"分享三个使用TextField的细节"沉浸式状态样相关的内容&#xff0c;本章回中将介绍 如何创建单例模式.闲话休提&#xff0c;让我们一起Talk Flutter吧。 …

java实现局域网内视频投屏播放(三)投屏原理

常见投屏方案 常见的投屏方案主要有以下几种&#xff1a; DLNA DLNA的全称是DIGITAL LIVING NETWORK ALLIANCE(数字生活网络联盟)。DLNA委员会已经于2017年1月5日正式解散&#xff0c;原因是旧的标准已经无法满足新设备的发展趋势&#xff0c;DLNA标准将来也不会再更新。但是…

【MySQL】数据库基础入门 安装MySQL

目录 介绍&#xff1a; 安装MySQL: 设置 root 账号密码 2.配置环境变量 2.找到 Path 系统变量, 点击 "编辑" 介绍&#xff1a; MySQL是一个开源的关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;它是一种用于管理和存储数据的软件。 安装MySQL: …