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 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 <ctime>
29# include <string>
30# include <aleph.H>
31# include <tpl_dynArray.H>
32# include <tpl_treapRk.H>
33# include <tpl_binNodeUtils.H>
34
35# include <cstdlib>
36# include <cassert>
37using namespace std;
38# include <cstdlib>
39# include <cassert>
40using namespace Aleph;
41
42
43void printNode(Treap_Rk<int>::Node *node, int, int)
44{
45 cout << node->get_key() << " ";
46}
47
48void printPrio(Treap_Rk<int>::Node *node, int, int)
49{
50 cout << node->getPriority() << " ";
51}
52
53
54int main(int argc, char *argv[])
55{
56 int n = 10;
57 if (argc > 1)
58 {
59 try { n = stoi(argv[1]); } catch (...) { n = 10; }
60 }
61
62 if (n <= 0)
63 {
64 cerr << "n must be positive" << endl;
65 return 1;
66 }
67
68 unsigned int t = std::time(0);
69 if (argc > 2)
70 {
71 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
72 }
73
74 srand(t);
75
76 cout << "testTreap_Rk " << n << " " << t << endl;
77
79 Treap_Rk<int> tree;
81 int i, value;
82
83 cout << "Inserting " << n << " random values in tree ...\n";
84
85 for (i = 0; i < n; i++)
86 {
87 do
88 {
89 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
90 node = tree.search(value);
91 }
92 while (node not_eq NULL);
93
94 cout << value << " ";
95 node = new Treap_Rk<int>::Node (value);
96 tree.insert(node);
97 keys.append(value);
98 }
99
100 assert(is_treap(tree.getRoot()));
101
103
104 cout << endl << endl
105 << "Preorden" << endl;
106
108
109 cout << endl << endl;
110
111 cout << "inorden prio" << endl;
113 cout << endl << endl;
114
115 for (i = 0; i < n; i++)
116 {
117 node = tree.select(i);
118 cout << node->get_key() << " ";
119 }
120
121 cout << endl << endl;
122
123 cout << "Lista de posiciones infijas" << endl;
124
125 for (i = 0; i < n; ++i)
126 {
127 std::pair<int,Treap_Rk<int>::Node*> pos = tree.position(keys[i]);
128 cout << keys[i] << "<-->" << pos.first << endl;
129 }
130
131 cout << endl << endl;
132
133 for (i = 0; i < n/2; i++)
134 {
135 value = keys[i];
136 node = tree.remove(value);
137 assert(node != NULL);
138
139 cout << value << " ";
140 delete node;
141 }
142
143 cout << endl << "verifying Treap_Rk after deletions ... "
144 << endl;
145 assert(is_treap(tree.getRoot()));
147 cout << " done" << endl;
148
149 cout << "Preorden" << endl;
151 cout << endl;
152
153 cout << "inorden prio" << endl;
155 cout << endl;
156
157 cout << "The path length is " << internal_path_length(tree.getRoot())
158 << endl;
159
160 cout << "Recorrido por iterador" << endl;
161 for (Treap_Rk<int>::Iterator it(tree); it.has_curr(); it.next())
162 cout << KEY(it.get_curr()) << " ";
163 cout << endl;
164
165 n = tree.size();
166 size_t beg = n/4;
167 size_t end = 3*n/4;
168 cout << "Eliminacion de rango [" << beg << " .. " << end << "]" << endl;
170
171 assert(is_treap(tree.getRoot()));
173
174 // Verify rank metadata is correctly updated after removal
177
178 cout << "Arbol restante" << endl;
180 cout << endl;
181
182 cout << "Arbol eliminado" << endl;
184 cout << endl;
185
187 destroyRec(tree.getRoot());
188 tree.getRoot() = nullptr;
189
190 cout << endl << "testTreap_Rk " << n << " " << t << endl;
191}
192
193
194
195
196
197
198
Core header for the Aleph-w library.
@ KEY
Definition btreepic.C:169
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
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
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.
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:43
void printPrio(Treap_Rk< int >::Node *node, int, int)
Definition testTreapRk.C:48
Utility functions for binary tree operations.
Lazy and scalable dynamic array implementation.
Treap with rank (order statistics).