|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Demonstrates array-based heap structure and generates visualization files. More...
#include <cstdlib>#include <iostream>#include <fstream>#include <ctime>#include <tclap/CmdLine.h>#include <tpl_arrayHeap.H>#include <cassert>Go to the source code of this file.
Macros | |
| #define | HEAP_LLINK(i) (2*(i)) |
| #define | HEAP_RLINK(i) (2*(i) + 1) |
Functions | |
| void | preorder (int v[], int n, int i) |
| Write preorder traversal of 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. | |
| bool | exists_in_heap (ArrayHeap< int > &heap, int x) |
| Check if value exists in heap. | |
| int | main (int argc, char *argv[]) |
Variables | |
| ofstream | output |
| ofstream | output_tex |
Demonstrates array-based heap structure and generates visualization files.
This program creates an array-based min-heap (priority queue) with random values and generates visualization files showing the heap structure in various traversal orders. Heaps are fundamental data structures for priority queues and efficient sorting.
A heap is a complete binary tree stored in an array that satisfies the heap property:
Key properties:
Heaps use a clever array indexing scheme:
i/2 (integer division)2*i2*i + 1Time: O(log n) - height of tree
Time: O(log n) - height of tree
Time: O(n) - surprisingly efficient! Why: Most nodes are near bottom, few swaps needed
| Operation | Time Complexity | Description |
|---|---|---|
| insert | O(log n) | Add element, bubble up |
| extract_min | O(log n) | Remove root, bubble down |
| peek | O(1) | Get root without removal |
| build_heap | O(n) | Heapify array in-place |
| decrease_key | O(log n) | Decrease element, bubble up |
| increase_key | O(log n) | Increase element, bubble down |
| Structure | Insert | Extract Min | Build | Best For |
|---|---|---|---|---|
| Heap | O(log n) | O(log n) | O(n) | Priority queues |
| Sorted Array | O(n) | O(1) | O(n log n) | Static data |
| BST | O(log n) | O(log n) | O(n log n) | Dynamic, ordered |
| Linked List | O(1) | O(n) | O(n) | Simple cases |
heap-ejm-aux.Tree**: Preorder and inorder traversals for btreepicheap-ejm-aux.tex**: Level-order traversal formatted for LaTeXThe tree visualization shows the heap structure, which is a complete binary tree (all levels full except possibly the last, filled left-to-right).
Definition in file writeHeap.C.
| #define HEAP_LLINK | ( | i | ) | (2*(i)) |
Definition at line 211 of file writeHeap.C.
| #define HEAP_RLINK | ( | i | ) | (2*(i) + 1) |
Definition at line 212 of file writeHeap.C.
| bool exists_in_heap | ( | ArrayHeap< int > & | heap, |
| int | x | ||
| ) |
Check if value exists in heap.
| heap | The heap to search |
| x | Value to find |
Definition at line 277 of file writeHeap.C.
References Aleph::ArrayHeap< T, Compare >::size().
Referenced by main().
| void inorder | ( | int | v[], |
| int | n, | ||
| int | i | ||
| ) |
Write inorder traversal of heap.
| v | Heap array (1-indexed) |
| n | Number of elements |
| i | Current index |
Definition at line 244 of file writeHeap.C.
References Aleph::divide_and_conquer_partition_dp(), HEAP_LLINK, HEAP_RLINK, inorder, and output.
| void level_order | ( | int | v[], |
| int | n | ||
| ) |
Write level-order traversal for LaTeX output.
| v | Heap array (1-indexed) |
| n | Number of elements |
Definition at line 262 of file writeHeap.C.
References Aleph::divide_and_conquer_partition_dp(), and output_tex.
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 285 of file writeHeap.C.
References cmd, Aleph::divide_and_conquer_partition_dp(), exists_in_heap(), inorder, Aleph::ArrayHeap< T, Compare >::insert(), level_order(), output, output_tex, preorder, Aleph::ArrayHeap< T, Compare >::size(), and Aleph::ArrayHeap< T, Compare >::top().
| void preorder | ( | int | v[], |
| int | n, | ||
| int | i | ||
| ) |
Write preorder traversal of heap.
| v | Heap array (1-indexed) |
| n | Number of elements |
| i | Current index |
Definition at line 224 of file writeHeap.C.
References Aleph::divide_and_conquer_partition_dp(), HEAP_LLINK, HEAP_RLINK, output, and preorder.
| ofstream output |
Definition at line 215 of file writeHeap.C.
Referenced by Aleph::__generate_tree(), Aleph::FFT< Real >::Plan::apply_mixed_radix_transform(), Aleph::FFT< Real >::apply_window(), Aleph::FFT< Real >::apply_window(), avl_to_btreepic(), Aleph::FFT< Real >::balance_polynomial_for_roots(), Aleph::FFT< Real >::batched_sosfilt(), Aleph::FFT< Real >::batched_stft(), Aleph::NTT< MOD, ROOT >::bigint_multiply(), Aleph::FFT< Real >::bilinear_substitute_polynomial(), Aleph::Aho_Corasick::contains_any(), Aleph::FFT< Real >::OverlapSave::convolve(), Aleph::FFT< Real >::PartitionedConvolver::convolve(), Aleph::FFT< Real >::OverlapAddBank::convolve_impl(), Aleph::FFT< Real >::OverlapAdd::convolve_impl(), Aleph::Huffman_Decoder_Engine::decode(), Aleph::FFT< Real >::demean_copy(), Eepic_Geom_Object::draw(), Eepic_Point::draw(), Eepic_Segment::draw(), Eepic_Arrow::draw(), Eepic_Thick_Segment::draw(), Eepic_Thick_Dash_Segment::draw(), Eepic_Thick_Arrow::draw(), Eepic_Dotted_Segment::draw(), Eepic_Dash_Segment::draw(), Eepic_Dotted_Arrow::draw(), Eepic_Dash_Arrow::draw(), Eepic_Thick_Dash_Arrow::draw(), Eepic_Triangle::draw(), Eepic_Ellipse::draw(), Eepic_Shade_Ellipse::draw(), Eepic_Thick_Ellipse::draw(), Eepic_Text::draw(), Eepic_Left_Text::draw(), Eepic_Right_Text::draw(), Eepic_Center_Text::draw(), Eepic_Polygon::draw(), Eepic_Polygon_With_Points::draw(), Eepic_Polygon_With_Arrows::draw(), Eepic_Dotted_Polygon::draw(), Eepic_Dash_Polygon::draw(), Eepic_Dotted_Polygon_With_Points::draw(), Eepic_Dash_Polygon_With_Points::draw(), Eepic_Dash_Polygon_With_Arrow::draw(), Eepic_Shade_Polygon::draw(), Eepic_Shade_Polygon_With_Points::draw(), Eepic_Shade_Polygon_With_Arrows::draw(), Eepic_Shade_Polygon_With_Text_Points::draw(), Eepic_Shade_Polygon_With_Vertex_Numbers::draw(), Eepic_Spline::draw(), Eepic_Dash_Spline::draw(), Eepic_Spline_Arrow::draw(), Eepic_Dash_Spline_Arrow::draw(), Eepic_Thick_Polygon_With_Points::draw(), Eepic_Thick_Polygon_With_Arrows::draw(), Eepic_Thick_Dotted_Polygon::draw(), Eepic_Thick_Dash_Polygon::draw(), Eepic_Thick_Dotted_Polygon_With_Points::draw(), Eepic_Thick_Dash_Polygon_With_Points::draw(), Eepic_Thick_Dash_Polygon_With_Arrow::draw(), Eepic_Thick_Shade_Polygon::draw(), Eepic_Thick_Shade_Polygon_With_Points::draw(), Eepic_Thick_Shade_Polygon_With_Arrows::draw(), Eepic_Thick_Shade_Polygon_With_Text_Points::draw(), Eepic_Thick_Shade_Polygon_With_Vertex_Numbers::draw(), Eepic_Thick_Spline::draw(), Eepic_Thick_Dash_Spline::draw(), Eepic_Thick_Spline_Arrow::draw(), Eepic_Thick_Dash_Spline_Arrow::draw(), Eepic_Regular_Polygon::draw(), Eepic_Regular_Polygon_With_Points::draw(), Eepic_Dotted_Regular_Polygon::draw(), Eepic_Dash_Regular_Polygon::draw(), Eepic_Dotted_Regular_Polygon_With_Points::draw(), Eepic_Dash_Regular_Polygon_With_Points::draw(), Eepic_Shade_Regular_Polygon::draw(), Eepic_Shade_Regular_Polygon_With_Points::draw(), Eepic_Shade_Regular_Polygon_With_Arrows::draw(), Eepic_Shade_Regular_Polygon_With_Text_Points::draw(), Eepic_Shade_Regular_Polygon_With_Vertex_Numbers::draw(), Aleph::Tikz_Scene::draw(), Eepic_Plane::draw(), Aleph::Tikz_Plane::draw(), draw_arc(), draw_arc_tikz(), Eepic_Plane::draw_arrow(), Eepic_Plane::draw_arrow_dash_segment(), Eepic_Plane::draw_arrow_dotted_segment(), Eepic_Plane::draw_arrows_in_polygon(), Aleph::Tikz_Scene::draw_beamer_document(), Aleph::Tikz_Scene::draw_beamer_document_epilogue(), Aleph::Tikz_Scene::draw_beamer_document_preamble(), Aleph::Tikz_Scene::draw_beamer_frame(), Aleph::Tikz_Scene::draw_beamer_frame_begin(), Aleph::Tikz_Scene::draw_beamer_overlays_document(), Aleph::Tikz_Plane::draw_cartesian_axis(), Eepic_Plane::draw_cartesian_axis(), Eepic_Plane::draw_closed_polygon(), Eepic_Plane::draw_closed_polygon_with_arrows(), Aleph::Tikz_Plane::draw_coordinate_grid(), Aleph::Tikz_Plane::draw_cubic_bezier(), Eepic_Plane::draw_dash_segment(), Eepic_Plane::draw_dash_spline(), Eepic_Plane::draw_dash_spline_arrow(), Eepic_Plane::draw_dots_from_polygon(), Eepic_Plane::draw_dotted_closed_polygon(), Eepic_Plane::draw_dotted_segment(), Aleph::Tikz_Plane::draw_ellipse(), Eepic_Plane::draw_ellipse(), Aleph::Tikz_Plane::draw_legend(), Aleph::Tikz_Plane::draw_line_eq(), Aleph::Tikz_Plane::draw_object(), Aleph::Tikz_Plane::draw_point(), Eepic_Plane::draw_point(), Eepic_Plane::draw_point_text(), Eepic_Plane::draw_points_text_in_polygon(), Aleph::Tikz_Plane::draw_polygon(), Eepic_Plane::draw_polygon(), Aleph::Tikz_Plane::draw_polyline(), Aleph::Tikz_Plane::draw_quadratic_bezier(), Aleph::Tikz_Plane::draw_rectangle(), Aleph::Tikz_Plane::draw_regular_polygon(), Aleph::Tikz_Plane::draw_rotated_ellipse(), Aleph::Tikz_Plane::draw_segment(), Eepic_Plane::draw_segment(), Eepic_Plane::draw_spline(), Eepic_Plane::draw_spline_arrow(), Aleph::Tikz_Plane::draw_text(), Eepic_Plane::draw_text(), Aleph::Tikz_Plane::draw_tikzset_styles(), Aleph::Tikz_Plane::draw_triangle(), Eepic_Plane::draw_vertex_numbers_in_polygon(), Aleph::FFT< Real >::evaluate_polynomial_and_derivative(), Aleph::FFT< Real >::evaluate_series_at_unit_circle(), Aleph::FFT< Real >::LFilterBank::filter(), Aleph::FFT< Real >::SOSFilterBank::filter(), Aleph::FFT< Real >::filtfilt(), Aleph::floyd_all_shortest_paths_latex(), Aleph::FFT< Real >::BatchedSTFTProcessor::flush(), Aleph::FFT< Real >::BatchedISTFTProcessor::flush(), Aleph::FFT< Real >::OverlapSave::flush(), Aleph::FFT< Real >::PartitionedConvolver::flush(), Aleph::FFT< Real >::freqz(), Aleph::FFT< Real >::freqz_impl(), generate_bin_tree(), generate_cross_spanning_tree(), generate_curve(), Aleph::generate_dot_file(), generate_epilogue(), generate_forest(), generate_graph(), Aleph::generate_graphpic(), Aleph::generate_graphviz(), generate_infix_traversal(), generate_net_spanning_tree(), generate_picture(), generate_prefix_traversal(), generate_prologue(), generate_split_lines(), generate_tree(), generate_tree(), huffman_to_btreepic(), Aleph::FFT< Real >::iir_filter_impl(), Aleph::FFT< Real >::iir_filtfilt_impl(), inorder(), Aleph::FFT< Real >::Plan::inverse_transform_real(), Aleph::FFT< Real >::Plan::inverse_transform_real_batch(), Aleph::FFT< Real >::inverse_transform_real_general_impl(), Aleph::FFT< Real >::inverse_transform_real_optimized_impl(), Aleph::FFT< Real >::istft_impl(), Aleph::FFT< Real >::magnitude_spectrum(), main(), main(), Aleph::FFT< Real >::Plan::mixed_radix_transform_recursive(), Aleph::NTT< MOD, ROOT >::multipoint_eval(), Aleph::NTT< MOD, ROOT >::multipoint_eval_recursive(), Aleph::NTT< MOD, ROOT >::normalize_poly(), Aleph::ConvexPolygonIntersectionBasic::operator()(), Aleph::Dft_Store_Arc< GT >::operator()(), Aleph::Dft_Store_Node< GT >::operator()(), Aleph::Warc< GT >::operator()(), Aleph::Wnode< GT >::operator()(), Aleph::Dft_Store_Arc< GT >::operator()(), Aleph::Dft_Store_Node< GT >::operator()(), Aleph::FFT< Real >::overlap_add_frames(), Aleph::NTT< MOD, ROOT >::padded_copy(), Aleph::FFT< Real >::pbatched_sosfilt(), Aleph::FFT< Real >::pbatched_stft(), Aleph::NTT< MOD, ROOT >::pbigint_multiply(), Aleph::FFT< Real >::LFilterBank::pfilter(), Aleph::FFT< Real >::SOSFilterBank::pfilter(), Aleph::FFT< Real >::pfiltfilt(), Aleph::FFT< Real >::BatchedSTFTProcessor::pflush(), Aleph::FFT< Real >::BatchedISTFTProcessor::pflush(), Aleph::FFT< Real >::phase_spectrum(), Aleph::FFT< Real >::Plan::pinverse_transform_real(), Aleph::FFT< Real >::Plan::pinverse_transform_real_batch(), Aleph::FFT< Real >::poles(), Aleph::NTT< MOD, ROOT >::poly_add_normalized(), Aleph::NTT< MOD, ROOT >::poly_add_series(), Aleph::NTT< MOD, ROOT >::poly_derivative(), Aleph::NTT< MOD, ROOT >::poly_integral(), Aleph::NTT< MOD, ROOT >::poly_power(), Aleph::NTT< MOD, ROOT >::poly_scalar_mul_series(), Aleph::NTT< MOD, ROOT >::poly_sqrt(), Aleph::NTT< MOD, ROOT >::poly_sub_normalized(), Aleph::NTT< MOD, ROOT >::poly_sub_series(), Aleph::FFT< Real >::polynomial_multiply(), Aleph::FFT< Real >::polynomial_power(), Aleph::FFT< Real >::power_spectrum(), Aleph::FFT< Real >::BatchedISTFTProcessor::pprocess_block(), Aleph::FFT< Real >::BatchedSTFTProcessor::pprocess_block(), Aleph::FFT< Real >::prefix_copy(), Aleph::NTT< MOD, ROOT >::prefix_copy(), preorder(), print_key(), print_key(), Aleph::FFT< Real >::BatchedISTFTProcessor::process_block(), Aleph::FFT< Real >::BatchedSTFTProcessor::process_block(), Aleph::FFT< Real >::project_real_batch_output(), Aleph::FFT< Real >::project_real_output(), Aleph::FFT< Real >::ptransform_padded(), Aleph::FFT< Real >::ptransformed(), Aleph::FFT< Real >::Plan::ptransformed(), Aleph::NTT< MOD, ROOT >::ptransformed(), Aleph::NTT< MOD, ROOT >::Plan::ptransformed(), Aleph::FFT< Real >::ptransformed_axes(), Aleph::FFT< Real >::ptransformed_batch(), Aleph::FFT< Real >::Plan::ptransformed_batch(), Aleph::NTT< MOD, ROOT >::Plan::ptransformed_batch(), put_string(), put_string_tkiz(), NTTIndustrialTest::random_poly(), Aleph::NTTExact::reconstruct_product(), Aleph::FFT< Real >::reflect_pad_signal(), Aleph::FFT< Real >::resample_poly(), Aleph::FFT< Real >::reshape_matrix_row_major(), Aleph::FFT< Real >::reshape_tensor3_row_major(), Aleph::FFT< Real >::reverse_copy(), Aleph::NTT< MOD, ROOT >::reverse_poly(), Aleph::IO_Graph< GT, Load_Node, Store_Node, Load_Arc, Store_Arc, NF, AF >::save(), Aleph::BitArray::save(), Aleph::BitArray::save_in_array_of_chars(), Aleph::IO_Graph< GT, Load_Node, Store_Node, Load_Arc, Store_Arc, NF, AF >::save_in_text_mode(), Aleph::Huffman_Encoder_Engine::save_leaf_keys_in_prefix(), save_parameters(), Aleph::Huffman_Encoder_Engine::save_string_as_bytes(), Aleph::save_tree(), Aleph::Huffman_Encoder_Engine::save_tree(), Aleph::Huffman_Encoder_Engine::save_tree_in_array_of_chars(), Aleph::save_tree_in_array_of_chars(), Aleph::save_tree_keys_in_prefix(), Aleph::FFT< Real >::scaled_copy(), Aleph::Aho_Corasick::search(), Aleph::NTT< MOD, ROOT >::series_prefix(), Aleph::FFT< Real >::slice_copy(), Aleph::FFT< Real >::sos_filtfilt_impl(), Aleph::FFT< Real >::substitute_rational_polynomial(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), Aleph::FFT< Real >::to_array(), Aleph::FFT< Real >::transform_padded(), Aleph::FFT< Real >::transform_stft_frames(), Aleph::FFT< Real >::transformed(), Aleph::FFT< Real >::Plan::transformed(), Aleph::NTT< MOD, ROOT >::transformed(), Aleph::NTT< MOD, ROOT >::Plan::transformed(), Aleph::FFT< Real >::transformed_axes(), Aleph::FFT< Real >::transformed_batch(), Aleph::FFT< Real >::Plan::transformed_batch(), Aleph::NTT< MOD, ROOT >::Plan::transformed_batch(), Aleph::FFT< Real >::transpose_spectrogram_layout_impl(), Aleph::NTT< MOD, ROOT >::truncate_poly(), Aleph::FFT< Real >::unwrap_phase(), Aleph::FFT< Real >::upfirdn(), write_avl(), write_bin(), write_rand(), write_rb(), write_splay(), write_treap(), Aleph::FFT< Real >::zero_pad_edges(), Aleph::FFT< Real >::zero_padded_copy(), Aleph::FFT< Real >::zero_padded_copy(), Aleph::NTT< MOD, ROOT >::zero_series(), and Aleph::FFT< Real >::zeros().
| ofstream output_tex |
Definition at line 216 of file writeHeap.C.
Referenced by level_order(), and main().