|
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>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 209 of file writeHeap.C.
| #define HEAP_RLINK | ( | i | ) | (2*(i) + 1) |
Definition at line 210 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 275 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 242 of file writeHeap.C.
References HEAP_LLINK, HEAP_RLINK, inorder(), Aleph::maps(), and output.
Referenced by avl_to_btreepic(), Aleph::build_tree(), build_tree(), inorder(), main(), read_from_stream(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
| 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 260 of file writeHeap.C.
References Aleph::maps(), and output_tex.
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 283 of file writeHeap.C.
References exists_in_heap(), inorder(), Aleph::ArrayHeap< T, Compare >::insert(), level_order(), Aleph::maps(), 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 222 of file writeHeap.C.
References HEAP_LLINK, HEAP_RLINK, Aleph::maps(), output, and preorder().
Referenced by avl_to_btreepic(), Aleph::build_tree(), build_tree(), main(), preorder(), Aleph::preorder_to_bst(), and read_from_stream().
| ofstream output |
Definition at line 213 of file writeHeap.C.
Referenced by Aleph::__generate_tree(), avl_to_btreepic(), Aleph::Huffman_Decoder_Engine::decode(), 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(), Eepic_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(), Eepic_Plane::draw_cartesian_axis(), Eepic_Plane::draw_closed_polygon(), Eepic_Plane::draw_closed_polygon_with_arrows(), 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(), Eepic_Plane::draw_ellipse(), Eepic_Plane::draw_point(), Eepic_Plane::draw_point_text(), Eepic_Plane::draw_points_text_in_polygon(), Eepic_Plane::draw_polygon(), Eepic_Plane::draw_segment(), Eepic_Plane::draw_spline(), Eepic_Plane::draw_spline_arrow(), Eepic_Plane::draw_text(), Eepic_Plane::draw_vertex_numbers_in_polygon(), Aleph::floyd_all_shortest_paths_latex(), 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(), inorder(), main(), 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()(), preorder(), put_string(), put_string_tkiz(), 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(), 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(), write_avl(), write_bin(), write_rand(), write_rb(), write_splay(), and write_treap().
| ofstream output_tex |
Definition at line 214 of file writeHeap.C.
Referenced by level_order(), and main().