#include <memory>//智能指针头文件
#include <random>//随机数头文件
#include <iostream>
#include <algorithm>
#include <ctime>//time头文件 template<class T>
struct node
{
T key;
unsigned weight;
std::shared_ptr<node<T>> lchild, rchild, parent;
node(T key, unsigned weight, std::shared_ptr<node<T>> lchild = nullptr,
std::shared_ptr<node<T>> rchild = nullptr, std::shared_ptr<node<T>> parent = nullptr):
key(key)
{
this->weight = weight;
this->lchild = lchild;
this->rchild = rchild;
this->parent = parent;
}
}; template<class T>
class Treap
{
private:
std::shared_ptr<node<T>> root;
std::default_random_engine generator;
std::uniform_int_distribution<int> dis;
void left_rotation(std::shared_ptr<node<T>> a)
{
if (a == nullptr) return; std::shared_ptr<node<T>> b = a->lchild;
if (b == nullptr) return; if (b->rchild != nullptr)
{
a->lchild = b->rchild;
b->rchild->parent = a;
}
else
a->lchild = nullptr;// b->parent = a->parent;
if (a->parent == nullptr)
root = b;
else
{
if (a->parent->lchild == a)
a->parent->lchild = b;
else
a->parent->rchild = b;
} b->rchild = a;
a->parent = b;
}
void right_rotation(std::shared_ptr<node<T>> a)
{
if (a == nullptr) return; std::shared_ptr<node<T>> b = a->rchild;
if (b == nullptr) return; if (b->lchild != nullptr)
{
a->rchild = b->lchild;
b->lchild->parent = a;
}
else
a->rchild = nullptr; b->parent = a->parent;
if (a->parent == nullptr)
root = b;
else
{
if (a->parent->lchild == a)
a->parent->lchild = b;
else
a->parent->rchild = b;
} b->lchild = a;
a->parent = b;
}
public:
Treap() :generator(time(nullptr)), dis(, )
{
root = nullptr;
}
void insert(T key)
{
std::shared_ptr<node<T>> tmp = std::make_shared<node<T>>(key, dis(generator));
std::shared_ptr<node<T>> ptr = root;
if (ptr == nullptr)
{
root = tmp;
return;
} while (true)
{
if (key <= ptr->key)
{
if (ptr->lchild == nullptr) break;
ptr = ptr->lchild;
}
else
{
if (ptr->rchild == nullptr) break;
ptr = ptr->rchild;
}
} if (key <= ptr->key)
ptr->lchild = tmp;
else
ptr->rchild = tmp;
tmp->parent = ptr; while (ptr != nullptr)
{
if (tmp->weight < ptr->weight)
{
if (ptr->lchild == tmp)
{
left_rotation(ptr);
ptr = tmp->parent;
}
else
{
right_rotation(ptr);
ptr = tmp->parent;
}
}
else
break;
}
}
void earse(T key)
{
std::shared_ptr<node<T>> ptr = root;
while (ptr != nullptr)
{
if (key == ptr->key)
break;
else if (key < ptr->key)
ptr = ptr->lchild;
else
ptr = ptr->rchild;
}
if (ptr == nullptr) return;
while (true)
{
if (ptr->lchild == nullptr || ptr->rchild == nullptr)
{
if (ptr->lchild == nullptr && ptr->rchild == nullptr)
{
if (ptr->parent == nullptr) root = nullptr;
else
{
if (ptr->parent->lchild == ptr)
ptr->parent->lchild = nullptr;
else
ptr->parent->rchild = nullptr;
}
}
else if (ptr->lchild == nullptr)
{
if (ptr->parent == nullptr)
root = ptr->rchild;
else
{
if (ptr->parent->lchild == ptr)
ptr->parent->lchild = ptr->rchild;
else
ptr->parent->rchild = ptr->rchild;
}
ptr->rchild->parent = ptr->parent;
}
else
{
if (ptr->parent == nullptr)
root = ptr->lchild;
else
{
if (ptr->parent->lchild == ptr)
ptr->parent->lchild = ptr->lchild;
else
ptr->parent->rchild = ptr->lchild;
}
ptr->lchild->parent = ptr->parent;
}
return;
}
else
{
if (ptr->lchild->weight <= ptr->rchild->weight)
left_rotation(ptr);
else
right_rotation(ptr);
}
}
}
};

最新文章

  1. 【bzoj4008】 HNOI2015—亚瑟王
  2. HDU 1005 F(Contest #1)
  3. Guava学习笔记:EventBus
  4. POJ 3641 快速幂+素数
  5. asp.net获取文件夹下的所有文件
  6. hdu 1242 dfs/bfs
  7. intelij idea
  8. 用sql语句按周、按月、按季、按年统
  9. ORACLE11.2.0 SQLPLUS 报 error while loading shared libraries
  10. ocx控件避免弹出警告的类--2
  11. [Dev Blog] KCV插件 —— Provissy Tools 。
  12. HDU1300 Pearls
  13. python基础——元组
  14. 伪类选择器 E:nth-child(n)、E:nth-of-type(n)
  15. OpenWrt启动过程分析+添加自启动脚本【转】
  16. jQuery对象的操作
  17. 【poj3415】 Common Substrings
  18. docker网络之macvlan
  19. pytest文档9-参数化parametrize
  20. Groovy学习()Groovy是啥?

热门文章

  1. 用Python爬取影视网站,直接解析播放地址。
  2. dd---复制文件并对原文件的内容进行转换和格式化处理
  3. python3 geohash 导入错误及解决
  4. 洛谷 P2014 选课 && caioj 1108 树形动态规划(TreeDP)3:选课
  5. 关于memset赋最值
  6. 紫书 习题 10-8 UVa 10622(gcd)
  7. 《机器学习系统设计》之应用scikit-learn做文本分类(上)
  8. ios svn repository
  9. mvc下是如何传值的
  10. 如何利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例