「二叉搜索树 / set / 朝鲜树 / 替罪羊树」快速排序
要求
给定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
参考链接:
https://baike.baidu.com/item/朝鲜树/17008833
「二叉搜索树 / set / 朝鲜树 / 替罪羊树」快速排序的更多相关文章
- 二叉树、二叉搜索树、平衡二叉树、B树、B+树的精确定义和区别探究
概述 关于树的概念很多,B树,B+树,红黑树等等. 但是你去翻翻百度百科,或者用百度或者谷歌搜索一下中文的树结构的介绍,全都是狗屁.没有哪个中文网站是真正精确解释树的定义的,尤其是百度百科. 下面我要 ...
- 第七章 二叉搜索树(d4)AVL树:(3+4)-重构
- 第七章 二叉搜索树 (d3)AVL树:删除
- 第七章 二叉搜索树 (d2)AVL树:插入
- 第七章 二叉搜索树 (d1)AVL树:重平衡
- 高度平衡的二叉搜索树(AVL树)
AVL树的基本概念 AVL树是一种高度平衡的(height balanced)二叉搜索树:对每一个结点x,x的左子树与右子树的高度差(平衡因子)至多为1. 有人也许要问:为什么要有AVL树呢?它有什么 ...
- 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 ...
- PAT L3-016 二叉搜索树的结构
https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...
- L3-1 二叉搜索树的结构 (30 分)
讲解的很不错的链接:https://blog.csdn.net/chudongfang2015/article/details/79446477#commentBox 题目链接:https://pin ...
随机推荐
- 【转】wait和waitpid详解
发现进程有关的编程题里面的包含知识量实在是太庞大,这是关于wait和waitpid区别的,以前只是粗略知道它们的区别,这是网上看到的比较全的对比 转自http://blog.chinaunix.net ...
- 美化console.log的文本(转载)
原文地址:http://www.css88.com/archives/5260 JavaScript Console 那些少人所知的特性 console.log("%c css88.com& ...
- HDU 5916 Harmonic Value Description (构造)
题意:给你 n 和 m,求一个1-n的排列,使得∑gcd(Ai,Ai+1) 恰为第 m 小. 析:可以想到最小的就是相邻都互质,然后依次,第 m 小就可以有一个是gcd为 k,然后其他的为1喽. 那 ...
- adb: command not found 解決方法(转载)
转自:http://a7419.pixnet.net/blog/post/59806205-adb%3A-command-not-found--%E8%A7%A3%E6%B1%BA%E6%96%B9% ...
- PCB Redis的安装使用
记录一下Redis的安装与基本使用 一.Redis简介 Redis(REmote DIctionary Server)远程字典服务器,免费开源,是一个高性能的(Key/Value)分布式内存数据库.其 ...
- Ruby Encoding类
Encoding类 内部编码 IO对象内部处理时候的编码 外部编码 IO对象对外输出的时候的编码 输入 外部字符与自己的外部编码对比(没设定的默认 ...
- 解决Robot Framework运行时没有Log的方案
Robot Framework自动化测试过程中,运行多次后会出现RIDE没有log的情况. 造成这种现象的原因是: 执行失败的测试用例,chrome.exe和chromedriver.exe进程没有关 ...
- Android Dialogs(1)Dialog简介及Dialog分类
Dialogs A dialog is a small window that prompts the user to make a decision or enter additional info ...
- Android最简单的实例 :环境搭建及HelloWorld
Android开发之旅:环境搭建及HelloWorld 2010-04-12 00:45 by 吴秦, 883961 阅读, 140 评论, 收藏, 编辑 ——工欲善其事必先利其器 引言 本系列适合 ...
- 转-Mac下Apache Tomcat安装配置
ava Web如果稍微知道一点,一般对Tomcat都不会陌生,Apache是普通服务器,本身只支持html即普通网页,可以通过插件支持PHP,还可以与Tomcat连通(单向Apache连接Tomcat ...