202# include <tclap/CmdLine.h>
206using namespace Aleph;
209# define HEAP_LLINK(i) (2*(i))
210# define HEAP_RLINK(i) (2*(i) + 1)
262 for (
int i = 1; i <= n; ++i)
277 for (
int i = 1; i <= heap.
size(); ++i)
288 "Demonstrate array-based heap structure.\n"
289 "Creates a heap and generates visualization files.",
292 TCLAP::ValueArg<unsigned int>
nArg(
"n",
"count",
293 "Number of elements",
294 false, 10,
"unsigned int");
297 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
298 "Random seed (0 = use time)",
299 false, 0,
"unsigned int");
304 unsigned int n =
nArg.getValue();
305 unsigned int t =
seedArg.getValue();
312 cout <<
"=== Array-Based Heap Demo ===" <<
endl;
313 cout <<
"Elements: " << n <<
", Seed: " << t <<
endl <<
endl;
316 output.open(
"heap-ejm-aux.Tree", ios::out);
317 output_tex.open(
"heap-ejm-aux.tex", ios::out);
321 cerr <<
"Error: cannot open output files" <<
endl;
328 cout <<
"Inserting values: ";
329 for (
unsigned int i = 0; i < n; i++)
334 value = 1 +
static_cast<int>(10.0 * n *
rand() / (
RAND_MAX + 10.0 * n));
340 cout << value <<
" ";
349 cout <<
"Preorder: ";
361 cout <<
"Level-order: ";
362 for (
int i = 1; i <= heap.
size(); ++i)
363 cout << heap[i] <<
" ";
373 cout <<
" - heap-ejm-aux.Tree (preorder + inorder for btreepic)" <<
endl;
374 cout <<
" - heap-ejm-aux.tex (level-order for LaTeX)" <<
endl;
376 catch (TCLAP::ArgException& e)
378 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Fixed-capacity binary heap and heapsort algorithms.
void preorder(int v[], int n, int i)
Write preorder traversal of heap.
bool exists_in_heap(ArrayHeap< int > &heap, int x)
Check if value exists in heap.
void inorder(int v[], int n, int i)
Write inorder traversal of heap.
void level_order(int v[], int n)
Write level-order traversal for LaTeX output.