TheAlgorithms/C++ 1.0.0
All the 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.

Definition at line 40 of file treap.cpp.

Constructor & Destructor Documentation

◆ Treap()

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

Initialization.

Definition at line 55 of file treap.cpp.

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

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

Definition at line 112 of file treap.cpp.

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

◆ _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

Definition at line 139 of file treap.cpp.

139 {
140 if (k <= size[childs[x][0]]) {
141 return _get_k_th(childs[x][0], k);
142 }
143 k -= size[childs[x][0]] + cnt[x];
144 if (k <= 0) {
145 return key[x];
146 }
147 return _get_k_th(childs[x][1], k);
148 }
double k(double x)
Another test function.
int _get_k_th(int &x, int k)
Find the KTH largest value (internal method)
Definition treap.cpp:139

◆ _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

Definition at line 155 of file treap.cpp.

155 {
156 if (!x) {
157 return 0;
158 }
159 if (k == key[x]) {
160 return size[childs[x][0]] + 1;
161 } else if (k < key[x]) {
162 return _get_rank(childs[x][0], k);
163 } else {
164 return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k);
165 }
166 }
int _get_rank(int x, int k)
Query the rank of specified element (internal method)
Definition treap.cpp:155

◆ _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

Definition at line 85 of file treap.cpp.

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

◆ erase()

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

Erase element (External method)

Parameters
kKey to erase

Definition at line 208 of file treap.cpp.

208{ _erase(root, k); }
int root
root of the treap
Definition treap.cpp:41

◆ 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

Definition at line 214 of file treap.cpp.

214{ return _get_k_th(root, k); }

◆ 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

Definition at line 188 of file treap.cpp.

188 {
189 int x = root, next = -1;
190 while (x) {
191 if (key[x] > k) {
192 next = key[x], x = childs[x][0];
193 } else {
194 x = childs[x][1];
195 }
196 }
197 return next;
198 }

◆ 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

Definition at line 172 of file treap.cpp.

172 {
173 int x = root, pre = -1;
174 while (x) {
175 if (key[x] < k) {
176 pre = key[x], x = childs[x][1];
177 } else {
178 x = childs[x][0];
179 }
180 }
181 return pre;
182 }

◆ 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

Definition at line 220 of file treap.cpp.

220{ return _get_rank(root, k); }

◆ insert()

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

Insert element (External method)

Parameters
kKey to insert

Definition at line 203 of file treap.cpp.

203{ _insert(root, k); }

◆ 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

Definition at line 71 of file treap.cpp.

71 {
72 int y = childs[x][t];
73 childs[x][t] = childs[y][1 - t];
74 childs[y][1 - t] = x;
75 // The rotation will only change itself and its son nodes
76 update(x);
77 update(y);
78 x = y;
79 }

◆ update()

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

Update the subtree size of the node.

Parameters
xThe node to update

Definition at line 63 of file treap.cpp.

63 {
64 size[x] = size[childs[x][0]] + cnt[x] + size[childs[x][1]];
65 }

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

Definition at line 45 of file treap.cpp.

45 {
46 {}};

◆ cnt

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

Maintains the subtree size for ranking query.

Definition at line 49 of file treap.cpp.

50 {};

◆ key

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

Node identifier.

Definition at line 43 of file treap.cpp.

43{};

◆ priority

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

Random priority.

Definition at line 44 of file treap.cpp.

44{};

◆ root

int data_structures::treap::Treap::root = 0

root of the treap

Definition at line 41 of file treap.cpp.

◆ size

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

The number of copies per node.

Definition at line 51 of file treap.cpp.

51{};

◆ treapCnt

int data_structures::treap::Treap::treapCnt = 0

Total number of current nodes in the treap.

Definition at line 42 of file treap.cpp.


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