Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
data_structures::treap::Treap Struct Reference

Struct representation of the treap. More...

Collaboration diagram for data_structures::treap::Treap:
[legend]

Public Member Functions

 Treap ()
 Initialization.
 
void update (int x)
 Update the subtree size of the node.
 
void rotate (int &x, int t)
 Rotate without breaking the property of BST.
 
void _insert (int &x, int k)
 Insert a value into the specified subtree (internal method)
 
void _erase (int &x, int k)
 Erase a value from the specified subtree (internal method)
 
int _get_k_th (int &x, int k)
 Find the KTH largest value (internal method)
 
int _get_rank (int x, int k)
 Query the rank of specified element (internal method)
 
int get_predecessor (int k)
 Get the predecessor node of element k.
 
int get_next (int k)
 Get the successor node of element k.
 
void insert (int k)
 Insert element (External method)
 
void erase (int k)
 Erase element (External method)
 
int get_k_th (int k)
 Get the KTH largest value (External method)
 
int get_rank (int k)
 Get the rank of specified element (External method)
 

Public Attributes

int root = 0
 root of the treap
 
int treapCnt = 0
 Total number of current nodes in the treap.
 
std::array< int, maxNodekey = {}
 Node identifier.
 
std::array< int, maxNodepriority = {}
 Random priority.
 
std::array< std::array< int, 2 >, maxNodechilds
 
std::array< int, maxNodecnt
 Maintains the subtree size for ranking query.
 
std::array< int, maxNodesize = {}
 The number of copies per node.
 

Detailed Description

Struct representation of the treap.

Constructor & Destructor Documentation

◆ Treap()

data_structures::treap::Treap::Treap ( )
inline

Initialization.

54 : treapCnt(1) {
55 priority[0] = INT32_MAX;
56 size[0] = 0;
57 }
int treapCnt
Total number of current nodes in the treap.
Definition treap.cpp:41
std::array< int, maxNode > priority
Random priority.
Definition treap.cpp:43
std::array< int, maxNode > size
The number of copies per node.
Definition treap.cpp:50

Member Function Documentation

◆ _erase()

void data_structures::treap::Treap::_erase ( int & x,
int k )
inline

Erase a value from the specified subtree (internal method)

Parameters
xErase from the subtree of node x (Usually x=root)
kKey to erase
111 {
112 if (key[x] == k) {
113 if (cnt[x] > 1) {
114 cnt[x]--;
115 } // If the node has more than one copy, the number of copies --
116 else {
117 if (childs[x][0] == 0 && childs[x][1] == 0) {
118 x = 0;
119 return;
120 } // If there are no children, delete and return
121 // Otherwise, we need to rotate the sons and delete them
122 // recursively
123 int t = (priority[childs[x][0]] > priority[childs[x][1]]);
124 rotate(x, t);
125 _erase(x, k);
126 }
127 } else { // Find the target value based on BST properties
128 _erase(childs[x][key[x] < k], k);
129 }
130 update(x);
131 }
std::array< int, maxNode > key
Node identifier.
Definition treap.cpp:42
void rotate(int &x, int t)
Rotate without breaking the property of BST.
Definition treap.cpp:70
void update(int x)
Update the subtree size of the node.
Definition treap.cpp:62
std::array< std::array< int, 2 >, maxNode > childs
Definition treap.cpp:44
void _erase(int &x, int k)
Erase a value from the specified subtree (internal method)
Definition treap.cpp:111
std::array< int, maxNode > cnt
Maintains the subtree size for ranking query.
Definition treap.cpp:48
Here is the call graph for this function:

◆ _get_k_th()

int data_structures::treap::Treap::_get_k_th ( int & x,
int k )
inline

Find the KTH largest value (internal method)

Parameters
xQuery the subtree of node x (Usually x=root)
kThe queried rank
Returns
The element ranked number k
138 {
139 if (k <= size[childs[x][0]]) {
140 return _get_k_th(childs[x][0], k);
141 }
142 k -= size[childs[x][0]] + cnt[x];
143 if (k <= 0) {
144 return key[x];
145 }
146 return _get_k_th(childs[x][1], k);
147 }
double k(double x)
Another test function.
Definition composite_simpson_rule.cpp:117
int _get_k_th(int &x, int k)
Find the KTH largest value (internal method)
Definition treap.cpp:138
Here is the call graph for this function:

◆ _get_rank()

int data_structures::treap::Treap::_get_rank ( int x,
int k )
inline

Query the rank of specified element (internal method)

Parameters
xQuery the subtree of node x (Usually x=root)
kThe queried element
Returns
The rank of element k
154 {
155 if (!x) {
156 return 0;
157 }
158 if (k == key[x]) {
159 return size[childs[x][0]] + 1;
160 }
161 else if (k < key[x]) {
162 return _get_rank(childs[x][0], k);
163 }
164 else {
165 return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k);
166 }
167 }
int _get_rank(int x, int k)
Query the rank of specified element (internal method)
Definition treap.cpp:154
Here is the call graph for this function:

