随笔分类 - 二叉搜索树BST,平衡树
数据结构
摘要:P3369 【模板】普通平衡树 方法一: //avl数组版 #include<bits/stdc++.h> using namespace std; const int maxn=100010; struct avlnode{ int val; int size; int cnt; int heig
阅读全文
摘要:P3391 【模板】文艺平衡树 //非旋转treap #include<bits/stdc++.h> using namespace std; const int N=10e5+5; int ch[N][2],tag[N],val[N],siz[N],key[N]; int tn,root,n,m;
阅读全文
摘要:P3369 【模板】普通平衡树 //AVL树题解 #include<bits/stdc++.h> using namespace std; typedef struct AVLTreeNode{ int key;//键值 int height;//树的高度 int size;//树的大小 int c
阅读全文
摘要:P6136 【模板】普通平衡树(数据加强版) //非旋转的Treap树 #include<bits/stdc++.h> using namespace std; const int INF=0x7fffffff; struct Node *nil; // 自定义的空指针,防止翻车(RE) struc
阅读全文
摘要:核心操作:分裂与合并1.分裂(split)有两种:按权值分裂与按位置(排名)分裂如果按权值分裂(split):那么分出来两棵树的第一棵树上的每个数的大小都小于(或者小于等于,视具体情况)x。如果按位置分裂(split):那么分出来两棵树的第一棵树上恰好有x个结点。 //按权值分裂 inline vo
阅读全文
摘要:P1177 【模板】快速排序 #include<bits/stdc++.h> using namespace std; typedef struct AVLTreeNode{ int key; int height; AVLTreeNode* left; AVLTreeNode* right; AV
阅读全文
摘要:P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G #include<bits/stdc++.h> using namespace std; typedef struct AVLTreeNode{ int key; int height;
阅读全文
摘要:P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G这是一道贪心算法的题目,用BST维护有序性,每次取最小值,并删除,取两次合并为新的一堆,进行n-1次,这样就合并为一堆了统计过程每一次的大小。指针版 #include<bits/stdc++
阅读全文
摘要:P3391 【模板】文艺平衡树splay解法1 #include <bits/stdc++.h> using namespace std; const int N = 100005; int ch[N][2], par[N], val[N], cnt[N], size[N], rev[N], roo
阅读全文
摘要:P3369 【模板】普通平衡树 #include <bits/stdc++.h> using namespace std; const int N = 200005; int ch[N][2], par[N], val[N], cnt[N], size[N], ncnt, root; bool ch
阅读全文
摘要:P3369 【模板】普通平衡树 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define lc ch[p][0] #define rc ch[p][1] using namespace std;
阅读全文
摘要:数据结构定义 typedef struct BITNode{ int data; int bf; int cnt; struct BITNode *lchild,*rchild; }BITNode,*BITree; 左旋 void L_Rotate(BITree &T) { BITree tmp;
阅读全文
摘要:数据结构定义 struct node{ int data;//结点的内容 int left;//左子树 int right;//右子树 int cnt; int size; } Bst[100100]; queue<int> sq; int root=0; int tot=0; 更新子树大小:等于左
阅读全文
摘要:#include<iostream> #include<cstdio> using namespace std; const int Maxn=10000000; int tr[2*Maxn+10]; int lowbit(int x) { return x&(-x); } void add(int
阅读全文
摘要:#include<bits/stdc++.h> #define INF INT_MAX using namespace std; const int maxn=100010; int sum=0,R=0; int sz[maxn],v[maxn],num[maxn],rd[maxn],son[max
阅读全文
摘要:替罪羊树——简单粗暴的数据结构正题如果在一棵平衡的二叉搜索树内进行查询等操作,时间就可以稳定在log(n),但是每一次的插入节点和删除节点,都可能会使得这棵树不平衡,最坏情况就是退化成一条链,显然我们不想要这种树,于是各种维护的方法出现了,大部分的平衡树都是通过旋转来维护平衡的,但替罪羊树就很厉害了
阅读全文
摘要:Splay简易教程 百度百科伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(log n)内完成插入、查找和删除操作。它由丹尼尔·斯立特Daniel Sleator 和 罗伯特·恩卓·塔扬Robert Endre Tarjan 在1985年发明的。在伸展树上的一般操作都基于伸展
阅读全文
摘要:AVL树在计算机科学中,AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。AVL树得名于它的发明者G. M. Adelson-Velsky和E. M. Landis,他们在1962
阅读全文
摘要:红黑树红黑树在AVL树“适度平衡”的基础上,进一步放宽条件:红黑树的左右子树高度差绝对值不超过两倍。红黑树也是一种平衡二叉搜索树,虽然和AVL树一样,查找、插入和删除的时间复杂度均为O(logn),但是其统计性能更好一些,任何不平衡都可以在3次旋转之内解决。因此红黑树被广泛应用,例如在C++ STL
阅读全文
摘要:1. 概述AVL树是最早提出的自平衡二叉树,在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。AVL树得名于它的发明者G.M. Adelson-Velsky和E.M. Landis。AVL树种查找、插入和删除在平均和最坏情况下都是O(log n),增加和删除可能需要通过一次
阅读全文