Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testSplayRk1.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
28#include <iostream>
29#include <cstdlib>
30#include <cstdint>
31#include <ctime>
32#include <random>
33#include <cerrno>
34#include <climits>
35#include <tpl_splay_treeRk.H>
36#include <tpl_binNodeXt.H>
37
38using namespace std;
39
40int
41main (int argc, char *argv[])
42{
43 int n = 1000;
44 if (argc > 1)
45 {
46 char *endptr = nullptr;
47 errno = 0;
48 const long parsed_n = strtol (argv[1], &endptr, 10);
49 if (errno != 0 or endptr == argv[1] or *endptr != '\0' or
51 {
52 cerr << "Invalid value for n: " << argv[1] << " (must be between 1 and 1,000,000)" << endl;
53 exit (1);
54 }
55 n = static_cast<int>(parsed_n);
56 }
57
58 auto t = static_cast<unsigned int> (time (nullptr));
59 if (argc > 2)
60 {
61 if (argv[2][0] == '-')
62 {
63 cerr << "Invalid value for t (must be non-negative): " << argv[2] << endl;
64 exit(1);
65 }
66 char *endptr = nullptr;
67 errno = 0;
68 const unsigned long parsed_t = strtoul (argv[2], &endptr, 10);
69 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or parsed_t > UINT_MAX)
70 {
71 cerr << "Invalid value for t: " << argv[2] << endl;
72 exit (1);
73 }
74 t = static_cast<unsigned int>(parsed_t);
75 }
76
77 std::mt19937 rng(t);
78
79 // Safely compute upper bound for random distribution
80 const uint64_t upper_64 = static_cast<uint64_t>(n) * 100;
81 const int upper = (upper_64 > static_cast<uint64_t>(INT_MAX)) ?
82 INT_MAX : static_cast<int>(upper_64);
83 std::uniform_int_distribution<int> dist(1, upper);
84
85 cout << "testSplayTree " << n << " " << t << endl;
86
88
89 cout << "Inserting " << n << " random values in tree ...\n";
90
91 unsigned int insCount = 0;
92
93 while (insCount < static_cast<unsigned int>(n))
94 {
95 const int value = dist(rng);
96 if (Splay_Tree_Rk<int>::Node *node = tree.search (value); node == nullptr)
97 {
98 node = new Splay_Tree_Rk<int>::Node (value);
99 tree.insert (node);
100 insCount++;
101 }
102 }
103
104 cout << insCount << " Items inserted" << endl;
105
106 if (tree.verify ())
107 cout << "arbol equilibrado" << endl;
108 else
109 {
110 cout << "error de equilibrio en el arbol" << endl;
111 destroyRec (tree.getRoot ());
112 return EXIT_FAILURE;
113 }
114
115 destroyRec (tree.getRoot ());
116 cout << "testSplayTree " << n << " " << t << endl;
117}
Node * insert(Node *p)
Inserts a node in a top down splay tree.
NodeType< Key > Node
Node * search(const Key &key)
Searches a key in a top down splay tree.
Node *& getRoot()
Get the top-down splay tree's root.
static mt19937 rng
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
Definition ahDefs.C:132
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 binary node with subtree count.
Top-down splay tree with rank support.