Aleph-w 3.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 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 <gsl/gsl_rng.h>
30# include <tpl_dynarray_set.H>
31# include <tpl_splay_tree.H>
32# include <string>
33
34# include <cassert>
35using namespace std;
36
37
39{
40 cout << KEY(p) << " " ;
41}
42
43
45{
46 cout << KEY(p) << " " ;
47}
48
49
50int main(int argc, char * argv[])
51{
52 int n = 1000;
53 if (argc > 1)
54 {
55 try { n = stoi(argv[1]); }
56 catch (...) { n = 1000; }
57 }
58
59 if (n <= 0)
60 {
61 cerr << "Error: n must be a positive integer." << endl;
62 return 1;
63 }
64
65 unsigned int seed = 0;
66 if (argc > 2)
67 {
68 try { seed = static_cast<unsigned int>(stoul(argv[2])); }
69 catch (...) { seed = 0; }
70 }
71
74
75 cout << "test_Splay_Tree " << n << " " << seed << endl;
76
77 Splay_Tree<int> tree;
79
80 DynArray_Set<int> values;
81
82 cout << "Testing for insertions" << endl;
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 p = tree.search(value);
91 }
92 while (p != NULL);
93
94 cout << value << " ";
95 p = new Splay_Tree<int>::Node (value);
96 tree.insert(p);
97 values.append(value);
98 }
99
100 cout << endl << "Inorder " << endl;
102
103 cout << endl << "Preorder " << endl;
105
106 cout << endl << endl;
107
108 cout << "Testing for random searches" << endl;
109 for (int i = 0; i < n; ++i)
110 {
111 int idx = gsl_rng_uniform_int(r, values.size());
112 int value = values(idx);
113 cout << value << " ";
114 if (tree.search(value) == NULL)
115 AH_ERROR("BUG detected ins searching");
116 }
117 cout << "Done" << endl;
118
119 cout << "Removing test" << endl;
120
121 for (int i = 0; i < n/7; i++)
122 {
123 int & value = values(gsl_rng_uniform_int(r, values.size()));
124 p = tree.remove(value);
125 cout << value << " ";
126 assert(p != NULL);
127 assert(value == p->get_key());
128 delete p;
129 values.remove(value);
130 }
131
132 cout << endl << "Inorder " << endl << endl;
134
135 cout << endl;
136
137 destroyRec(tree.getRoot());
138
139 DynList<int>({122, 363, 1247, 510, 701, 1565, 1157, 728, 1564, 492, 861, 422}).
140 for_each([&tree] (int k)
141 {
142 tree.insert(new Splay_Tree<int>::Node(k));
143 });
144 cout << endl << "Preorder " << endl;
146
147 destroyRec(tree.getRoot());
149
150 cout << endl;
151}
#define AH_ERROR(format, args...)
Print an error message (always enabled).
Definition ahDefs.H:271
@ KEY
Definition btreepic.C:169
Set-like container backed by a dynamic array.
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
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 recursively all the memory occupied by the tree root
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.
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:48
static int * k
gsl_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).