Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testTreapRk.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 <ctime>
34# include <string>
35# include <aleph.H>
36# include <tpl_dynArray.H>
37# include <tpl_treapRk.H>
38# include <tpl_binNodeUtils.H>
39
40# include <cstdlib>
41# include <cassert>
42using namespace std;
43# include <cstdlib>
44# include <cassert>
45using namespace Aleph;
46
47
48void printNode(Treap_Rk<int>::Node *node, int, int)
49{
50 cout << node->get_key() << " ";
51}
52
53void printPrio(Treap_Rk<int>::Node *node, int, int)
54{
55 cout << node->getPriority() << " ";
56}
57
58
59int main(int argc, char *argv[])
60{
61 int n = 10;
62 if (argc > 1)
63 {
64 try { n = stoi(argv[1]); } catch (...) { n = 10; }
65 }
66
67 if (n <= 0)
68 {
69 cerr << "n must be positive" << endl;
70 return 1;
71 }
72
73 unsigned int t = std::time(0);
74 if (argc > 2)
75 {
76 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
77 }
78
79 srand(t);
80
81 cout << "testTreap_Rk " << n << " " << t << endl;
82
84 Treap_Rk<int> tree;
86 int i, value;
87
88 cout << "Inserting " << n << " random values in tree ...\n";
89
90 for (i = 0; i < n; i++)
91 {
92 do
93 {
94 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
95 node = tree.search(value);
96 }
97 while (node not_eq NULL);
98
99 cout << value << " ";
100 node = new Treap_Rk<int>::Node (value);
101 tree.insert(node);
102 keys.append(value);
103 }
104
105 assert(is_treap(tree.getRoot()));
106
108
109 cout << endl << endl
110 << "Preorden" << endl;
111
113
114 cout << endl << endl;
115
116 cout << "inorden prio" << endl;
118 cout << endl << endl;
119
120 for (i = 0; i < n; i++)
121 {
122 node = tree.select(i);
123 cout << node->get_key() << " ";
124 }
125
126 cout << endl << endl;
127
128 cout << "Lista de posiciones infijas" << endl;
129
130 for (i = 0; i < n; ++i)
131 {
132 std::pair<int,Treap_Rk<int>::Node*> pos = tree.position(keys[i]);
133 cout << keys[i] << "<-->" << pos.first << endl;
134 }
135
136 cout << endl << endl;
137
138 for (i = 0; i < n/2; i++)
139 {
140 value = keys[i];
141 node = tree.remove(value);
142 assert(node != NULL);
143
144 cout << value << " ";
145 delete node;
146 }
147
148 cout << endl << "verifying Treap_Rk after deletions ... "
149 << endl;
150 assert(is_treap(tree.getRoot()));
152 cout << " done" << endl;
153
154 cout << "Preorden" << endl;
156 cout << endl;
157
158 cout << "inorden prio" << endl;
160 cout << endl;
161
162 cout << "The path length is " << internal_path_length(tree.getRoot())
163 << endl;
164
165 cout << "Recorrido por iterador" << endl;
166 for (Treap_Rk<int>::Iterator it(tree); it.has_curr(); it.next())
167 cout << KEY(it.get_curr()) << " ";
168 cout << endl;
169
170 n = tree.size();
171 size_t beg = n/4;
172 size_t end = 3*n/4;
173 cout << "Eliminacion de rango [" << beg << " .. " << end << "]" << endl;
175
176 assert(is_treap(tree.getRoot()));
178
179 // Verify rank metadata is correctly updated after removal
182
183 cout << "Arbol restante" << endl;
185 cout << endl;
186
187 cout << "Arbol eliminado" << endl;
189 cout << endl;
190
192 destroyRec(tree.getRoot()); // leaves the root as Node::NullPtr (sentinel)
193
194 cout << endl << "testTreap_Rk " << n << " " << t << endl;
195}
196
197
198
199
200
201
202
Core header for the Aleph-w library.
int main()
@ KEY
Definition btreepic.C:169
size_t size_t int32_t value
Definition ca-c-api.h:116
Node *& getRoot() noexcept
Return the tree's root.
Node * select(const size_t i) const
Return the i-th node in order sense.
size_t size() const noexcept
Return the number of nodes contained in the tree.
Node * search(const Key &key) const noexcept
Search a key in a treap.
Node * remove(Node *&root, const Key &key) noexcept
bool insert(Node *&root, Node *p) noexcept
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 check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
std::pair< int, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
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
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
STL namespace.
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
int keys[]
void printNode(Treap_Rk< int >::Node *node, int, int)
Definition testTreapRk.C:48
void printPrio(Treap_Rk< int >::Node *node, int, int)
Definition testTreapRk.C:53
Utility functions for binary tree operations.
Lazy and scalable dynamic array implementation.
Treap with rank (order statistics).