Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testBalance.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 <ctime>
28# include <cstdlib>
29# include <cassert>
30
31# include <iostream>
32# include <string>
33# include <aleph.H>
34# include <tpl_dynArray.H>
35# include <tpl_balanceXt.H>
36
37
38using namespace Aleph;
39using namespace std;
40
42
43void print_node(Node * p, int, int)
44{
45 cout << "(" << p->get_key() << "," << p->getCount() << ")" ;
46}
47
48
49void print_key(Node * p, int, int)
50{
51 cout << p->get_key() << " ";
52}
53
54
55int main(int argc, char *argv[])
56{
57 int n = 10;
58 if (argc > 1)
59 {
60 try
61 {
62 n = stoi(argv[1]);
63 }
64 catch (...)
65 {
66 n = 10;
67 }
68 }
69
70 if (n <= 0)
71 {
72 cerr << "Error: n must be a positive integer." << endl;
73 return 1;
74 }
75
76 unsigned int t = std::time(0);
77
78 if (argc > 2)
79 {
80 try
81 {
82 t = stoi(argv[2]);
83 }
84 catch (...)
85 {
86 t = std::time(0);
87 }
88 }
89
90 srand(t);
91
92 cout << argv[0] << " " << n << " " << t << endl;
93
94 int value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
96
97 for (int i = 0; i < n - 1; i++)
98 {
99 while (1)
100 {
101 value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
103 continue;
104
105 Node * p = new Node (value);
107 break;
108 }
109 }
110
112
113 cout << endl << endl;
114
115 preOrderRec(root, &print_key); cout << endl;
116
118
120
121 cout << endl << endl;
122
123 preOrderRec(root, &print_key); cout << endl;
124
126}
Core header for the Aleph-w library.
WeightedDigraph::Node Node
Node for extended binary search tree.
static BinNodeXt *const NullPtr
Key & get_key() noexcept
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
Node * insert_by_key_xt(Node *&r, Node *p, Compare &cmp) noexcept
Insert a node in an extended binary search tree.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Search a key in a binary search tree.
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.
Node * balance_tree(Node *root)
Reequilibra un árbol binario de búsqueda.
STL namespace.
void print_node(Node *p, int, int)
Definition testBalance.C:43
void print_key(Node *p, int, int)
Definition testBalance.C:49
BinNodeXt< int > Node
Definition testBalance.C:41
Tree balancing with extended nodes.
Lazy and scalable dynamic array implementation.