Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeRankTree.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
116# include <iostream>
117# include <fstream>
118# include <tclap/CmdLine.h>
119# include <aleph.H>
120# include <tpl_dynArray.H>
121# include <tpl_binNodeXt.H>
122# include <tpl_binNodeUtils.H>
123# include <tpl_binTree.H>
124
125# include <cstdlib>
126# include <cassert>
127using namespace Aleph;
128# include <cstdlib>
129# include <cassert>
130using namespace std;
131
133
134ofstream output;
135
136void print_key(Node* p, int, int)
137{
138 output << p->get_key() << " ";
139}
140
141void print_count(Node* p, int, int)
142{
143 output << p->getCount() << " ";
144}
145
146void print_tag(Node*, int, int pos)
147{
148 output << "tag " << pos << " " << pos << " N -15 35" << endl;
149}
150
151int main(int argc, char* argv[])
152{
153 try
154 {
155 TCLAP::CmdLine cmd(
156 "Demonstrate ranked BST with subtree counts.\n"
157 "Creates a tree where each node stores the size of its subtree.",
158 ' ', "1.0");
159
160 TCLAP::ValueArg<int> nArg("n", "count",
161 "Number of elements",
162 false, 10, "int");
163 cmd.add(nArg);
164
165 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
166 "Random seed (0 = use time)",
167 false, 0, "unsigned int");
168 cmd.add(seedArg);
169
170 cmd.parse(argc, argv);
171
172 int n = nArg.getValue();
173 unsigned int t = seedArg.getValue();
174
175 if (t == 0)
176 t = time(nullptr);
177
178 srand(t);
179
180 cout << "=== Ranked BST Demo ===" << endl;
181 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
182
183 // Open output file
184 output.open("rank-tree-aux.Tree", ios::out);
185 if (!output.is_open())
186 {
187 cerr << "Error: cannot open output file" << endl;
188 return 1;
189 }
190
191 // Build ranked tree
193
194 cout << "Building ranked BST..." << endl;
195 cout << "Values: ";
196 for (int i = 0; i < n; i++)
197 {
198 int value;
199 while (true)
200 {
201 value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
202 if (searchInBinTree(root, value) == Node::NullPtr)
203 break;
204 }
205
206 Node* p = new Node(value);
208 cout << value << " ";
209 }
210 cout << endl << endl;
211
214
215 // Display tree statistics
216 cout << "Tree statistics:" << endl;
217 cout << " Total nodes: " << root->getCount() << endl;
218 cout << " Height: " << computeHeightRec(root) << endl;
219 cout << " Root key: " << KEY(root) << endl;
220 cout << " Root count: " << root->getCount() << endl;
221
222 // Demonstrate select operations
223 cout << endl << "Order statistics (select):" << endl;
224 for (int i = 0; i < min(n, 5); i++)
225 {
226 Node* sel = select(root, i);
227 cout << " Position " << i << ": " << KEY(sel) << endl;
228 }
229
230 // Write preorder traversal of keys
232
233 // Write auxiliary section with subtree counts
234 output << endl << endl << "START-AUX ";
236
237 // Write position tags for visualization
238 output << endl << endl << "% Position tags (infix order)" << endl << endl;
240 output << endl;
241
242 output.close();
243
244 cout << endl << "Generated file:" << endl;
245 cout << " - rank-tree-aux.Tree (with subtree counts and position tags)" << endl;
246
248 }
249 catch (TCLAP::ArgException& e)
250 {
251 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
252 return 1;
253 }
254
255 return 0;
256}
Core header for the Aleph-w library.
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Node for extended binary search tree.
static BinNodeXt *const NullPtr
Key & get_key() noexcept
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4111
__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.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search 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.
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.
Extended binary node with subtree count.
Generic unbalanced binary search tree.
Lazy and scalable dynamic array implementation.
void print_tag(Node *, int, int pos)
void print_key(Node *p, int, int)
ofstream output
void print_count(Node *p, int, int)
BinNodeXt< int > Node