202# include <tclap/CmdLine.h>
208using namespace Aleph;
211# define HEAP_LLINK(i) (2*(i))
212# define HEAP_RLINK(i) (2*(i) + 1)
264 for (
int i = 1; i <= n; ++i)
279 for (
int i = 1; i <= heap.
size(); ++i)
290 "Demonstrate array-based heap structure.\n"
291 "Creates a heap and generates visualization files.",
294 TCLAP::ValueArg<unsigned int>
nArg(
"n",
"count",
295 "Number of elements",
296 false, 10,
"unsigned int");
299 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
300 "Random seed (0 = use time)",
301 false, 0,
"unsigned int");
306 unsigned int n =
nArg.getValue();
307 unsigned int t =
seedArg.getValue();
314 cout <<
"=== Array-Based Heap Demo ===" <<
endl;
315 cout <<
"Elements: " << n <<
", Seed: " << t <<
endl <<
endl;
318 output.open(
"heap-ejm-aux.Tree", ios::out);
319 output_tex.open(
"heap-ejm-aux.tex", ios::out);
323 cerr <<
"Error: cannot open output files" <<
endl;
330 cout <<
"Inserting values: ";
331 for (
unsigned int i = 0; i < n; i++)
336 value = 1 +
static_cast<int>(10.0 * n *
rand() / (
RAND_MAX + 10.0 * n));
342 cout << value <<
" ";
346 cout <<
"Heap size: " << heap.
size() <<
endl;
347 cout <<
"Min element (root): " << heap.
top() <<
endl <<
endl;
351 cout <<
"Preorder: ";
363 cout <<
"Level-order: ";
364 for (
int i = 1; i <= heap.
size(); ++i)
365 cout << heap[i] <<
" ";
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;
378 catch (TCLAP::ArgException& e)
380 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.
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.
Fixed-capacity binary heap and heapsort algorithms.
bool exists_in_heap(ArrayHeap< int > &heap, int x)
Check if value exists in heap.
void level_order(int v[], int n)
Write level-order traversal for LaTeX output.