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