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
212# include <cstdlib>
213# include <cassert>
214using namespace std;
215# include <cstdlib>
216# include <cassert>
217using namespace Aleph;
218
219ofstream output;
220
221static void printNode(BinNode<int>* node, int, int)
222{
223 output << node->get_key() << " ";
224}
225
226int main(int argc, char* argv[])
227{
228 try
229 {
230 TCLAP::CmdLine cmd(
231 "Demonstrate root insertion in BST.\n"
232 "Shows how root insertion maintains recently inserted elements near the root.",
233 ' ', "1.0");
234
235 TCLAP::ValueArg<int> nArg("n", "count",
236 "Number of elements",
237 false, 20, "int");
238 cmd.add(nArg);
239
240 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
241 "Random seed (0 = use time)",
242 false, 0, "unsigned int");
243 cmd.add(seedArg);
244
245 cmd.parse(argc, argv);
246
247 int n = nArg.getValue();
248 unsigned int t = seedArg.getValue();
249
250 if (t == 0)
251 t = time(nullptr);
252
253 srand(t);
254
255 cout << "=== Root Insertion Demo ===" << endl;
256 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
257
258 // Open output file
259 output.open("insert_root-aux.Tree", ios::out);
260 if (!output.is_open())
261 {
262 cerr << "Error: cannot open output file" << endl;
263 return 1;
264 }
265
266 BinNode<int>* root = nullptr;
267 int inserted_count = 0;
268 int last_inserted = -1;
269
270 cout << "Inserting values: ";
271 for (int i = 0; i < n; i++)
272 {
273 int value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
274
275 BinNode<int>* p = new BinNode<int>(value);
276
277 // insert_root returns nullptr if duplicate
278 while (Aleph::insert_root(root, p) == nullptr)
279 {
280 KEY(p) = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
281 }
282
283 cout << KEY(p) << " ";
284 last_inserted = KEY(p);
286 }
287 cout << endl << endl;
288
290
291 cout << "Statistics:" << endl;
292 cout << " Items inserted: " << inserted_count << endl;
293 cout << " Tree height: " << computeHeightRec(root) << endl;
294 cout << " Last inserted (should be root): " << last_inserted << endl;
295 cout << " Actual root: " << KEY(root) << endl;
296
297 if (KEY(root) == last_inserted)
298 cout << " [OK] Root is the last inserted element" << endl;
299 else
300 cout << " [Note] Root may have changed due to rotations" << endl;
301
302 // Write preorder traversal
304 output << endl;
305 output.close();
306
307 cout << endl << "Generated file:" << endl;
308 cout << " - insert_root-aux.Tree" << endl;
309
311 }
312 catch (TCLAP::ArgException& e)
313 {
314 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
315 return 1;
316 }
317
318 return 0;
319}
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
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.
CmdLine cmd
Definition testHash.C:43
Utility functions for binary tree operations.
Basic binary tree node definitions.
ofstream output
static void printNode(BinNode< int > *node, int, int)