forked from chittaranjan-rath/Prim-Implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathveb.hpp
More file actions
257 lines (226 loc) · 11.4 KB
/
veb.hpp
File metadata and controls
257 lines (226 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*******************************************************************************
* Copyright (C) 2016 Dominik Dragoun *
* *
* This file is part of my Van Emde Boas tree data structure implementation in *
* C/C++. It is released under MIT License, which should be distributed with *
* this file. It is also avaible at <https://opensource.org/licenses/MIT>. *
*******************************************************************************/
/***************************************************************************//**
* @file veb.h
*
* @brief File containing declarations of a class implementing Van Emde
* Boas tree data structure.
* @author Dominik Dragoun (dominik@dragoun.com)
* @date June, 2016
* @copyright Copyright (C) 2016 Dominik Dragoun.
* @license This project is released undes the MIT License.
******************************************************************************/
#ifndef __VEB_H_458976543568798867538649758687654752463747856374562543646__
#define __VEB_H_458976543568798867538649758687654752463747856374562543646__
#include <cmath>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// #define DEBUG
#define DEBUG_OS std::cout
#define DEBUG_OS_ENDL std::endl
#define UNDEFINED INT_MIN
/***************************************************************************//**
* @brief Struct containing the Van Emde Boas tree.
*
* @details It is a data structure which implements an associative array with
* m-bit integer keys. It supports operations of ordered associative
* array (Insert, Delete and Find) and two more order opperations -
* Find Next (Succ) and Find Previous (Pred). All these operations
* are performed in O(log m) time, or equivalently in O(log log M)
* time, where M = 2^m is the maximum number of elements that can be
* stored in the tree. It also supports the operation Minimum (Min)
* and Maximum (Max), wich returns the minimum and maximum element
* stored in the tree respectively. These both run in O(1) time,
* since the minimum and maximum element are stored as attributes in
* each tree.
******************************************************************************/
struct TvEB
{
/*************************************************************************//**
* @brief Constructor.
*
* @param[in] uniSize The size of the tree universe
****************************************************************************/
TvEB ( int uniSize );
/*************************************************************************//**
* @brief Destructor.
****************************************************************************/
~TvEB();
/*************************************************************************//**
* @brief The size of the universe.
****************************************************************************/
const int uni;
/*************************************************************************//**
* @brief The square root of the universe size.
****************************************************************************/
const int uniSqrt;
/*************************************************************************//**
* @brief The lower square root of the universe size.
****************************************************************************/
const int lowerUniSqrt;
/*************************************************************************//**
* @brief The higher square root of the universe size.
****************************************************************************/
const int higherUniSqrt;
/*************************************************************************//**
* @brief The minimal value in the tree.
****************************************************************************/
int min;
/*************************************************************************//**
* @brief The maximal value in the tree.
****************************************************************************/
int max;
/*************************************************************************//**
* @brief The pointer to the summary structure of the tree.
****************************************************************************/
TvEB * summary;
/*************************************************************************//**
* @brief The pointer to the array of clusters of the tree.
****************************************************************************/
TvEB ** cluster;
};
/***************************************************************************//**
* @brief Rounds up the given value to the next higher power of two.
*
* @param[in] val The value to round up.
*
* @return The higher power of two from the given value.
******************************************************************************/
int powTwoRoundUp ( int val );
/***************************************************************************//**
* @brief Returns the lower square root of the given value, which is 2^(
* floor ( log_2 ( value ) / 2 ) ).
*
* @param[in] val The value from which the lower square root is calculated.
*
* @return The lower square root of the value.
******************************************************************************/
float lowerSqrt ( int val );
/***************************************************************************//**
* @brief Returns the higher square root of the given value, which is 2^(
* ceil ( log_2 ( value ) / 2 ) ).
*
* @param[in] val The value from which the higher square root is calculated.
*
* @return The higher square root of the value.
******************************************************************************/
float higherSqrt ( int val );
/***************************************************************************//**
* @brief Returns the element's index in the cluster.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] val The value of the element.
*
* @return The element's index in the cluster.
******************************************************************************/
int low ( TvEB * tree, int val );
/***************************************************************************//**
* @brief Returns the index of the element's cluster.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] val The value of the element.
*
* @return The index of the element's cluster.
******************************************************************************/
int high ( TvEB * tree, int val );
/***************************************************************************//**
* @brief Returns the value on index low in the cluster high.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] high The cluster index.
* @param[in] low The index of the element in the cluster.
*
* @return The value of the element.
******************************************************************************/
int index ( TvEB * tree, int high, int low );
/***************************************************************************//**
* @brief Finds the lowest value stored in the given tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[out] res The lowest element.
*
* @retval true Successfully found the minimum.
* @retval false Failed to found the minimum.
******************************************************************************/
bool vEB_min ( TvEB * tree, int & res );
/***************************************************************************//**
* @brief Finds the highest value stored in the given tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[out] res The highest element.
*
* @retval true Successfully found the maximum.
* @retval false Failed to found the maximum.
******************************************************************************/
bool vEB_max ( TvEB * tree, int & res );
/***************************************************************************//**
* @brief Inserts the given value into the given vEB tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] val The value of the element to insert.
*
* @retval true Successfully inserted the value.
* @retval false Failed to insert the value.
******************************************************************************/
bool vEB_insert ( TvEB *& tree, int val, int parentUniSqrt = 65536 );
/***************************************************************************//**
* @brief Removes the given value from the given vEB tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] val The value of the element to remove.
*
* @retval true Successfully removed the value.
* @retval false Failed to remove the value.
******************************************************************************/
bool vEB_delete ( TvEB *& tree, int val );
/***************************************************************************//**
* @brief Finds if the given value is in the given vEB tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] val The value of the element to find.
*
* @retval true Successfully found the element.
* @retval false Failed to found the element.
******************************************************************************/
bool vEB_find ( TvEB * tree, int val );
/***************************************************************************//**
* @brief Finds the smallest value at least the given value in the given
* tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] val The lower bound for the value of the sought element
* @param[out] res The found element.
*
* @retval true Successfully found the successor.
* @retval false Failed to found the successor.
******************************************************************************/
bool vEB_succ ( TvEB * tree, int val, int & res );
/***************************************************************************//**
* @brief Finds the largest value at most the given value in the given
* tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param[in] val The upper bound for the value of the sought element
* @param[out] res The found element.
*
* @retval true Successfully found the predecessor.
* @retval false Failed to found the predecessor.
******************************************************************************/
bool vEB_pred ( TvEB * tree, int val, int & res );
/***************************************************************************//**
* @brief Prints pointer values of the given tree.
*
* @param[in] tree The pointer to the van Emde Boas tree.
* @param os The output stream in which the values are printed.
******************************************************************************/
void vEB_print ( TvEB * tree );
#endif /* __VEB_H_458976543568798867538649758687654752463747856374562543646__ */