Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testTreap.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <iostream>
28# include <string>
29# include <ctime>
30# include <aleph.H>
31# include <tpl_dynArray.H>
32# include <tpl_sort_utils.H>
33# include <tpl_treap.H>
34# include <tpl_binNodeUtils.H>
35
36# include <cstdlib>
37# include <cassert>
38using namespace std;
39# include <cstdlib>
40# include <cassert>
41using namespace Aleph;
42
44
46{
47 while (true)
48 {
49 // entre 1 y 1000
50 unsigned long r = 1+ (int) (1000.0*rand()/(RAND_MAX+1.0));
51
52 if (sequential_search(rand_sequence, r, 0, rand_sequence.size() - 1) ==
53 -1)
54 {
56
57 return r;
58 }
59 }
60}
61
63{
64 cout << endl
65 << "Secuencia aleatorios: ";
66 for (int i = 0; i < rand_sequence.size(); i++)
67 cout << " " << (long) rand_sequence[i];
68
69 cout << endl;
70
72}
73
74void printNode(Treap<int>::Node *node, int, int)
75{
76 cout << node->get_key() << " ";
77}
78
79void printNode(Treap<int>::Node *node, int, bool)
80{
81 cout << node->get_key() << " ";
82}
83
84void printPrio(Treap<int>::Node *node, int, int)
85{
86 cout << node->getPriority() << " ";
87}
88
89void printPair(Treap<int>::Node *node, int, int)
90{
91 cout << "(" << node->get_key() << "," << node->getPriority() << ") ";
92}
93
94int main(int argc, char *argv[])
95{
96 int n = 10;
97 unsigned int t = std::time(0);
98 int value;
99
100 try
101 {
102 if (argc > 1)
103 n = std::stoi(argv[1]);
104
105 if (argc > 2)
106 t = std::stoi(argv[2]);
107 }
108 catch (...)
109 {
110 // ignore
111 }
112
113 if (n <= 0)
114 {
115 cout << "n must be positive" << endl;
116 return 1;
117 }
118
119 srand(t);
120
121 cout << "testTreapRec " << n << " " << t << endl;
122
123 Treap<int> tree;
124 Treap<int>::Node *node;
125 int i;
126
127 cout << "Inserting " << n << " random values in treee ...\n";
128
129 for (i = 0; i < n; i++)
130 {
131 do
132 {
133 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
134 node = tree.search(value);
135 }
136 while (node not_eq NULL);
137
138 node = new Treap<int>::Node (value);
139 tree.insert(node);
140 cout << "(" << value << "," << PRIO(node) << ") ";
141 }
142
143 cout << endl << endl
144 << "level order" << endl;
145
147 {
148 cout << p->get_key() << " ";
149 return true;
150 });
151
152 assert(is_treap(tree.getRoot()));
153
154 cout << endl << endl
155 << "Preorden" << endl;
156
158
159 cout << endl << endl;
160
161 cout << "inorden prio" << endl;
163 cout << endl << endl;
164
166
167 cout << endl << endl;
168
169
170 cout << endl << endl
171 << "Preorden prio" << endl;
172
174
175 cout << endl << endl;
176
177 cout << "inorden prio" << endl;
179 cout << endl << endl;
180
181 // print_aleatorio_and_reset_dynarray();
182
183 cout << endl << endl;
184
185 for (i = 0; i < n/2; i++)
186 {
187 do
188 {
189 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
190 node = tree.remove(value);
191 }
192 while (node == NULL);
193
194 cout << value << " ";
195 delete node;
196 }
197
198 cout << endl << "verifying Treap after deletions ... "
199 << endl;
200 assert(is_treap(tree.getRoot()));
201 cout << " done" << endl;
202
203 cout << "Preorden" << endl;
205 cout << endl;
206
207 cout << "inorden prio" << endl;
209 cout << endl;
210
211 cout << "The path length is " << internal_path_length(tree.getRoot())
212 << endl;
213
214 destroyRec(tree.getRoot());
215
216 cout << endl << "testTreapRec " << n << " " << t << endl;
217}
218
219
220
221
222
223
224
Core header for the Aleph-w library.
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
Node * remove(const Key &key) noexcept
Remove a key from the tree.
Definition tpl_treap.H:381
Node * search(const Key &key) const noexcept
Search a key in a treap.
Definition tpl_treap.H:222
Node *& getRoot() noexcept
Return the tree's root.
Definition tpl_treap.H:214
Node * insert(Node *root, Node *p) noexcept
Definition tpl_treap.H:229
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
bool level_traverse(Node *root, Operation &operation)
Level traverse a tree and execute an operation.
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
Definition treapNode.H:120
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
long sequential_search(T *a, const T &x, const long l, const long r, Equal eq=Equal())
Linear search for an element in an array.
STL namespace.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:614
long aleatorio()
Definition testTreap.C:45
void printPair(Treap< int >::Node *node, int, int)
Definition testTreap.C:89
void printPrio(Treap< int >::Node *node, int, int)
Definition testTreap.C:84
void print_aleatorio_and_reset_dynarray()
Definition testTreap.C:62
DynArray< unsigned long > rand_sequence
Definition testTreap.C:43
void printNode(Treap< int >::Node *node, int, int)
Definition testTreap.C:74
gsl_rng * r
Utility functions for binary tree operations.
Lazy and scalable dynamic array implementation.
Comprehensive sorting algorithms and search utilities for Aleph-w.
Treap: randomized BST combining tree and heap properties.