BST(普通二叉搜索树)
没有例题,没找
#include<bits/stdc++.h>
#define ElemType int
#define ENDIN 0
#define mian() main()
#define breal break
using namespace std;
typedef struct BSTNode{
ElemType data;//当前节点数据
BSTNode *lc,*rc;//左右子结点指针
}BSTNode,*BSTree;
BSTree SearchBST(BSTree T,ElemType key){//查找
if((!T)||key==T->data){//树空(未找到)或找到,返回当前节点
return T;
}
else if(key<T->data){//查找元素比当前小,去左边
return SearchBST(T->lc,key);
}
else if(key>T->data){//否则去右边
return SearchBST(T->rc,key);
}
}
void InsertBST(BSTree &T,ElemType key){//查找
if(!T){//找到了合适位置
BSTree S= new BSTNode;//申请内存创建节点
S->data =key;
S->lc=S->rc=NULL;
T=S;
}
else if(key<T->data){//查找元素比当前小,去左边
//cout<<T->lc<<endl;
InsertBST(T->lc,key);
//cout<<T->lc<<endl;
}
else if(key>T->data){//否则去右边
InsertBST(T->rc,key);
}
}
void CreateBST(BSTree &T){
T=NULL;
ElemType e;
cin>>e;
while(e!=ENDIN){
InsertBST(T,e);
cin>>e;
}
}
void DeleteBST(BSTree &T,ElemType dk){
BSTree p=T,f=NULL,q,s;
if(!T){
return ;
}
while(p){
if(p->data==dk){
breal;
}
f=p;
if(dk<p->data){
p=p->lc;
}
else{
p=p->rc;
}
}
if(p==NULL){
return;
}
if(p->lc&&p->rc){
BSTree S=T->lc,Q=T;
while(S->rc!=NULL){
Q=S;
S=S->rc;
}
T->data=S->data;
if(Q!=T){
Q->rc=S->lc;
}
else{
Q->lc=S->lc;
}
delete S;
return ;
}
else if(T->lc==NULL&&T->rc==NULL){
cout<<"删除:"<<T->data<<endl;
delete T;
return ;
}
else if(T->rc==NULL){
T=T->lc;
return ;
}
else if(T->lc==NULL){
T=T->rc;
return ;
}
else{}
}
void bianli(BSTree T){
if(T->lc){
bianli(T->lc);
}
cout<<T->data<<' ';
if(T->rc){
bianli(T->rc);
}
}
int main(){
BSTree T;
CreateBST(T);
ElemType searchkey;
bianli(T);
cout<<"查找:";
cin>>searchkey;
if(SearchBST(T,searchkey)){
cout<<"找到了"<<endl;
}
else{
cout<<"404:未找到"<<endl;
}
ElemType deletekey;
cout<<"删除元素:";
cin>>deletekey;
DeleteBST(T,deletekey);
if(SearchBST(T,deletekey)){
cout<<"找到了"<<endl;
}
else{
cout<<"404:未找到"<<endl;
}
bianli(T);
}AVL平衡树
这个是之前写的忘了是啥题了
/*
平衡树(AVL)模板
规则:
左子树所有节点都比根节点小,右子树所有节点都比根节点大
无重复元素
整个树最大深度-最小深度<2
调平衡:
分为LL,RR,LR,RL
*/
#include <bits/stdc++.h>
#define debug cout<<"debug"<<endl;
#define INT const int
#define LF const double
using namespace std;
INT MAXN=1e4+10;
INT MAXM=1e5+10;
typedef struct AVLNode{
int data,height;//节点内容 节点高度
struct AVLNode *lchild;
struct AVLNode *rchild;
//左右子节点指针
}*AVLTree;//实际上就是把AVLNode*这个指针类型重命名为AVLTree,乐死我了多长时间了才看懂
int Height(AVLTree T){
if(!T){
return 0;
}
return T->height;
}
inline void updateHeight(AVLTree &T){
if(!T){//节点空,没有高度
return;
}
T->height=max(Height(T->lchild),Height(T->rchild))+1;//当前节点高度为它子节点高度最大值加一
}
AVLTree LL(AVLTree &T){
AVLTree temp=T->lchild;
T->lchild=temp->rchild;
temp->rchild=T;
//……不理解自己画图,我懒得描述了
updateHeight(T);
updateHeight(temp);
return temp;//此时temp已经代替T为根了
}
AVLTree RR(AVLTree &T){
AVLTree temp=T->rchild;
T->rchild=temp->lchild;
temp->lchild=T;
//……不理解自己画图,我懒得描述了
updateHeight(T);
updateHeight(temp);
return temp;//此时temp已经代替T为根了
}
AVLTree LR(AVLTree &T){
AVLTree temp=T->lchild->rchild;
T->lchild->rchild=temp->lchild;
temp->lchild=T->lchild;
T->lchild=temp;
updateHeight(T);
updateHeight(temp);
T=LL(T);//转换成LL型了,采用LL平衡
return T;
}
AVLTree RL(AVLTree &T){
AVLTree temp=T->rchild->lchild;
T->rchild->lchild=temp->rchild;
temp->rchild=T->rchild;
T->rchild=temp;
updateHeight(T);
updateHeight(temp);
T=RR(T);//转换成RR型了,采用RR平衡
return T;
}
inline void adjust(AVLTree &T){
if(!T){//空树无需操作
return;
}
if(Height(T->lchild)-Height(T->rchild)>1){//左子树比右子树高,确定平衡类型为L-(高差大于1,触发平衡机制)
if(Height(T->lchild->lchild)>=Height(T->lchild->rchild)){//左子树中左子树比右子树高,是LL
T=LL(T);
}
else{//左子树中右子树比左子树高,是LR
T=LR(T);
}
}
else if(Height(T->rchild)-Height(T->lchild)>1){//右子树比左子树高,确定平衡类型为R-(高差大于1,触发平衡机制)
if(Height(T->rchild->rchild)>=Height(T->rchild->lchild)){//右子树中右子树比左子树高,是RR
T=RR(T);
}
else{//右子树中左子树比右子树高,是RL
T=RL(T);
}
}
}
AVLTree Insert(AVLTree &T,int x){
if(!T){//树空
T=new AVLNode;//向堆区申请内存
T->lchild =T->rchild=NULL;//左右子节点为空
T->data =x;
T->height=1;//最新节点,所以高为1(本身)
return T;
}
if(T->data==x){//插入点已存在
return T;//无需操作
}
else if(x<T->data){//小于根节点,向左
T->lchild=Insert(T->lchild,x);
}
else{//大于根节点,向右
T->rchild=Insert(T->rchild,x);
}
updateHeight(T);//更新当前节点高度
adjust(T);//调平衡
return T;
}
void CreatAVL(AVLTree &T){
int n,x;
cin>>n;
for(int i=1;i<=n;++i){
cin>>x;
Insert(T,x);//在树T插入x
//debug;
}
}
void inorder(AVLTree T){
if(T->lchild)
inorder(T->lchild);
cout<<T->data<<" "<<T->height<<endl;
if(T->rchild)
inorder(T->rchild);
}
void show(AVLTree T){
inorder(T);
}
AVLTree DeleteAVL(AVLTree &T,int key){
if(!T){
return T;
}
if(T->data==key){
if(!T->lchild||!T->rchild){
AVLTree temp=T;
T=(T->lchild?T->lchild:T->rchild);
delete temp;
}
else{
AVLTree s=T->lchild;
while(s->rchild){
s=s->rchild;
}
T->data=s->data;
T->lchild=DeleteAVL(T->lchild,T->data);
}
}
else{
if(key<T->data){
T->lchild=DeleteAVL(T->lchild,key);
}
else{
T->rchild=DeleteAVL(T->rchild,key);
}
}
updateHeight(T);
adjust(T);
return T;
}
int main() {
AVLTree root = NULL;//空树
CreatAVL(root);
show(root);
while(1){
int x;
cin>>x;
DeleteAVL(root,x);
show(root);
}
}splay模板
问题:洛谷P3369
注释已经足够详细\
#include <bits/stdc++.h>
//#define int int64_t
//#define int __int128
//#define MOD (1000000007)
//#define eps (1e-6)
#define endl '\n'
#define debug cout<<"debug"<<endl;
using namespace std;
namespace Splay{
#define LC(x) tr[x].s[0]
#define RC(x) tr[x].s[1]
#define fa(x) tr[x].p
const int Tree_MAXN=1e5+10;
struct Node{
int v,cnt,s[2],p,size;
//节点值,重复个数,左右子节点,父节点,子树大小
void newNode(int p1,int v1){
p=p1;
v=v1;
cnt=size=1;
}
};
Node tr[Tree_MAXN];
int root=0;//根节点点编号
int idx=0;//元素个数
void push_up(int x){//维护每个子树的大小
tr[x].size=tr[LC(x)].size+tr[RC(x)].size+tr[x].cnt;//子树大小=左右子树大小+根节点重复个数
}
bool get(int x){
return x==RC(fa(x));//x是否是其父节点的右节点
}
void rotate(int x){
int y=fa(x),z=fa(y),k=get(x);
if(tr[x].s[k^1])//如果c=0(x为左节点,要右旋),那么要操作的就是X的右节点(画图,注意左旋右旋时x都是下面那个节点别弄混了)
tr[tr[x].s[k^1]].p=y;//将操作节点的父节点指向y
//此时x对tr[x].s[c^1]的链接没有断开
tr[y].s[k]=tr[x].s[k^1];//y指向操作节点,接完y和x的操作节点的双向边
tr[x].s[k^1]=y;
tr[y].p=x;
tr[x].p=z;
if(z)
tr[z].s[tr[z].s[1]==y]=x;//不能变x,y子树在z的相对位置
push_up(x),push_up(y);//维护一下子树大小,z的子树大小显然没变不用维护
}
void splay(int x,int k){//旋转到目标底下
while(fa(x)!=k){
int y=fa(x),z=fa(y);
if(z!=k){
((LC(z)==y)^(LC(y)==x))?rotate(x):rotate(y);
}
rotate(x);
}
if(!k){
root=x;
}
}
void insert(int v){
int x=root,p=0;//p:临时存储父节点,因为如果去到0是没法访问这个虚构节点的父节点的
while(x&&v!=tr[x].v){
p=x;
x=tr[x].s[v>tr[x].v];
}
if(x){//重复元素,次数++
++tr[x].cnt;
}
else{//新元素,插入
x=++idx;
tr[p].s[v>tr[p].v]=x;
tr[x].newNode(p,v);
}
splay(x,0);
}
void find(int v){
int x=root;
while(tr[x].s[v>tr[x].v]&&v!=tr[x].v){//必须有点才能搜
x=tr[x].s[v>tr[x].v];
}
splay(x,0);
}
int get_pre(int v){
find(v);
int x=root;
if(tr[x].v<v){//因为如果树中不存在v,find会找到第一个比它大或小的元素并转上根节点,如果根比v小说明根就是前驱
return root;
}
x=tr[x].s[0];//去比自己小的子树
while(tr[x].s[1]){//在比v小的子树中找最大的就是前驱了
x=tr[x].s[1];
}
splay(x,0);
return x;
}
int get_suc(int v){//原理同前驱
find(v);
int x=root;
if(tr[x].v>v){
return root;
}
x=tr[x].s[1];
while(tr[x].s[0]){
x=tr[x].s[0];
}
splay(x,0);
return x;
}
void del(int v){
int pre=get_pre(v);
int suc=get_suc(v);
splay(pre,0);
splay(suc,pre);
int DEL=LC(suc);
/*
把前驱转到根,后继转到根节点下面
那么后继的左子结点就是要删的东西
因为这个子树只能有一个节点(前驱后继的定义就是前面的第一个和后面的第一个)
*/
if(tr[DEL].cnt>1){
--tr[DEL].cnt;
splay(DEL,0);
}
else{
LC(suc)=0;
splay(suc,0);//反正转就对了
}
}
int get_rank(int v){
insert(v);//插入一个防止找不到
find(v);
del(v);
return tr[LC(root)].size;
}
int get_val(int k){
int x=root;
while(1){
int y=LC(x);
if(tr[y].size+tr[x].cnt<k){
k-=tr[y].size+tr[x].cnt;
x=RC(x);
}
else if(tr[y].size>=k){
x=LC(x);
}
else{
break;
}
}
splay(x,0);
return tr[x].v;
}
void splayInit(){
insert(INT32_MAX);
insert(INT32_MIN);
}
}
using namespace Splay;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
splayInit();
int n;
cin>>n;
for(int i=1;i<=n;++i){
int op,val;
cin>>op>>val;
switch (op) {
case 1:
insert(val);
break;
case 2:
del(val);
break;
case 3:
cout<<get_rank(val)<<endl;
break;
case 4:
cout<<get_val(val+1)<<endl;
break;
case 5:
cout<<tr[get_pre(val)].v<<endl;
break;
case 6:
cout<<tr[get_suc(val)].v<<endl;
break;
}
}
return 0;
}FHQ-Treap
又叫非旋Treap,用拆分合并操作代替了传统的旋转 例题:还是那个普通平衡树,但是没写main函数部分
#include <bits/stdc++.h>
//#define int int64_t
//#define int __int128
//#define MOD (1000000007)
//#define eps (1e-6)
#define endl '\n'
#define debug cout<<"debug"<<endl;
using namespace std;
namespace FHQ_Treap{//小根堆非旋treap
#define lc tr[p].l
#define rc tr[p].r
const int Tree_MAXN=2e5+10;
struct Node{
int l,r;//左右子节点
int val;//权值,符合二叉搜索树
int key;//键值,符合堆,随机复制保证树的平衡
int siz;//子树大小
}tr[Tree_MAXN];
int n,root,idx;//n个点,根节点编号,当前编号
int newnode(int v){//构造新节点
tr[++idx].val=v;
tr[idx].key=rand();
tr[idx].siz=1;
return idx;
}
void push_up(int p){
tr[p].siz=tr[lc].siz+tr[rc].siz+1;
}
void split(int p,int v,int &x,int &y){//按值分裂,注意传引用
if(!p){//没有分裂的地方
x=y=0;
return ;
}
if(tr[p].val<=v){
/*
根节点比v小,那么左子树全都比v小,应该去右边继续找
*/
x=p;//将左树上一个遍历的点的右边连到p
split(rc,v,rc,y);
}
else{//同理
y=p;
split(lc,v,x,lc);
}
push_up(p);
}
int merge(int x,int y){//合并两棵树
if(!x||!y){//有空树
return x+y;//返回那个非空树
}
if(tr[x].key<tr[y].key){//小跟堆,x_key<y_key时x为y父节点
tr[x].r=merge(tr[x].r,y);
push_up(x);
return x;
}
else{
tr[y].l=merge(x,tr[y].l);
push_up(y);
return y;
}
}
void insert(int v){//插入
int x,y,z;
split(root,v,x,y);//根据要插入的值裂开
z=newnode(v);
root=merge(merge(x,z),y);//缝合树,注意不能写反,原来的右树不可以去左边
}
void del(int v){//只删一个v
int x,y,z;
split(root,v,x,z);//裂开后v在x树上
split(root,v-1,x,y);//裂开后vy即为所有x
y=merge(tr[y].l,tr[y].r);//因为只删一个,把最上层的v删掉,合并其左右子树
root=merge(merge(x,y),z);
}
void delall(int v){//删除全部的v
int x,y,z;
split(root,v,x,z);//裂开后v在x树上
split(root,v-1,x,y);//裂开后y即为所有v
root=merge(x,z);//不管y了即为删去所有v
}
int get_k(int p,int k){
if(k<=tr[lc].siz){
return get_k(lc,k);
}
if(k==tr[lc].siz+1){
return p;
}
return get_k(rc,k-tr[lc].siz-1);
}
int get_pre(int v){
int x,y;
split(root,v,x,y);//分裂出比v小的左树
int p=get_k(x,tr[x].siz);//获取左树最大值
root=merge(x,y);//合并回去
return tr[p].val;
}
int get_suc(int v){
int x,y;
split(root,v,x,y);//分裂出比v大的右树
int p=get_k(y,1);//获取右树最小值
root=merge(x,y);//合并回去
return tr[p].val;
}
}
signed main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
return 0;
}