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 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 <gsl/gsl_rng.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
79
80 cout << "test_Splay_Tree " << n << " " << seed << endl;
81
82 Splay_Tree<int> tree;
84
85 DynArray_Set<int> values;
86
87 cout << "Testing for insertions" << endl;
88
89 for (int i = 0; i < n; i++)
90 {
91 int value;
92 do
93 {
94 value = gsl_rng_uniform_int(r, 100*n);
95 p = tree.search(value);
96 }
97 while (p != NULL);
98
99 cout << value << " ";
101 tree.insert(p);
102 values.append(value);
103 }
104
105 cout << endl << "Inorder " << endl;
107
108 cout << endl << "Preorder " << endl;
110
111 cout << endl << endl;
112
113 cout << "Testing for random searches" << endl;
114 for (int i = 0; i < n; ++i)
115 {
116 int idx = gsl_rng_uniform_int(r, values.size());
117 int value = values(idx);
118 cout << value << " ";
119 if (tree.search(value) == NULL)
120 AH_ERROR("BUG detected ins searching");
121 }
122 cout << "Done" << endl;
123
124 cout << "Removing test" << endl;
125
126 for (int i = 0; i < n/7; i++)
127 {
128 int & value = values(gsl_rng_uniform_int(r, values.size()));
129 p = tree.remove(value);
130 cout << value << " ";
131 assert(p != NULL);
132 assert(value == p->get_key());
133 delete p;
134 values.remove(value);
135 }
136
137 cout << endl << "Inorder " << endl << endl;
139
140 cout << endl;
141
142 destroyRec(tree.getRoot());
143
144 DynList<int>({122, 363, 1247, 510, 701, 1565, 1157, 728, 1564, 492, 861, 422}).
145 for_each([&tree] (int k)
146 {
147 tree.insert(new Splay_Tree<int>::Node(k));
148 });
149 cout << endl << "Preorder " << endl;
151
152 destroyRec(tree.getRoot());
154
155 cout << endl;
156}
#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.
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.
Doubly-linked list (defined in tpl_dynList.H).
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.
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
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
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_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).