要求

给定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. 一些linux嵌入式资源下载地址

    linux内核源代码情景分析 非扫描版 上下册合订版 字清楚 带书签 1575页 pdfhttp://download.csdn.net/source/2002579***************** ...

  2. 并不对劲的LCT

    LCT,是连猫树(link-cat-tree)的缩写.它是树链剖分和splay的结合版本. 由于有很多关于LCT的文章以及这并不是对劲的文章,并不对劲的人并不打算讲得太详细. 推荐:详细的LCT-&g ...

  3. uoj 30 tourists

    题目大意: 一个无向图 每个点有权值 支持两个操作 1 修改某个点的权值 2 查询a-b所有简单路径的点上的最小值 思路: 可以把图变成圆方树 然后树链剖分 维护 对于每个方点使用可删堆维护 #inc ...

  4. Bootstrap中的各种下拉菜单

    @*基本下拉菜单与按钮下拉菜单的样式完全一致.不过,基本的下拉菜单使用<div class="dropdown">包裹,所有要换行.而按钮式下拉菜单<div cl ...

  5. Struts Filter告警:FilterDispatcher <<< is deprecated! Please use the new filters!

    在struts2.3.14下,web.xml中使用 <filter> <filter-name>struts2</filter-name> <!-- < ...

  6. strncasecmp与strcasecmp用法(转载)

    转自: http://blog.csdn.net/acb0y/article/details/5333334 strcasecmp strcasecmp(忽略大小写比较字符串)  相关函数 bcmp, ...

  7. Codeforces Round #325D (Div. 2) (DP)

    题目链接: D. Phillip and Trains 分析:dp 我们先初始化,dp[i]表示当前列第i行是否可达,r[i]表示上一个dp值,接下来从头搜到尾 如果该位置满足s[i+1]=='.'且 ...

  8. sublim text3快速生成html代码时,tab键失效问题

    sublime text3是一款非常强大的文本编辑器,个人觉得做前端的话这款工具很好用.便携,秒启.唯一让我觉得不是特别爽的就是插件啊,都需要自己安装.不过瑕不掩瑜,这款编辑器是很适合开发前端和PHP ...

  9. hdu 5201 The Monkey King【容斥原理+组合数学】

    原来我一开始以为的\( O(n^2) \)是调和级数\( O(nlog_2n) \)的! 首先枚举猴王的桃子个数\( x \),然后使用容斥原理,枚举有至少\( k \)个不满足的条件,那么这\( k ...

  10. 【插件开发】—— 13 GEF双击模型事件

    前文回顾: 1 插件学习篇 2 简单的建立插件工程以及模型文件分析 3 利用扩展点,开发透视图 4 SWT编程须知 5 SWT简单控件的使用与布局搭配 6 SWT复杂空间与布局搭配 7 SWT布局详解 ...