「二叉搜索树 / 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 ...
随机推荐
- YTU 1075: Time
1075: Time 时间限制: 1 Sec 内存限制: 128 MB 提交: 7 解决: 7 [提交][状态][讨论版] 题目描述 Digital clock use 4 digits to e ...
- java 类属性、方法加载的顺序
1.静态变量 2.静态代码块 3.局部代码块 4.构造函数 5.普通代码块 6.静态方法 7.普通方法 8.普通属性
- 源代码管理工具GIT
01.GIT简介 svn是集中式的源代码管理工具,必须联网才能操作 git是分布式的. 有两中:一个是本地代码仓库,一个是远程代码仓库 分布式源代码管理工具 02.GIT - 本地代码仓库使用流程 1 ...
- leetcode 戳气球
有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[ ...
- idea清除缓存和索引
转自:https://blog.csdn.net/mzy755423868/article/details/80559381
- 关于python安装lxml插件的问题
文章只是介绍自己安装时从安装不上到安装后报错,再到安装成功的心路历程,并不代表广大欧皇也会会出现同类型的问题,也不是总结和汇总各种出问题的原因. 直接进入正题,首先我这边是win环境,电脑上装的是py ...
- 【爬坑系列】之docker的overlay网络配置(未完,待续)
理论知识储备: 想了解vxlan网络的知识:https://www.cnblogs.com/shuiguizi/p/10923841.html 想了解docker网络的原理知识:https://www ...
- icons使用
1.将选中图标加入项目 2.unicode方式查看连接在线连接 3.复制代码到样式表 4.引用样式,并设置I标签,颜色和大小可以通过设置i标签color和font-size进行调整 <i cla ...
- 【css】如何实现环形进度条
最近团队的童鞋接到了一个有关环形进度条的需求,想要还原一个native的沿环轨迹渐变进度条的效果,看到这个效果的时候,笔者陷入了沉思.. 环形进度条的效果,最先想到的就是使用CSS利用两个半圆的hac ...
- Android偏好设置(2)为应用定义一个偏好设置xml
1.Defining Preferences in XML Although you can instantiate new Preference objects at runtime, you sh ...