◆ _insert()

void data_structures::treap::Treap::_insert ( int & x,
int k )
inline

Insert a value into the specified subtree (internal method)

Parameters
xInsert into the subtree of node x (Usually x=root)
kKey to insert
84 {
85 if (x) {
86 if (key[x] == k) {
87 cnt[x]++;
88 } // If the node already exists, the number of copies is ++
89 else {
90 int t = (key[x] < k); // Insert according to BST properties
91 _insert(childs[x][t], k);
92 // After insertion, the heap properties are retained by rotation
93 if (priority[childs[x][t]] < priority[x]) {
94 rotate(x, t);
95 }
96 }
97 } else { // Create a new node
98 x = treapCnt++;
99 key[x] = k;
100 cnt[x] = 1;
101 priority[x] = rand(); // Random priority
102 childs[x][0] = childs[x][1] = 0;
103 }
104 update(x);
105 }
void _insert(int &x, int k)
Insert a value into the specified subtree (internal method)
Definition treap.cpp:84
Here is the call graph for this function:

◆ erase()

void data_structures::treap::Treap::erase ( int k)
inline

Erase element (External method)

Parameters
kKey to erase
209{ _erase(root, k); }
int root
root of the treap
Definition treap.cpp:40
Here is the call graph for this function:

◆ get_k_th()

int data_structures::treap::Treap::get_k_th ( int k)
inline

Get the KTH largest value (External method)

Parameters
kThe queried rank
Returns
The element ranked number x
215{ return _get_k_th(root, k); }
Here is the call graph for this function:

◆ get_next()

int data_structures::treap::Treap::get_next ( int k)
inline

Get the successor node of element k.

Parameters
kThe queried element
Returns
The successor
189 {
190 int x = root, next = -1;
191 while (x) {
192 if (key[x] > k) {
193 next = key[x], x = childs[x][0];
194 } else {
195 x = childs[x][1];
196 }
197 }
198 return next;
199 }
T next(T... args)

◆ get_predecessor()

int data_structures::treap::Treap::get_predecessor ( int k)
inline

Get the predecessor node of element k.

Parameters
kThe queried element
Returns
The predecessor
173 {
174 int x = root, pre = -1;
175 while (x) {
176 if (key[x] < k) {
177 pre = key[x], x = childs[x][1];
178 } else {
179 x = childs[x][0];
180 }
181 }
182 return pre;
183 }

◆ get_rank()

int data_structures::treap::Treap::get_rank ( int k)
inline

Get the rank of specified element (External method)

Parameters
kThe queried element
Returns
The rank of element k
221{ return _get_rank(root, k); }
Here is the call graph for this function:

◆ insert()

void data_structures::treap::Treap::insert ( int k)
inline

Insert element (External method)

Parameters
kKey to insert
204{ _insert(root, k); }
Here is the call graph for this function:

◆ rotate()

void data_structures::treap::Treap::rotate ( int & x,
int t )
inline

Rotate without breaking the property of BST.

Parameters
xThe node to rotate
t0 represent left hand, while 1 right hand
70 {
71 int y = childs[x][t];
72 childs[x][t] = childs[y][1 - t];
73 childs[y][1 - t] = x;
74 // The rotation will only change itself and its son nodes
75 update(x);
76 update(y);
77 x = y;
78 }
Here is the call graph for this function:

◆ update()

void data_structures::treap::Treap::update ( int x)
inline

Update the subtree size of the node.

Parameters
xThe node to update
62 {
63 size[x] = size[childs[x][0]] + cnt[x] + size[childs[x][1]];
64 }

Member Data Documentation

◆ childs

std::array<std::array<int, 2>, maxNode> data_structures::treap::Treap::childs
Initial value:
= {
{}}

[i][0] represents the left child of node i, and [i][1] represents the right

44 {
45 {}}; ///< [i][0] represents the

◆ cnt

std::array<int, maxNode> data_structures::treap::Treap::cnt
Initial value:
=
{}

Maintains the subtree size for ranking query.

49 {}; ///< Maintains the subtree size for ranking query

◆ key

std::array<int, maxNode> data_structures::treap::Treap::key = {}

Node identifier.

42{}; ///< Node identifier

◆ priority

std::array<int, maxNode> data_structures::treap::Treap::priority = {}

Random priority.

43{}; ///< Random priority

◆ size

std::array<int, maxNode> data_structures::treap::Treap::size = {}

The number of copies per node.

50{}; ///< The number of copies per node

The documentation for this struct was generated from the following file: