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
125using namespace Aleph;
126using namespace std;
127
129
130ofstream output;
131
132void print_key(Node* p, int, int)
133{
134 output << p->get_key() << " ";
135}
136
137void print_count(Node* p, int, int)
138{
139 output << p->getCount() << " ";
140}
141
142void print_tag(Node*, int, int pos)
143{
144 output << "tag " << pos << " " << pos << " N -15 35" << endl;
145}
146
147int main(int argc, char* argv[])
148{
149 try
150 {
151 TCLAP::CmdLine cmd(
152 "Demonstrate ranked BST with subtree counts.\n"
153 "Creates a tree where each node stores the size of its subtree.",
154 ' ', "1.0");
155
156 TCLAP::ValueArg<int> nArg("n", "count",
157 "Number of elements",
158 false, 10, "int");
159 cmd.add(nArg);
160
161 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
162 "Random seed (0 = use time)",
163 false, 0, "unsigned int");
164 cmd.add(seedArg);
165
166 cmd.parse(argc, argv);
167
168 int n = nArg.getValue();
169 unsigned int t = seedArg.getValue();
170
171 if (t == 0)
172 t = time(nullptr);
173
174 srand(t);
175
176 cout << "=== Ranked BST Demo ===" << endl;
177 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
178
179 // Open output file
180 output.open("rank-tree-aux.Tree", ios::out);
181 if (!output.is_open())
182 {
183 cerr << "Error: cannot open output file" << endl;
184 return 1;
185 }
186
187 // Build ranked tree
189
190 cout << "Building ranked BST..." << endl;
191 cout << "Values: ";
192 for (int i = 0; i < n; i++)
193 {
194 int value;
195 while (true)
196 {
197 value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
198 if (searchInBinTree(root, value) == Node::NullPtr)
199 break;
200 }
201
202 Node* p = new Node(value);
204 cout << value << " ";
205 }
206 cout << endl << endl;
207
210
211 // Display tree statistics
212 cout << "Tree statistics:" << endl;
213 cout << " Total nodes: " << root->getCount() << endl;
214 cout << " Height: " << computeHeightRec(root) << endl;
215 cout << " Root key: " << KEY(root) << endl;
216 cout << " Root count: " << root->getCount() << endl;
217
218 // Demonstrate select operations
219 cout << endl << "Order statistics (select):" << endl;
220 for (int i = 0; i < min(n, 5); i++)
221 {
222 Node* sel = select(root, i);
223 cout << " Position " << i << ": " << KEY(sel) << endl;
224 }
225
226 // Write preorder traversal of keys
228
229 // Write auxiliary section with subtree counts
230 output << endl << endl << "START-AUX ";
232
233 // Write position tags for visualization
234 output << endl << endl << "% Position tags (infix order)" << endl << endl;
236 output << endl;
237
238 output.close();
239
240 cout << endl << "Generated file:" << endl;
241 cout << " - rank-tree-aux.Tree (with subtree counts and position tags)" << endl;
242
244 }
245 catch (TCLAP::ArgException& e)
246 {
247 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
248 return 1;
249 }
250
251 return 0;
252}
int main()
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
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