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 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <iostream>
33# include <string>
34# include <ctime>
35# include <aleph.H>
36# include <tpl_dynArray.H>
37# include <tpl_sort_utils.H>
38# include <tpl_treap.H>
39# include <tpl_binNodeUtils.H>
40
41# include <cstdlib>
42# include <cassert>
43using namespace std;
44# include <cstdlib>
45# include <cassert>
46using namespace Aleph;
47
49
51{
52 while (true)
53 {
54 // entre 1 y 1000
55 unsigned long r = 1+ (int) (1000.0*rand()/(RAND_MAX+1.0));
56
57 if (sequential_search(rand_sequence, r, 0, rand_sequence.size() - 1) ==
58 -1)
59 {
61
62 return r;
63 }
64 }
65}
66
68{
69 cout << endl
70 << "Secuencia aleatorios: ";
71 for (int i = 0; i < rand_sequence.size(); i++)
72 cout << " " << (long) rand_sequence[i];
73
74 cout << endl;
75
77}
78
79void printNode(Treap<int>::Node *node, int, int)
80{
81 cout << node->get_key() << " ";
82}
83
84void printNode(Treap<int>::Node *node, int, bool)
85{
86 cout << node->get_key() << " ";
87}
88
89void printPrio(Treap<int>::Node *node, int, int)
90{
91 cout << node->getPriority() << " ";
92}
93
94void printPair(Treap<int>::Node *node, int, int)
95{
96 cout << "(" << node->get_key() << "," << node->getPriority() << ") ";
97}
98
99int main(int argc, char *argv[])
100{
101 int n = 10;
102 unsigned int t = std::time(0);
103 int value;
104
105 try
106 {
107 if (argc > 1)
108 n = std::stoi(argv[1]);
109
110 if (argc > 2)
111 t = std::stoi(argv[2]);
112 }
113 catch (...)
114 {
115 // ignore
116 }
117
118 if (n <= 0)
119 {
120 cout << "n must be positive" << endl;
121 return 1;
122 }
123
124 srand(t);
125
126 cout << "testTreapRec " << n << " " << t << endl;
127
128 Treap<int> tree;
129 Treap<int>::Node *node;
130 int i;
131
132 cout << "Inserting " << n << " random values in treee ...\n";
133
134 for (i = 0; i < n; i++)
135 {
136 do
137 {
138 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
139 node = tree.search(value);
140 }
141 while (node not_eq NULL);
142
143 node = new Treap<int>::Node (value);
144 tree.insert(node);
145 cout << "(" << value << "," << PRIO(node) << ") ";
146 }
147
148 cout << endl << endl
149 << "level order" << endl;
150
152 {
153 cout << p->get_key() << " ";
154 return true;
155 });
156
157 assert(is_treap(tree.getRoot()));
158
159 cout << endl << endl
160 << "Preorden" << endl;
161
163
164 cout << endl << endl;
165
166 cout << "inorden prio" << endl;
168 cout << endl << endl;
169
171
172 cout << endl << endl;
173
174
175 cout << endl << endl
176 << "Preorden prio" << endl;
177
179
180 cout << endl << endl;
181
182 cout << "inorden prio" << endl;
184 cout << endl << endl;
185
186 // print_aleatorio_and_reset_dynarray();
187
188 cout << endl << endl;
189
190 for (i = 0; i < n/2; i++)
191 {
192 do
193 {
194 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
195 node = tree.remove(value);
196 }
197 while (node == NULL);
198
199 cout << value << " ";
200 delete node;
201 }
202
203 cout << endl << "verifying Treap after deletions ... "
204 << endl;
205 assert(is_treap(tree.getRoot()));
206 cout << " done" << endl;
207
208 cout << "Preorden" << endl;
210 cout << endl;
211
212 cout << "inorden prio" << endl;
214 cout << endl;
215
216 cout << "The path length is " << internal_path_length(tree.getRoot())
217 << endl;
218
219 destroyRec(tree.getRoot());
220
221 cout << endl << "testTreapRec " << n << " " << t << endl;
222}
223
224
225
226
227
228
229
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
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
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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
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:50
void printPair(Treap< int >::Node *node, int, int)
Definition testTreap.C:94
void printPrio(Treap< int >::Node *node, int, int)
Definition testTreap.C:89
void print_aleatorio_and_reset_dynarray()
Definition testTreap.C:67
DynArray< unsigned long > rand_sequence
Definition testTreap.C:48
void printNode(Treap< int >::Node *node, int, int)
Definition testTreap.C:79
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.