Aleph-w 3.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 <gsl/gsl_rng.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
74
75 if (argc > 1)
76 cout << argv[0] << " " << n << " " << seed << endl;
77
81
82 cout << "Inserting " << n << " random values in tree ...\n";
83
84 for (int i = 0; i < n; i++)
85 {
86 int value;
87 do
88 {
89 value = gsl_rng_uniform_int(r, 100*n);
90 node = tree.search(value);
91 assert(tree.verify());
92 }
93 while (node not_eq NULL);
94
95 cout << value << " ";
97 tree.insert(node);
98 assert(tree.verify());
99 keys(i) = value;
100 }
101 cout << endl << endl;
102
104
105 cout << "Sorting keys array" << endl;
107 for (int i = 0; i < keys.size(); ++i)
108 cout << keys(i) << " ";
109 cout << endl
110 << "done" << endl << endl;
111
112 cout << "inorden traversal prio" << endl;
114 cout << endl << endl;
115
116 cout << "Testing select" << endl;
117
118 for (int i = 0; i < n; i++)
119 {
120 node = tree.select(i);
121 cout << node->get_key() << " ";
122 assert(node->get_key() == keys(i));
123 }
124
125 cout << "done!" << endl << endl;
126
128
129 cout << "testing random positions" << endl;
130 for (int i = 0; i < n; ++i)
131 {
132 int idx = gsl_rng_uniform_int(r, keys.size());
133 std::pair<int, Splay_Tree_Rk<int>::Node*> pos =
134 tree.position(keys(idx));
136 assert(pos.second != NULL);
137 assert(pos.second->get_key() == keys(idx));
138 assert(idx == pos.first);
139 cout << idx << "<-->" << pos.first << endl
140 << keys(idx) << "<-->" << pos.second->get_key() << " " << endl;
141 }
142
143 for (int i = 0; i < n/2; i++)
144 {
145 int idx = gsl_rng_uniform_int(r, keys.size());
146 int & value = keys(idx);
147 cout << value << " ";
148 node = tree.remove(value);
149 assert(node != NULL);
150 assert(node->get_key() == value);
151 delete node;
153 }
154
155 cout << endl << "verifying Splay_Rk after deletions ... "
156 << endl;
158 cout << " done" << endl;
159
160 cout << "Inorden" << endl;
162 cout << endl;
163
164 cout << "The path length is " << internal_path_length(tree.getRoot())
165 << endl;
166
167 destroyRec(tree.getRoot());
169
170 if (argc > 1)
171 cout << endl << argv[0] << " " << n << " " << seed << endl;
172
174}
175
176
177
178
179
180
181
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.
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.
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
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 recursively all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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_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.