Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeInsertRoot.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
204# include <ctime>
205# include <iostream>
206# include <fstream>
207# include <tclap/CmdLine.h>
208# include <aleph.H>
209# include <tpl_binNode.H>
210# include <tpl_binNodeUtils.H>
211
212using namespace std;
213using namespace Aleph;
214
215ofstream output;
216
217static void printNode(BinNode<int>* node, int, int)
218{
219 output << node->get_key() << " ";
220}
221
222int main(int argc, char* argv[])
223{
224 try
225 {
226 TCLAP::CmdLine cmd(
227 "Demonstrate root insertion in BST.\n"
228 "Shows how root insertion maintains recently inserted elements near the root.",
229 ' ', "1.0");
230
231 TCLAP::ValueArg<int> nArg("n", "count",
232 "Number of elements",
233 false, 20, "int");
234 cmd.add(nArg);
235
236 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
237 "Random seed (0 = use time)",
238 false, 0, "unsigned int");
239 cmd.add(seedArg);
240
241 cmd.parse(argc, argv);
242
243 int n = nArg.getValue();
244 unsigned int t = seedArg.getValue();
245
246 if (t == 0)
247 t = time(nullptr);
248
249 srand(t);
250
251 cout << "=== Root Insertion Demo ===" << endl;
252 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
253
254 // Open output file
255 output.open("insert_root-aux.Tree", ios::out);
256 if (!output.is_open())
257 {
258 cerr << "Error: cannot open output file" << endl;
259 return 1;
260 }
261
262 BinNode<int>* root = nullptr;
263 int inserted_count = 0;
264 int last_inserted = -1;
265
266 cout << "Inserting values: ";
267 for (int i = 0; i < n; i++)
268 {
269 int value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
270
271 BinNode<int>* p = new BinNode<int>(value);
272
273 // insert_root returns nullptr if duplicate
274 while (Aleph::insert_root(root, p) == nullptr)
275 {
276 KEY(p) = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
277 }
278
279 cout << KEY(p) << " ";
280 last_inserted = KEY(p);
282 }
283 cout << endl << endl;
284
286
287 cout << "Statistics:" << endl;
288 cout << " Items inserted: " << inserted_count << endl;
289 cout << " Tree height: " << computeHeightRec(root) << endl;
290 cout << " Last inserted (should be root): " << last_inserted << endl;
291 cout << " Actual root: " << KEY(root) << endl;
292
293 if (KEY(root) == last_inserted)
294 cout << " [OK] Root is the last inserted element" << endl;
295 else
296 cout << " [Note] Root may have changed due to rotations" << endl;
297
298 // Write preorder traversal
300 output << endl;
301 output.close();
302
303 cout << endl << "Generated file:" << endl;
304 cout << " - insert_root-aux.Tree" << endl;
305
307 }
308 catch (TCLAP::ArgException& e)
309 {
310 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
311 return 1;
312 }
313
314 return 0;
315}
int main()
Core header for the Aleph-w library.
@ KEY
Definition btreepic.C:169
Node for binary search tree.
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.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Utility functions for binary tree operations.
Basic binary tree node definitions.
ofstream output
static void printNode(BinNode< int > *node, int, int)