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