要求

给定n个数,对这n个数进行排序

这题当然可以直接调用sort

#include<cstdio>
#include<vector>
#define ll long long
using namespace std;
ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
vector<int> a;
int main()
{
n=read();
for(int i=;i<=n;i++)
{
int x=read();
a.push_back(x);
}
sort(a.begin(),a.end());
for(vector<int>::iterator i=a.begin();i!=a.end();i++)
printf("%d ",*i);
return ;
}

用set实现排序,元素必须无重复

 #include<cstdio>
#include<set>
#define ll long long
using namespace std;
ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
set<int>st;
int main()
{
n=read();
for(int i=;i<=n;i++)
{
int x=read();
st.insert(x);
}
for(set<int>::iterator i=st.begin();i!=st.end();i++)
printf("%d ",*i);
return ;
}

用二叉搜索树来排序,但不能通过已经排序好的大数据点

 #include<cstdio>
#define ll long long
using namespace std;
ll read()
{
ll x = , f = ; char ch = getchar();
while (ch<'' || ch>'') { if (ch == '-')f = -; ch = getchar(); }
while (ch >= ''&&ch <= '') { x = x * + ch - ''; ch = getchar(); }
return x * f;
}
int rt, cnt; //rt为根节点标号,cnt为当前节点个数
int t, n, ans;
int v[], ls[], rs[];
int insert(int &k, int x)
{
if (!k)
{
k = ++cnt;
v[k] = x;
return k;
}
if (x < v[k]) insert(ls[k], x);
else insert(rs[k], x);
return k;
} //中序遍历
void dfs(int x)
{
if (!x)return;
dfs(ls[x]);
printf("%d ", v[x]);
dfs(rs[x]);
}
int main()
{
n = read();
for (int i = ; i <= n; i++)
{
int x = read();
insert(rt, x);
}
dfs(rt);
return ;
}

可以打乱输入的数据实现深度期望

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#define ll long long
using namespace std; ll read()
{
ll x = , f = ; char ch = getchar();
while (ch<'' || ch>'') { if (ch == '-')f = -; ch = getchar(); }
while (ch >= ''&&ch <= '') { x = x * + ch - ''; ch = getchar(); }
return x * f;
}
int rt, cnt;
int t, n, ans;
int v[], ls[], rs[]; int insert(int &k, int x)
{
if (!k)
{
k = ++cnt;
v[k] = x;
return k;
}
if (x < v[k])insert(ls[k], x);
else insert(rs[k], x);
return k;
}
void dfs(int x)
{
if (!x)return;
dfs(ls[x]);
printf("%d ", v[x]);
dfs(rs[x]);
}
int a[];
int main()
{
n = read();
for (int i = ; i <= n; i++)
{
a[i] = read();
swap(a[i], a[rand() % i + ]);
}
for (int i = ; i <= n; i++)
insert(rt, a[i]);
dfs(rt);
return ;
}

朝鲜树,当插入超过某个深度时重构整颗树

 #include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define mod 1000000
#define pi acos(-1)
#define inf 0x7fffffff
#define ll long long
using namespace std;
ll read()
{
ll x = , f = ; char ch = getchar();
while (ch<'' || ch>'') { if (ch == '-')f = -; ch = getchar(); }
while (ch >= ''&&ch <= '') { x = x * + ch - ''; ch = getchar(); }
return x * f;
}
bool flag;
int rt, cnt;
int t, n, ans;
int v[], ls[], rs[];
int a[]; int insert(int &k, int x, int depth)
{
if (depth > ) flag = ; //插入某个数时深度大于设定,将重构标志设为true
if (!k)
{
k = ++cnt;
v[k] = x;
return k;
}
if (x < v[k])insert(ls[k], x, depth + );
else insert(rs[k], x, depth + );
return k;
}
void dfs(int x)
{
if (!x)return;
dfs(ls[x]);
printf("%d ", v[x]);
dfs(rs[x]);
} //简单重构,甚至没有利用前i个有序
void rebuild(int &k, int l, int r)
{
if (l > r)return;
int mid = (l + r) >> ;
k = mid;
v[k] = a[mid];
rebuild(ls[k], l, mid - );
rebuild(rs[k], mid + , r);
}
int main()
{
n = read();
for (int i = ; i <= n; i++)
a[i] = read();
for (int i = ; i <= n; i++)
{
insert(rt, a[i], );
if (flag)
{
for (int j = ; j <= i; j++) ls[j] = rs[j] = v[j] = ;
rebuild(rt, , i); //对前i个重构
flag = ;
}
}
dfs(rt);
return ;
}

