Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeHeap.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
198# include <cstdlib>
199# include <iostream>
200# include <fstream>
201# include <ctime>
202# include <tclap/CmdLine.h>
203# include <tpl_arrayHeap.H>
204
205using namespace std;
206using namespace Aleph;
207
208// Local macros for heap navigation (1-indexed array)
209# define HEAP_LLINK(i) (2*(i))
210# define HEAP_RLINK(i) (2*(i) + 1)
211
212// Output streams
213ofstream output;
214ofstream output_tex;
215
222void preorder(int v[], int n, int i)
223{
224 if (i > n)
225 return;
226
227 output << " " << v[i];
228 cout << " " << v[i];
229
230 assert(v[i] > 0);
231
232 preorder(v, n, HEAP_LLINK(i));
233 preorder(v, n, HEAP_RLINK(i));
234}
235
242void inorder(int v[], int n, int i)
243{
244 if (i > n)
245 return;
246
247 assert(v[i] > 0);
248
249 inorder(v, n, HEAP_LLINK(i));
250 output << " " << v[i];
251 cout << " " << v[i];
252 inorder(v, n, HEAP_RLINK(i));
253}
254
260void level_order(int v[], int n)
261{
262 for (int i = 1; i <= n; ++i)
263 {
264 assert(v[i] > 0);
265 output_tex << v[i] << " ";
266 }
267}
268
276{
277 for (int i = 1; i <= heap.size(); ++i)
278 if (heap[i] == x)
279 return true;
280 return false;
281}
282
283int main(int argc, char* argv[])
284{
285 try
286 {
287 TCLAP::CmdLine cmd(
288 "Demonstrate array-based heap structure.\n"
289 "Creates a heap and generates visualization files.",
290 ' ', "1.0");
291
292 TCLAP::ValueArg<unsigned int> nArg("n", "count",
293 "Number of elements",
294 false, 10, "unsigned int");
295 cmd.add(nArg);
296
297 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
298 "Random seed (0 = use time)",
299 false, 0, "unsigned int");
300 cmd.add(seedArg);
301
302 cmd.parse(argc, argv);
303
304 unsigned int n = nArg.getValue();
305 unsigned int t = seedArg.getValue();
306
307 if (t == 0)
308 t = time(nullptr);
309
310 srand(t);
311
312 cout << "=== Array-Based Heap Demo ===" << endl;
313 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
314
315 // Open output files
316 output.open("heap-ejm-aux.Tree", ios::out);
317 output_tex.open("heap-ejm-aux.tex", ios::out);
318
319 if (!output.is_open() || !output_tex.is_open())
320 {
321 cerr << "Error: cannot open output files" << endl;
322 return 1;
323 }
324
325 // Build heap with unique positive values
326 ArrayHeap<int> heap;
327
328 cout << "Inserting values: ";
329 for (unsigned int i = 0; i < n; i++)
330 {
331 int value;
332 do
333 {
334 value = 1 + static_cast<int>(10.0 * n * rand() / (RAND_MAX + 10.0 * n));
335 assert(value > 0);
336 }
337 while (exists_in_heap(heap, value));
338
339 heap.insert(value);
340 cout << value << " ";
341 }
342 cout << endl << endl;
343
344 cout << "Heap size: " << heap.size() << endl;
345 cout << "Min element (root): " << heap.top() << endl << endl;
346
347 // Write preorder traversal
348 output << "Prefix ";
349 cout << "Preorder: ";
350 preorder(&heap[0], heap.size(), 1);
351 cout << endl;
352
353 // Write inorder traversal
354 output << endl << "Infix ";
355 cout << "Inorder: ";
356 inorder(&heap[0], heap.size(), 1);
357 output << endl;
358 cout << endl;
359
360 // Write level-order for LaTeX
361 cout << "Level-order: ";
362 for (int i = 1; i <= heap.size(); ++i)
363 cout << heap[i] << " ";
364 cout << endl;
365 level_order(&heap[0], heap.size());
366 output_tex << endl;
367
368 // Close files
369 output.close();
370 output_tex.close();
371
372 cout << endl << "Generated files:" << endl;
373 cout << " - heap-ejm-aux.Tree (preorder + inorder for btreepic)" << endl;
374 cout << " - heap-ejm-aux.tex (level-order for LaTeX)" << endl;
375 }
376 catch (TCLAP::ArgException& e)
377 {
378 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
379 return 1;
380 }
381
382 return 0;
383}
int main()
Fixed-capacity binary heap backed by a raw array.
T & insert(const T &key)
Insert an element into the heap.
T & top()
Return a mutable reference to the minimum element.
constexpr size_t size() const noexcept
Return the number of elements currently stored.
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.
Fixed-capacity binary heap and heapsort algorithms.
void preorder(int v[], int n, int i)
Write preorder traversal of heap.
Definition writeHeap.C:222
#define HEAP_LLINK(i)
Definition writeHeap.C:209
#define HEAP_RLINK(i)
Definition writeHeap.C:210
ofstream output
Definition writeHeap.C:213
bool exists_in_heap(ArrayHeap< int > &heap, int x)
Check if value exists in heap.
Definition writeHeap.C:275
void inorder(int v[], int n, int i)
Write inorder traversal of heap.
Definition writeHeap.C:242
void level_order(int v[], int n)
Write level-order traversal for LaTeX output.
Definition writeHeap.C:260
ofstream output_tex
Definition writeHeap.C:214