Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testSplayTree.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# include <cstdlib>
33# include <ctime>
34# include <iostream>
35# include <string>
36# include <aleph.H>
37# include <tpl_splay_tree.H>
38# include <tpl_binNodeUtils.H>
39
40using namespace std;
41using namespace Aleph;
42
43
44static void print(Splay_Tree<int>::Node *p, int, int)
45{
46 cout << p->get_key() << " ";
47}
48
49static int r[] = { 776, 846, 736, 515, 528, 677, 404, 629, 879, 762 };
50
51int main(int argc, char *argv[])
52{
53 int n = 1000;
54 unsigned int t = std::time(0);
55 int value;
56
57 if (argc > 1)
58 {
59 try
60 {
61 n = stoi(argv[1]);
62 }
63 catch (...)
64 {
65 n = 1000;
66 }
67 }
68
69 if (n <= 0)
70 {
71 cerr << "Error: n must be a positive integer." << endl;
72 return 1;
73 }
74
75 if (argc > 2)
76 {
77 try
78 {
79 t = stoi(argv[2]);
80 }
81 catch (...)
82 {
83 t = std::time(0);
84 }
85 }
86
87 srand(t);
88
89 cout << "testSplayTree " << n << " " << t << endl;
90
91 Splay_Tree<int> tree;
93 int i;
94
95 cout << "Inserting " << n << " random values in treee ...\n";
96
97 unsigned int insCount = 0, delCount = 0;
98
99 for (i = 0; i < n; i++)
100 {
101 value = 1+(int) (n*10.0*rand()/(RAND_MAX+1.0));
102 // value = r[i];
103 node = tree.search(value);
104 if (node == NULL)
105 {
106 insCount++;
107 node = new Splay_Tree<int>::Node (value);
108 tree.insert(node);
109 cout << value << " ";
110 }
111 }
112 cout << endl;
113
114 cout << endl << "Preorder " << endl << endl;
115 preOrderRec(tree.getRoot(), print);
116
117 cout << endl << insCount << " Items inserted" << endl;
118
119 for (i = 0; i < n; i++)
120 {
121 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
122 node = tree.remove(value);
123 if (node != NULL)
124 {
125 delCount++;
126 delete node;
127 }
128 }
129
130 cout << delCount << " Items removed" << endl;
131
132 destroyRec(tree.getRoot());
133 cout << "testSplayTree " << n << " " << t << endl;
134}
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
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.
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
STL namespace.
static int r[]
static void print(Splay_Tree< int >::Node *p, int, int)
Utility functions for binary tree operations.
Top-down splay tree implementation (without rank support).