Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_splayRk.C
Go to the documentation of this file.
1/* Aleph-w
2
3 / \ | | ___ _ __ | |__ __ __
4 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
5 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
6 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
7 |_|
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2018 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31# include <iostream>
32# include <ctime>
33# include <string>
34# include <ah-random.H>
35# include <aleph.H>
36# include <tpl_dynarray_set.H>
37# include <tpl_splay_treeRk.H>
38# include <tpl_binNodeUtils.H>
39# include <tpl_sort_utils.H>
40
41# include <cassert>
42using namespace std;
43# include <cassert>
44using namespace Aleph;
45
46
48{
49 cout << node->get_key() << " ";
50}
51
52int main(int argc, char *argv[])
53{
54 int n = 10;
55 if (argc > 1)
56 {
57 try { n = stoi(argv[1]); } catch (...) { n = 10; }
58 }
59
60 if (n <= 0)
61 {
62 cerr << "n must be positive" << endl;
63 return 1;
64 }
65
66 unsigned int seed = 0;
67 if (argc > 2)
68 {
69 try { seed = stoul(argv[2]); } catch (...) { seed = 0; }
70 }
71
73
74 if (argc > 1)
75 cout << argv[0] << " " << n << " " << seed << endl;
76
80
81 cout << "Inserting " << n << " random values in tree ...\n";
82
83 for (int i = 0; i < n; i++)
84 {
85 int value;
86 do
87 {
88 value = r.uniform_int(100*n);
89 node = tree.search(value);
90 assert(tree.verify());
91 }
92 while (node not_eq NULL);
93
94 cout << value << " ";
96 tree.insert(node);
97 assert(tree.verify());
98 keys(i) = value;
99 }
100 cout << endl << endl;
101
102 assert(check_rank_tree(tree.getRoot()));
103
104 cout << "Sorting keys array" << endl;
106 for (int i = 0; i < keys.size(); ++i)
107 cout << keys(i) << " ";
108 cout << endl
109 << "done" << endl << endl;
110
111 cout << "inorden traversal prio" << endl;
113 cout << endl << endl;
114
115 cout << "Testing select" << endl;
116
117 for (int i = 0; i < n; i++)
118 {
119 node = tree.select(i);
120 cout << node->get_key() << " ";
121 assert(node->get_key() == keys(i));
122 }
123
124 cout << "done!" << endl << endl;
125
126 assert(check_rank_tree(tree.getRoot()));
127
128 cout << "testing random positions" << endl;
129 for (int i = 0; i < n; ++i)
130 {
131 int idx = r.uniform_int(keys.size());
132 std::pair<int, Splay_Tree_Rk<int>::Node*> pos =
133 tree.position(keys(idx));
134 assert(check_rank_tree(tree.getRoot()));
135 assert(pos.second != NULL);
136 assert(pos.second->get_key() == keys(idx));
137 assert(idx == pos.first);
138 cout << idx << "<-->" << pos.first << endl
139 << keys(idx) << "<-->" << pos.second->get_key() << " " << endl;
140 }
141
142 for (int i = 0; i < n/2; i++)
143 {
144 int idx = r.uniform_int(keys.size());
145 int & value = keys(idx);
146 cout << value << " ";
147 node = tree.remove(value);
148 assert(node != NULL);
149 assert(node->get_key() == value);
150 delete node;
152 }
153
154 cout << endl << "verifying Splay_Rk after deletions ... "
155 << endl;
156 assert(check_rank_tree(tree.getRoot()));
157 cout << " done" << endl;
158
159 cout << "Inorden" << endl;
161 cout << endl;
162
163 cout << "The path length is " << internal_path_length(tree.getRoot())
164 << endl;
165
166 destroyRec(tree.getRoot());
168
169 if (argc > 1)
170 cout << endl << argv[0] << " " << n << " " << seed << endl;
171
172}
173
174
175
176
177
178
179
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Set-like container backed by a dynamic array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
std::pair< long, Node * > position(const Key &key)
Returns the inorder (sorted) position of key.
Node * insert(Node *p)
Inserts a node in a top down splay tree.
NodeType< Key > Node
Node * remove(const Key &key)
Remove a key from a top-down splay tree.
Node * search(const Key &key)
Searches a key in a top down splay tree.
Node * select(const size_t &i)
Returns the node whose inorder position in the extended tree is i.
Node *& getRoot()
Get the top-down splay tree's root.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
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 all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
STL namespace.
int keys[]
ValueArg< size_t > seed
Definition testHash.C:53
Gsl_Compatible_Rng r
void printNode(Splay_Tree_Rk< int >::Node *node, int, int)
Utility functions for binary tree operations.
Array-based dynamic set.
Comprehensive sorting algorithms and search utilities for Aleph-w.
Top-down splay tree with rank support.