Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_splay_tree.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 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <iostream>
34# include <ah-random.H>
35# include <tpl_dynarray_set.H>
36# include <tpl_splay_tree.H>
37# include <string>
38
39# include <cassert>
40using namespace std;
41
42
44{
45 cout << KEY(p) << " " ;
46}
47
48
50{
51 cout << KEY(p) << " " ;
52}
53
54
55int main(int argc, char * argv[])
56{
57 int n = 1000;
58 if (argc > 1)
59 {
60 try { n = stoi(argv[1]); }
61 catch (...) { n = 1000; }
62 }
63
64 if (n <= 0)
65 {
66 cerr << "Error: n must be a positive integer." << endl;
67 return 1;
68 }
69
70 unsigned int seed = 0;
71 if (argc > 2)
72 {
73 try { seed = static_cast<unsigned int>(stoul(argv[2])); }
74 catch (...) { seed = 0; }
75 }
76
78
79 cout << "test_Splay_Tree " << n << " " << seed << endl;
80
81 Splay_Tree<int> tree;
83
84 DynArray_Set<int> values;
85
86 cout << "Testing for insertions" << endl;
87
88 for (int i = 0; i < n; i++)
89 {
90 int value;
91 do
92 {
93 value = r.uniform_int(100*n);
94 p = tree.search(value);
95 }
96 while (p != NULL);
97
98 cout << value << " ";
100 tree.insert(p);
101 values.append(value);
102 }
103
104 cout << endl << "Inorder " << endl;
106
107 cout << endl << "Preorder " << endl;
109
110 cout << endl << endl;
111
112 cout << "Testing for random searches" << endl;
113 for (int i = 0; i < n; ++i)
114 {
115 int idx = r.uniform_int(values.size());
116 int value = values(idx);
117 cout << value << " ";
118 if (tree.search(value) == NULL)
119 AH_ERROR("BUG detected ins searching");
120 }
121 cout << "Done" << endl;
122
123 cout << "Removing test" << endl;
124
125 for (int i = 0; i < n/7; i++)
126 {
127 int & value = values(r.uniform_int(values.size()));
128 p = tree.remove(value);
129 cout << value << " ";
130 assert(p != NULL);
131 assert(value == p->get_key());
132 delete p;
133 values.remove(value);
134 }
135
136 cout << endl << "Inorder " << endl << endl;
138
139 cout << endl;
140
141 destroyRec(tree.getRoot());
142
143 DynList<int>({122, 363, 1247, 510, 701, 1565, 1157, 728, 1564, 492, 861, 422}).
144 for_each([&tree] (int k)
145 {
146 tree.insert(new Splay_Tree<int>::Node(k));
147 });
148 cout << endl << "Preorder " << endl;
150
151 destroyRec(tree.getRoot());
152
153 cout << endl;
154}
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
#define AH_ERROR(...)
Print an error message (always enabled).
Definition ahDefs.H:270
int main()
@ KEY
Definition btreepic.C:169
size_t size_t int32_t value
Definition ca-c-api.h:116
Set-like container backed by a dynamic array.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
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
NodeType< Key > Node
Node *& getRoot() noexcept
Get the top-down splay tree's root.
Node * remove(const Key &key) noexcept
Remove a key from a top down splay tree.
Node * insert(Node *p) noexcept
Inserts a node in a top-down splay tree.
Node * search(const Key &key) noexcept
Searches a key in a top-down splay tree.
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
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
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
Definition ahAlgo.H:76
STL namespace.
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Gsl_Compatible_Rng r
void print_node(Splay_Tree< int >::Node *p, int, int)
void write_node(Splay_Tree< int >::Node *p, int, int)
Array-based dynamic set.
Top-down splay tree implementation (without rank support).