数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
本文地址: http://blog.csdn.net/caroline_wendy
算法逻辑: 依据节点的大小, 建立树, 输出树的根节点, 并把此重置为最大值, 再重构树.
由于树中保留了一些比較的逻辑, 所以降低了比較次数.
也称锦标赛排序, 时间复杂度为O(nlogn), 由于每一个值(共n个)须要进行树的深度(logn)次比較.
參考<数据结构>(严蔚敏版) 第278-279页.
树形选择排序(tree selection sort)是堆排序的一个过渡, 并非核心算法.
可是全然依照书上算法, 实现起来极其麻烦, 差点儿没有不论什么人实现过.
须要记录建树的顺序, 在重构时, 才干降低比較.
本着娱乐和分享的精神, 应人之邀, 简单的实现了一下.
代码:
/*
* TreeSelectionSort.cpp
*
* Created on: 2014.6.11
* Author: Spike
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <climits> using namespace std; /*树的结构*/
struct BinaryTreeNode{
bool from; //推断来源, 左true, 右false
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}; /*构建叶子节点*/
BinaryTreeNode* buildList (const std::vector<int>& L)
{
BinaryTreeNode* btnList = new BinaryTreeNode[L.size()]; for (std::size_t i=0; i<L.size(); ++i)
{
btnList[i].from = true;
btnList[i].m_nValue = L[i];
btnList[i].m_pLeft = NULL;
btnList[i].m_pRight = NULL;
} return btnList;
} /*不足偶数时, 需补充节点*/
BinaryTreeNode* addMaxNode (BinaryTreeNode* list, int n)
{
/*最大节点*/
BinaryTreeNode* maxNode = new BinaryTreeNode(); //最大节点, 用于填充
maxNode->from = true;
maxNode->m_nValue = INT_MAX;
maxNode->m_pLeft = NULL;
maxNode->m_pRight = NULL; /*复制数组*/
BinaryTreeNode* childNodes = new BinaryTreeNode[n+1]; //添加一个节点
for (int i=0; i<n; ++i) {
childNodes[i].from = list[i].from;
childNodes[i].m_nValue = list[i].m_nValue;
childNodes[i].m_pLeft = list[i].m_pLeft;
childNodes[i].m_pRight = list[i].m_pRight;
}
childNodes[n] = *maxNode;
delete[] list;
list = NULL; return childNodes;
} /*依据左右子树大小, 创建树*/
BinaryTreeNode* buildTree (BinaryTreeNode* childNodes, int n)
{
if (n == 1) {
return childNodes;
} if (n%2 == 1) {
childNodes = addMaxNode(childNodes, n);
} int num = n/2 + n%2;
BinaryTreeNode* btnList = new BinaryTreeNode[num];
for (int i=0; i<num; ++i) {
btnList[i].m_pLeft = &childNodes[2*i];
btnList[i].m_pRight = &childNodes[2*i+1];
bool less = btnList[i].m_pLeft->m_nValue <= btnList[i].m_pRight->m_nValue;
btnList[i].from = less;
btnList[i].m_nValue = less ? btnList[i].m_pLeft->m_nValue : btnList[i].m_pRight->m_nValue;
} buildTree(btnList, num); } /*返回树根, 又一次计算数*/
int rebuildTree (BinaryTreeNode* tree)
{
int result = tree[0].m_nValue; std::stack<BinaryTreeNode*> nodes;
BinaryTreeNode* node = &tree[0];
nodes.push(node); while (node->m_pLeft != NULL) {
node = node->from ? node->m_pLeft : node->m_pRight;
nodes.push(node);
} node->m_nValue = INT_MAX;
nodes.pop(); while (!nodes.empty())
{
node = nodes.top();
nodes.pop();
bool less = node->m_pLeft->m_nValue <= node->m_pRight->m_nValue;
node->from = less;
node->m_nValue = less ?
node->m_pLeft->m_nValue : node->m_pRight->m_nValue;
} return result;
} /*从上到下打印树*/
void printTree (BinaryTreeNode* tree) { BinaryTreeNode* node = &tree[0];
std::queue<BinaryTreeNode*> temp1;
std::queue<BinaryTreeNode*> temp2; temp1.push(node); while (!temp1.empty())
{
node = temp1.front();
if (node->m_pLeft != NULL && node->m_pRight != NULL) {
temp2.push(node->m_pLeft);
temp2.push(node->m_pRight);
} temp1.pop(); if (node->m_nValue == INT_MAX) {
std::cout << "MAX" << " ";
} else {
std::cout << node->m_nValue << " ";
} if (temp1.empty())
{
std::cout << std::endl;
temp1 = temp2;
std::queue<BinaryTreeNode*> empty;
std::swap(temp2, empty);
}
}
} int main ()
{
std::vector<int> L = {49, 38, 65, 97, 76, 13, 27, 49};
BinaryTreeNode* tree = buildTree(buildList(L), L.size()); std::cout << "Begin : " << std::endl;
printTree(tree); std::cout << std::endl; std::vector<int> result;
for (std::size_t i=0; i<L.size(); ++i)
{
int value = rebuildTree (tree);
std::cout << "Round[" << i+1 << "] : " << std::endl;
printTree(tree); std::cout << std::endl;
result.push_back(value);
} std::cout << "result : ";
for (std::size_t i=0; i<L.size(); ++i) {
std::cout << result[i] << " ";
}
std::cout << std::endl; return 0;
}
输出:
Begin :
13
38 13
38 65 13 27
49 38 65 97 76 13 27 49 Round[1] :
27
38 27
38 65 76 27
49 38 65 97 76 MAX 27 49 Round[2] :
38
38 49
38 65 76 49
49 38 65 97 76 MAX MAX 49 Round[3] :
49
49 49
49 65 76 49
49 MAX 65 97 76 MAX MAX 49 Round[4] :
49
65 49
MAX 65 76 49
MAX MAX 65 97 76 MAX MAX 49 Round[5] :
65
65 76
MAX 65 76 MAX
MAX MAX 65 97 76 MAX MAX MAX Round[6] :
76
97 76
MAX 97 76 MAX
MAX MAX MAX 97 76 MAX MAX MAX Round[7] :
97
97 MAX
MAX 97 MAX MAX
MAX MAX MAX 97 MAX MAX MAX MAX Round[8] :
MAX
MAX MAX
MAX MAX MAX MAX
MAX MAX MAX MAX MAX MAX MAX MAX result : 13 27 38 49 49 65 76 97
数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)的更多相关文章
- 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort
选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...
- 数据结构 - 只需选择排序(simple selection sort) 详细说明 和 代码(C++)
数据结构 - 只需选择排序(simple selection sort) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/28601 ...
- 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现
选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...
- 简单选择排序(Simple Selection Sort)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- js 实现排序算法 -- 选择排序(Selection Sort)
原文: 十大经典排序算法(动图演示) 选择排序(Selection Sort) 选择排序(Selection-sort)是一种简单直观的排序算法.它的工作原理:首先在未排序序列中找到最小(大)元素,存 ...
- 【排序基础】1、选择排序法 - Selection Sort
文章目录 选择排序法 - Selection Sort 为什么要学习O(n^2)的排序算法? 选择排序算法思想 操作:选择排序代码实现 选择排序法 - Selection Sort 简单记录-bobo ...
- 【算法】选择排序(Selection Sort)(二)
选择排序(Selection Sort) 选择排序(Selection-sort)是一种简单直观的排序算法.它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余 ...
- 数据结构与算法-排序(二)选择排序(Selection Sort)
摘要 选择排序的逻辑是先遍历比较出序列中最大的,然后把最大的放在最后位置. 遵循这个逻辑,用代码实现时,做到1.减少比较次数之外,这里引入一个新的指标 - 稳定性,2.保证排序过程中的稳定性也是一个优 ...
- 选择排序(Selection Sort)
选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...
随机推荐
- 最大似然估计的缺陷 —— 方差和均值的 bias
0. 均匀分布期望的最大似然估计 首先我们来看,如何通过最大似然估计的形式估计均匀分布的期望.均匀分布的概率密度函数为:f(x|θ)=1θ,0≤x≤θ.不失一般性地,将 x1,x2,-,xn 排序为顺 ...
- FC 网络
通常情况下,SAN系统中服务器与存储介质通过一种特殊的网络相连,这种网络就是FC 网络. FC 网络是一种新发展的与传统的TCP/IP网络并列的一种高速网络.它有自己的地址分配和网络管理的体系. FC ...
- USACO 2.1 Sorting a Three-Valued Sequence
Sorting a Three-Valued Sequence IOI'96 - Day 2 Sorting is one of the most frequently performed compu ...
- spring《四》自动装配
byName模式<bean autowire="byName"> Spring会查找一个叫做date的bean定义. byType模式<bean autowire ...
- sql获得某个时间段的数据
CONVERT(Date, 时间字符串变量 ) between CONVERT(Date,'2015/2/10') and CONVERT(Date,'2015-3-10')
- C/C++函数调用约定与this指针
关于 C/C++ 函数调用约定,大多数时候并不会影响程序逻辑,但遇到跨语言编程时,了解一下还是有好处的. VC 中默认调用是 __cdecl 方式,Windows API 使用 __stdcall 调 ...
- Kattis -Backspace
Backspace Bjarki having trouble Shortly before the programming contest started, Bjarki decided to up ...
- 问题请教:关于同一个POD中多容器的广播信息问题
广大博友好,最近在K8S集群中遇到一个问题,贴出来同大家分享一下 同一个POD中多个容器 如何处理广播信息? 经测试 同一个POD中当先启动的容器占用广播端口后,其他的容器启动就会报bind erro ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- IOS - PDF合并
#pragma mark - Merge PDF - (void)mergePDF { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSD ...