要求

给定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. Spring Cloud 学习总结001-服务治理-Eureka

    学习参考:http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/ spring cloud由[服务注册中 ...

  2. B. Flag of Berland

    B. Flag of Berland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数

    BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数 Description 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系数不定方程的数学家之一. 为了纪念他,这些方程一般被称 ...

  4. 【HAOI 2007】 上升序列

    [题目链接] 点击打开链接 [算法] 先预处理 : 将序列反转,求最长下降子序列 对于每个询问,根据字典序性质,贪心即可 [代码] #include<bits/stdc++.h> usin ...

  5. BZOJ2283: [Sdoi2011]火星移民

    Description 在2xyz年,人类已经移民到了火星上.由于工业的需要,人们开始在火星上采矿.火星的矿区是一个边长为N的正六边形,为了方便规划,整个矿区被分为6*N*N个正三角形的区域(如图1) ...

  6. 动画库tween.js

    动画库tween.js var Tween = { Linear:function (start,alter,curTime,dur) {return start+curTime/dur*alter; ...

  7. Pascal输出星星

    program Project2; {$APPTYPE CONSOLE} uses SysUtils; var i,j:integer; begin { TODO -oUser -cConsole M ...

  8. Windows Hadoop安装

    由于hadoop版本2.7.1对其他相关工具兼容较好,本文以此版本为例. 一.下载解压 各镜像站现已没有这个版本,所以去Apache官网下载 http://www.apache.org/dyn/clo ...

  9. bzoj 4826: [Hnoi2017]影魔【单调栈+树状数组+扫描线】

    参考:https://www.cnblogs.com/lcf-2000/p/6789680.html 这是一个相对码量少的做法,用到了区间修改区间查询的树状数组,详见:www.cnblogs.com/ ...

  10. 第四代增强 NEW BADI的定义及实现

    NEW BADI 是在第四代增强框架下创建的BADI,是相对于第三代增强Classic Badi 而言的. 根据第四代增强的基本概念,所有显式增强Enhancement options 都必须放在增强 ...