Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeSplay.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 <cstdlib>
29# include <ctime>
30# include <iostream>
31# include <fstream>
32# include <string>
33# include <aleph.H>
34# include <tpl_splay_tree.H>
35# include <tpl_binNodeUtils.H>
36
37using namespace Aleph;
38using namespace std;
39
40
41ofstream output("splay-tree-aux.Tree", ios::out);
42
44{
45 output << p->get_key() << " ";
46}
47
48int main(int argc, char *argv[])
49{
50 int n = 1000;
51 if (argc > 1)
52 {
53 try { n = stoi(argv[1]); } catch (...) { n = 1000; }
54 }
55
56 if (n <= 0)
57 {
58 cerr << "n must be positive" << endl;
59 return 1;
60 }
61
62 unsigned int t = std::time(0);
63 if (argc > 2)
64 {
65 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
66 }
67
68 srand(t);
69
70 cout << "writeSplay " << n << " " << t << endl;
71
72 Splay_Tree<int> tree;
74 int i, value;
75
76 cout << "Inserting " << n << " random values in tree ...\n";
77
78 for (i = 0; i < n; i++)
79 {
80 while (true)
81 {
82 value = 1+ static_cast<int> (n * 1.0 * rand () / (RAND_MAX + 1.0));
83 node = tree.search(value);
84 if (node == nullptr)
85 break;
86 }
87 node = new Splay_Tree<int>::Node (value);
88 tree.insert(node);
89 }
90
92
93 destroyRec(tree.getRoot());
94}
Core header for the Aleph-w library.
NodeType< Key > Node
Node *& getRoot() noexcept
Get the top-down splay tree's root.
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.
Utility functions for binary tree operations.
Top-down splay tree implementation (without rank support).
ofstream output
Definition writeHeap.C:215
void print_key(Splay_Tree< int >::Node *p, int, int)
Definition writeSplay.C:43