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 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 <ctime>
33# include <cstdlib>
34# include <cassert>
35
36# include <iostream>
37# include <string>
38# include <aleph.H>
39# include <tpl_dynArray.H>
40# include <tpl_binNodeXt.H>
41# include <tpl_binNodeUtils.H>
42# include <tpl_binTree.H>
43
44using namespace Aleph;
45using namespace std;
46
48
49void print_node(Node * p, int, int)
50{
51 cout << "(" << p->get_key() << "," << p->getCount() << ")" ;
52}
53
54
55void print_key(Node * p, int, int)
56{
57 cout << p->get_key() << " ";
58}
59
60
61int main(int argc, char *argv[])
62{
63 int n = 10;
64 if (argc > 1)
65 {
66 try
67 {
68 n = stoi(argv[1]);
69 }
70 catch (...)
71 {
72 n = 10;
73 }
74 }
75
76 // Validate input to prevent null root dereferencing
77 if (n <= 1)
78 {
79 cerr << "Error: n must be greater than 1 for meaningful tree operations." << endl;
80 return 1;
81 }
82
83 unsigned int t = std::time(0);
84
85 if (argc > 2)
86 {
87 try
88 {
89 t = stoi(argv[2]);
90 }
91 catch (...)
92 {
93 t = std::time(0);
94 }
95 }
96
97 srand(t);
98
99 cout << argv[0] << " " << n << " " << t << endl;
100
101 int value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
103
104 for (int i = 0; i < n - 1; i++)
105 {
106 value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
108 {
109 cout << value << " ";
110 Node * p = new Node (value);
112 }
113 else
114 cout << "." << endl;
115 }
116
117 cout << endl << endl;
118
119 preOrderRec(root, &print_node); cout << endl;
120 inOrderRec(root, &print_node); cout << endl;
121
124
125 cout << endl;
126
127 int num_nodes = root->getCount();
128
129 for (int i = 0; i < num_nodes; i++)
130 {
131 Node * p = select_rec(root, i);
132 cout << p->get_key() << " ";
133 assert(inorder_position(root, p->get_key(), p) == i);
134 }
135
136 for (int i = 0; i < num_nodes; i++)
137 {
138 auto value = (int) (100.0*n*rand()/(RAND_MAX+1.0));
139 auto p = searchInBinTree(root, value);
140 if (p == Node::NullPtr)
142 }
143
144 cout << endl << endl;
145
146 for (int i = 0; i < num_nodes; i++)
147 cout << select(root, i)->get_key() << " ";
148
149 {
150 Node * q = NULL;
151
152 Node * p = select(root, 0);
153 int pos = find_position(root, p->get_key() - 1, q);
154 cout << endl << endl
155 <<"Pos of " << q->get_key() - 1 << " is " << pos << endl
156 << "Next key is " << q->get_key() << endl;
157
158 p = select(root, num_nodes/2);
159 pos = find_position(root, p->get_key(), q);
160 cout << endl
161 << "Pos of " << p->get_key() << " 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 pos = find_position(root, p->get_key() + 1, q);
170 cout << endl
171 << "Pos of " << p->get_key() + 1 << " is " << pos << endl
172 << "key in node is " << q->get_key() << endl;
173
174 p = select(root, num_nodes - 1);
175 pos = find_position(root, p->get_key() + 1, q);
176 cout << endl
177 << "Pos of " << p->get_key() + 1 << " is " << pos << endl
178 << "Next key is " << p->get_key() << endl << endl;
179 }
180
182 for (int i = 0; i < num_nodes; i++)
183 keys[i] = select(root, i)->get_key();
184
185 cout << "Eliminando e insertando por clave " << num_nodes << endl;
186
187 for (int i = 0; i < num_nodes; i++)
188 {
189 Node * p = remove_by_pos_xt(root, i);
191 cout << "(" << i << "," << num_nodes << ")" << endl;
192 }
193
194 cout << "listo" << endl;
195
196 cout << "Eliminando e insertando por posicion " << num_nodes << endl;
197
198 for (int i = 0; i < num_nodes; i++)
199 {
200 Node * p = remove_by_pos_xt(root, i);
201 insert_by_pos_xt(root, p, i);
202 cout << "(" << i << "," << num_nodes << ")" << endl;
203 }
204
205 cout << "listo" << endl;
206
207 cout << "Eliminando e insertando por clave " << num_nodes << endl;
208
209 for (int i = 0; i < num_nodes; i++)
210 {
211 Node * p = select(root, i);
212 remove_by_key_xt(root, p->get_key());
213 insert_by_pos_xt(root, p, i);
214
217
218 cout << "(" << i << "," << num_nodes << ")" << endl;
219 }
220
221 cout << "listo" << endl;
222
223
224 Node * l, * r;
225
226 cout << endl << endl << "Particionando recursivamente ... " << endl << endl;
227 /* particion recursivamente */
229 cout << " ... listo" << endl;
230
231 // split_tree_pos(root, num_nodes/2, l, r);
232
235
238
240 cout << " | ";
242 cout << endl << endl;
243
244 destroyRec(l);
245 destroyRec(r);
246}
Core header for the Aleph-w library.
WeightedDigraph::Node Node
int main()
int num_nodes
Definition btreepic.C:410
size_t size_t int32_t value
Definition ca-c-api.h:116
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:4071
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.
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
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