Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testBinNodeXt.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_binNodeXt.H>
36# include <tpl_binNodeUtils.H>
37# include <tpl_binTree.H>
38
39using namespace Aleph;
40using namespace std;
41
43
44void print_node(Node * p, int, int)
45{
46 cout << "(" << p->get_key() << "," << p->getCount() << ")" ;
47}
48
49
50void print_key(Node * p, int, int)
51{
52 cout << p->get_key() << " ";
53}
54
55
56int main(int argc, char *argv[])
57{
58 int n = 10;
59 if (argc > 1)
60 {
61 try
62 {
63 n = stoi(argv[1]);
64 }
65 catch (...)
66 {
67 n = 10;
68 }
69 }
70
71 // Validate input to prevent null root dereferencing
72 if (n <= 1)
73 {
74 cerr << "Error: n must be greater than 1 for meaningful tree operations." << endl;
75 return 1;
76 }
77
78 unsigned int t = std::time(0);
79
80 if (argc > 2)
81 {
82 try
83 {
84 t = stoi(argv[2]);
85 }
86 catch (...)
87 {
88 t = std::time(0);
89 }
90 }
91
92 srand(t);
93
94 cout << argv[0] << " " << n << " " << t << endl;
95
96 int value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
98
99 for (int i = 0; i < n - 1; i++)
100 {
101 value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
102 if (searchInBinTree(root, value) == Node::NullPtr)
103 {
104 cout << value << " ";
105 Node * p = new Node (value);
107 }
108 else
109 cout << "." << endl;
110 }
111
112 cout << endl << endl;
113
114 preOrderRec(root, &print_node); cout << endl;
115 inOrderRec(root, &print_node); cout << endl;
116
119
120 cout << endl;
121
122 int num_nodes = root->getCount();
123
124 for (int i = 0; i < num_nodes; i++)
125 {
126 Node * p = select_rec(root, i);
127 cout << p->get_key() << " ";
128 assert(inorder_position(root, p->get_key(), p) == i);
129 }
130
131 for (int i = 0; i < num_nodes; i++)
132 {
133 auto value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
134 auto p = searchInBinTree(root, value);
135 if (p == Node::NullPtr)
136 assert(inorder_position(root, value) == -1);
137 }
138
139 cout << endl << endl;
140
141 for (int i = 0; i < num_nodes; i++)
142 cout << select(root, i)->get_key() << " ";
143
144 {
145 Node * q = NULL;
146
147 Node * p = select(root, 0);
148 int pos = find_position(root, p->get_key() - 1, q);
149 cout << endl << endl
150 <<"Pos of " << q->get_key() - 1 << " is " << pos << endl
151 << "Next key is " << q->get_key() << endl;
152
153 p = select(root, num_nodes/2);
154 pos = find_position(root, p->get_key(), q);
155 cout << endl
156 << "Pos of " << p->get_key() << " is " << pos << endl
157 << "key in node is " << q->get_key() << endl;
158
159 pos = find_position(root, p->get_key() - 1, q);
160 cout << endl
161 << "Pos of " << p->get_key() - 1 << " is " << pos << endl
162 << "key in node is " << q->get_key() << endl;
163
164 pos = find_position(root, p->get_key() + 1, q);
165 cout << endl
166 << "Pos of " << p->get_key() + 1 << " is " << pos << endl
167 << "key in node is " << q->get_key() << endl;
168
169 p = select(root, num_nodes - 1);
170 pos = find_position(root, p->get_key() + 1, q);
171 cout << endl
172 << "Pos of " << p->get_key() + 1 << " is " << pos << endl
173 << "Next key is " << p->get_key() << endl << endl;
174 }
175
177 for (int i = 0; i < num_nodes; i++)
178 keys[i] = select(root, i)->get_key();
179
180 cout << "Eliminando e insertando por clave " << num_nodes << endl;
181
182 for (int i = 0; i < num_nodes; i++)
183 {
184 Node * p = remove_by_pos_xt(root, i);
186 cout << "(" << i << "," << num_nodes << ")" << endl;
187 }
188
189 cout << "listo" << endl;
190
191 cout << "Eliminando e insertando por posicion " << num_nodes << endl;
192
193 for (int i = 0; i < num_nodes; i++)
194 {
195 Node * p = remove_by_pos_xt(root, i);
196 insert_by_pos_xt(root, p, i);
197 cout << "(" << i << "," << num_nodes << ")" << endl;
198 }
199
200 cout << "listo" << endl;
201
202 cout << "Eliminando e insertando por clave " << num_nodes << endl;
203
204 for (int i = 0; i < num_nodes; i++)
205 {
206 Node * p = select(root, i);
207 remove_by_key_xt(root, p->get_key());
208 insert_by_pos_xt(root, p, i);
209
212
213 cout << "(" << i << "," << num_nodes << ")" << endl;
214 }
215
216 cout << "listo" << endl;
217
218
219 Node * l, * r;
220
221 cout << endl << endl << "Particionando recursivamente ... " << endl << endl;
222 /* particion recursivamente */
224 cout << " ... listo" << endl;
225
226 // split_tree_pos(root, num_nodes/2, l, r);
227
230
233
235 cout << " | ";
237 cout << endl << endl;
238
239 destroyRec(l);
240 destroyRec(r);
241}
Core header for the Aleph-w library.
WeightedDigraph::Node Node
int num_nodes
Definition btreepic.C:410
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.
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Compute the inorder position of a key.
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 split_pos_rec(Node *&r, const size_t i, Node *&ts, Node *&tg)
Split a extended binary tree according to a position.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Node * remove_by_pos_xt(Node *&root, size_t pos)
Remove from a extended binary tree the node whose inorder position is pos.
Node * remove_by_key_xt(Node *&root, const typename Node::key_type &key, Compare &cmp) noexcept
Remove a key of extended binary tree.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
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.
void insert_by_pos_xt(Node *&r, Node *p, size_t pos)
Insert a node in a specific inorder position in a binary tree.
Node * select_rec(Node *r, const size_t i)
Recursively select the i-th node inorder sense.
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.
long find_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Find the inorder position of a key in an extended binary search tree.
STL namespace.
int keys[]
void print_node(Node *p, int, int)
void print_key(Node *p, int, int)
BinNodeXt< int > Node
gsl_rng * r
Utility functions for binary tree operations.
Extended binary node with subtree count.
Generic unbalanced binary search tree.
Lazy and scalable dynamic array implementation.
DynList< int > l