替罪羊树

通过非旋转的重构实现的二叉平衡树,是朝鲜树的高级版,详情可见https://www.cnblogs.com/lfri/p/10006414.html

参考链接:

http://hzwer.com/8016.html

https://baike.baidu.com/item/朝鲜树/17008833

「二叉搜索树 / set / 朝鲜树 / 替罪羊树」快速排序的更多相关文章

  1. 二叉树、二叉搜索树、平衡二叉树、B树、B+树的精确定义和区别探究

    概述 关于树的概念很多,B树,B+树,红黑树等等. 但是你去翻翻百度百科,或者用百度或者谷歌搜索一下中文的树结构的介绍,全都是狗屁.没有哪个中文网站是真正精确解释树的定义的,尤其是百度百科. 下面我要 ...

  2. 第七章 二叉搜索树(d4)AVL树:(3+4)-重构

  3. 第七章 二叉搜索树 (d3)AVL树:删除

  4. 第七章 二叉搜索树 (d2)AVL树:插入

  5. 第七章 二叉搜索树 (d1)AVL树:重平衡

  6. 高度平衡的二叉搜索树(AVL树)

    AVL树的基本概念 AVL树是一种高度平衡的(height balanced)二叉搜索树:对每一个结点x,x的左子树与右子树的高度差(平衡因子)至多为1. 有人也许要问:为什么要有AVL树呢?它有什么 ...

  7. LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  8. PAT L3-016 二叉搜索树的结构

    https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...

  9. L3-1 二叉搜索树的结构 (30 分)

    讲解的很不错的链接:https://blog.csdn.net/chudongfang2015/article/details/79446477#commentBox 题目链接:https://pin ...

随机推荐

  1. python 列表,元祖,字典

    一 列表 1 列表的循环遍历 namesList = ['xiaoWang','xiaoZhang','xiaoHua'] for name in namesList: print(name) 结果 ...

  2. luogu 3960 列队

    noip2017 D2T3 列队 某zz选手当时直接放弃了写了50还写错了 题目大意: 有一个n行m列的方阵,第i行j列的点编号为(i-1)m+j 每次把第x行y列的点拿出来,然后把这一行它之后的点都 ...

  3. VS2013文件同步插件开发

    一.插件功能描述 插件监控一个xml文件,当该文档有添加新元素在保存的时候将新增的元素同步到指定的目录下. 二.模板的选择 由于该功能是跟代码编辑有关的,要监控文档的保存事件,所以要在文档打开的时候就 ...

  4. 任务42:EF Core Migration

    任务42:EF Core Migration 右边的是在VS2017中使用的命令,左边是在VSCode 的DOS窗体中使用的 最新版本的core 2.2.1的 版本创建以后已经没有model类了. 下 ...

  5. springmvc h5上传图片

    工作中开发一个评价功能,需要上传拍照的图片,后台使用springmvc接收文件,前端FormData异步提交. 1. spring配置multipartResolver <bean id=&qu ...

  6. H5的draggable属性和jqueryUI.sortable

    拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 一.HTML5 新特性 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. Event On Event Handler 描述 d ...

  7. 创建swagger的springboot-stater,并在spring cloud zuul网关中引入

    Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...

  8. 实现strcmp功能

    判断两个字符串的大小 #include <stdio.h> int my_strcmp(const char *str1,const char *str2) { //判断两个字符串是否为空 ...

  9. [POI2008]Sta

    Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...

  10. Tarjan UVALive 6511 Term Project

    题目传送门 /* 题意:第i个人选择第a[i]个人,问组成强联通分量(自己连自己也算)外还有多少零散的人 有向图强联通分量-Tarjan算法:在模板上加一个num数组,记录每个连通分量的点数,若超过1 ...