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 This program is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <https://www.gnu.org/licenses/>.
25*/
26# include <iostream>
27# include <ctime>
28# include <string>
29# include <gsl/gsl_rng.h>
30# include <aleph.H>
31# include <tpl_dynarray_set.H>
32# include <tpl_splay_treeRk.H>
33# include <tpl_binNodeUtils.H>
34# include <tpl_sort_utils.H>
35
36# include <cassert>
37using namespace std;
38# include <cassert>
39using namespace Aleph;
40
41
43{
44 cout << node->get_key() << " ";
45}
46
47int main(int argc, char *argv[])
48{
49 int n = 10;
50 if (argc > 1)
51 {
52 try { n = stoi(argv[1]); } catch (...) { n = 10; }
53 }
54
55 if (n <= 0)
56 {
57 cerr << "n must be positive" << endl;
58 return 1;
59 }
60
61 unsigned int seed = 0;
62 if (argc > 2)
63 {
64 try { seed = stoul(argv[2]); } catch (...) { seed = 0; }
65 }
66
69
70 if (argc > 1)
71 cout << argv[0] << " " << n << " " << seed << endl;
72
76
77 cout << "Inserting " << n << " random values in tree ...\n";
78
79 for (int i = 0; i < n; i++)
80 {
81 int value;
82 do
83 {
84 value = gsl_rng_uniform_int(r, 100*n);
85 node = tree.search(value);
86 assert(tree.verify());
87 }
88 while (node not_eq NULL);
89
90 cout << value << " ";
91 node = new Splay_Tree_Rk<int>::Node (value);
92 tree.insert(node);
93 assert(tree.verify());
94 keys(i) = value;
95 }
96 cout << endl << endl;
97
99
100 cout << "Sorting keys array" << endl;
102 for (int i = 0; i < keys.size(); ++i)
103 cout << keys(i) << " ";
104 cout << endl
105 << "done" << endl << endl;
106
107 cout << "inorden traversal prio" << endl;
109 cout << endl << endl;
110
111 cout << "Testing select" << endl;
112
113 for (int i = 0; i < n; i++)
114 {
115 node = tree.select(i);
116 cout << node->get_key() << " ";
117 assert(node->get_key() == keys(i));
118 }
119
120 cout << "done!" << endl << endl;
121
123
124 cout << "testing random positions" << endl;
125 for (int i = 0; i < n; ++i)
126 {
127 int idx = gsl_rng_uniform_int(r, keys.size());
128 std::pair<int, Splay_Tree_Rk<int>::Node*> pos =
129 tree.position(keys(idx));
131 assert(pos.second != NULL);
132 assert(pos.second->get_key() == keys(idx));
133 assert(idx == pos.first);
134 cout << idx << "<-->" << pos.first << endl
135 << keys(idx) << "<-->" << pos.second->get_key() << " " << endl;
136 }
137
138 for (int i = 0; i < n/2; i++)
139 {
140 int idx = gsl_rng_uniform_int(r, keys.size());
141 int & value = keys(idx);
142 cout << value << " ";
143 node = tree.remove(value);
144 assert(node != NULL);
145 assert(node->get_key() == value);
146 delete node;
147 keys.remove(value);
148 }
149
150 cout << endl << "verifying Splay_Rk after deletions ... "
151 << endl;
153 cout << " done" << endl;
154
155 cout << "Inorden" << endl;
157 cout << endl;
158
159 cout << "The path length is " << internal_path_length(tree.getRoot())
160 << endl;
161
162 destroyRec(tree.getRoot());
164
165 if (argc > 1)
166 cout << endl << argv[0] << " " << n << " " << seed << endl;
167
169}
170
171
172
173
174
175
176
Core header for the Aleph-w library.
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.
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
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.
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:48
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.