|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Huffman tree visualization for btreepic LaTeX package. More...
#include <cstdlib>#include <ostream>#include <algorithm>#include <tpl_dynMapTree.H>#include <Huffman.H>Go to the source code of this file.
Classes | |
| struct | Infix_Desc |
| Descriptor for infix (in-order) traversal position. More... | |
| struct | Level_Desc |
| Descriptor for level-order traversal information. More... | |
Typedefs | |
| using | Level_Table = DynMapTree< Freq_Node *, Level_Desc, Treap > |
| Type alias for the level descriptor map. | |
Enumerations | |
| enum | Offset { NO_OFFSET , LEFT , RIGHT } |
| Horizontal offset direction for node label adjustment. More... | |
Functions | |
| long double | get_offset (size_t index) noexcept |
| Get safe offset value with bounds checking. | |
| void | reset_huffman_btreepic_state () noexcept |
| Reset global state for a new tree processing. | |
| void | save_infix_pos (Freq_Node *p, int level, int pos) |
| Save infix position for a node. | |
| void | save_level_pos_1 (Freq_Node *p, int pos, bool is_left) |
| First pass level-order callback: record child direction. | |
| void | save_level_pos_2 (Freq_Node *p, int pos, bool is_left) |
| Second pass level-order callback: link successors. | |
| void | write_prefix (Freq_Node *p, int level, int prefix_pos) |
| Write node position in prefix order. | |
| void | write_freq (Freq_Node *p, int level, int pos) |
| Write node frequency value. | |
| void | write_leaves (Freq_Node *p, int level, int pos) |
| Write leaf node labels with LaTeX escaping. | |
| size_t | num_digits (const int &n) |
| Compute number of decimal digits in an integer. | |
| void | adjust_nodes (Freq_Node *p, int p_level, int p_infix_pos) |
| Adjust node positions to prevent label overlap. | |
| void | huffman_to_btreepic (Freq_Node *p, const bool with_level_adjust=false) |
| Generate btreepic specification for a Huffman tree. | |
| void | huffman_to_btreepic (Freq_Node *p, std::ostream &output, bool with_level_adjust=false) |
| Generate btreepic specification to an output stream. | |
Variables | |
| DynMapTree< Freq_Node *, Infix_Desc, Treap > | infix_table |
| Map from node pointer to infix position descriptor. | |
| Level_Table | level_table |
| Map from node pointer to level traversal descriptor. | |
| std::ostream * | output_ptr = nullptr |
| Output stream pointer for btreepic commands. | |
| Freq_Node * | pred = nullptr |
| Predecessor node in level-order traversal. | |
| int | minimal_gap = 4 |
| Minimum gap (in nodes) before applying offset adjustment. | |
| constexpr size_t | MAX_OFFSET_INDEX = 7 |
| Maximum valid index for offset array. | |
| const long double | offset [] = { 10, 15, 25, 40, 55, 65, 85, 90 } |
| Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX) | |
Huffman tree visualization for btreepic LaTeX package.
This header provides utilities for generating btreepic specifications from Huffman trees. The btreepic package is a LaTeX tool for drawing binary trees with customizable node labels, positions, and annotations.
The generated output consists of:
start-prefix: Node positions in prefix orderstart-key: Frequency values for each nodeTAG commands: Leaf node labels with symbol namesxoffset commands: Horizontal adjustments for overlapping labelshuffman_to_btreepic() must complete before another call begins.Definition in file huffman_btreepic.H.
| using Level_Table = DynMapTree<Freq_Node *, Level_Desc, Treap> |
Type alias for the level descriptor map.
Definition at line 163 of file huffman_btreepic.H.
| enum Offset |
Horizontal offset direction for node label adjustment.
Used to track whether a node's label has been shifted left or right to avoid overlapping with adjacent nodes at the same level.
| Enumerator | |
|---|---|
| NO_OFFSET | No offset applied. |
| LEFT | Shifted to the left. |
| RIGHT | Shifted to the right. |
Definition at line 107 of file huffman_btreepic.H.
|
inline |
Adjust node positions to prevent label overlap.
Callback for in-order traversal that checks if adjacent nodes at the same level are too close together. If they have opposite orientations, xoffset commands are generated to shift labels apart.
| p | Node pointer |
| p_level | Tree level of node |
| p_infix_pos | In-order position of node |
Definition at line 391 of file huffman_btreepic.H.
References Aleph::BinNode< Key >::get_key(), get_offset(), infix_table, LEFT, level_table, Aleph::maps(), minimal_gap, NO_OFFSET, output_ptr, RIGHT, Aleph::DynSetTree< Key, Tree, Compare >::size(), and Aleph::HTList::size().
Referenced by huffman_to_btreepic().
|
inlinenoexcept |
Get safe offset value with bounds checking.
| index | Symbol length |
Definition at line 195 of file huffman_btreepic.H.
References MAX_OFFSET_INDEX, and offset.
Referenced by adjust_nodes(), TEST_F(), and TEST_F().
|
inline |
Generate btreepic specification for a Huffman tree.
Produces a complete btreepic specification that can be compiled with LaTeX to render the Huffman frequency tree. The output includes:
start-prefix line)start-key line)TAG commands)xoffset commands)| p | Root of the Huffman frequency tree |
| with_level_adjust | If true, apply automatic position adjustments to prevent overlapping labels at dense levels |
Definition at line 476 of file huffman_btreepic.H.
References adjust_nodes(), Aleph::inOrderRec(), Aleph::levelOrder(), Aleph::maps(), output_ptr, Aleph::preOrderRec(), reset_huffman_btreepic_state(), save_infix_pos(), save_level_pos_1(), save_level_pos_2(), write_freq(), write_leaves(), and write_prefix().
Referenced by huffman_to_btreepic(), main(), 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(), and TEST_F().
|
inline |
Generate btreepic specification to an output stream.
Preferred overload that takes an explicit output stream parameter instead of using the global output_ptr.
| p | Root of the Huffman frequency tree |
| output | Output stream for btreepic commands |
| with_level_adjust | If true, apply automatic position adjustments |
Definition at line 523 of file huffman_btreepic.H.
References huffman_to_btreepic(), Aleph::maps(), output, and output_ptr.
|
inline |
Compute number of decimal digits in an integer.
| n | Integer value |
Definition at line 375 of file huffman_btreepic.H.
|
inlinenoexcept |
Reset global state for a new tree processing.
Clears all traversal tables and resets the predecessor pointer. Called automatically at the start of huffman_to_btreepic().
Definition at line 206 of file huffman_btreepic.H.
References Aleph::DynSetTree< Key, Tree, Compare >::empty(), Aleph::DynList< T >::empty(), infix_table, level_table, and pred.
Referenced by huffman_to_btreepic(), HuffmanBtreepicTest::SetUp(), HuffmanBtreepicTest::TearDown(), TEST_F(), TEST_F(), and TEST_F().
|
inline |
Save infix position for a node.
Callback for in-order traversal that records each node's position and level in the infix_table.
| p | Node pointer |
| level | Tree level |
| pos | In-order position |
Definition at line 227 of file huffman_btreepic.H.
References infix_table.
Referenced by huffman_to_btreepic().
|
inline |
First pass level-order callback: record child direction.
Callback for level-order traversal that records whether each node is a left or right child.
| p | Node pointer |
| pos | Position in level (unused) |
| is_left | True if node is a left child |
Definition at line 242 of file huffman_btreepic.H.
References Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), level_table, and Aleph::maps().
Referenced by huffman_to_btreepic().
|
inline |
Second pass level-order callback: link successors.
Callback for level-order traversal that links each node to its successor in level-order.
| p | Node pointer |
| pos | Position in level-order traversal |
| is_left | Unused |
Definition at line 258 of file huffman_btreepic.H.
References Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), level_table, Aleph::maps(), and pred.
Referenced by huffman_to_btreepic().
|
inline |
Write node frequency value.
Callback for in-order traversal that outputs the frequency value of each node as a quoted string for btreepic labels.
| p | Node pointer |
| level | Unused |
| pos | Unused |
Definition at line 305 of file huffman_btreepic.H.
References Aleph::BinNode< Key >::get_key(), Aleph::maps(), and output_ptr.
Referenced by huffman_to_btreepic().
|
inline |
Write leaf node labels with LaTeX escaping.
Callback for in-order traversal that outputs TAG commands for leaf nodes (symbols). Handles special LaTeX characters and whitespace with appropriate escaping.
| p | Node pointer |
| level | Unused |
| pos | In-order position |
Definition at line 325 of file huffman_btreepic.H.
References Aleph::BinNode< Key >::get_key(), Aleph::LLINK(), Aleph::maps(), output_ptr, and Aleph::RLINK().
Referenced by huffman_to_btreepic().
|
inline |
Write node position in prefix order.
Callback for prefix-order traversal that outputs the infix position of each node, building the btreepic structure specification.
| p | Node pointer |
| level | Unused |
| prefix_pos | Unused |
Definition at line 286 of file huffman_btreepic.H.
References infix_table, and Aleph::maps().
Referenced by huffman_to_btreepic().
|
inline |
Map from node pointer to infix position descriptor.
Definition at line 170 of file huffman_btreepic.H.
Referenced by adjust_nodes(), reset_huffman_btreepic_state(), save_infix_pos(), TEST_F(), TEST_F(), and write_prefix().
|
inline |
Map from node pointer to level traversal descriptor.
Definition at line 173 of file huffman_btreepic.H.
Referenced by adjust_nodes(), reset_huffman_btreepic_state(), save_level_pos_1(), save_level_pos_2(), TEST_F(), and TEST_F().
|
inlineconstexpr |
Maximum valid index for offset array.
Definition at line 185 of file huffman_btreepic.H.
Referenced by get_offset().
|
inline |
Minimum gap (in nodes) before applying offset adjustment.
Definition at line 182 of file huffman_btreepic.H.
Referenced by adjust_nodes(), TEST_F(), TEST_F(), and TEST_F().
|
inline |
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Definition at line 188 of file huffman_btreepic.H.
Referenced by Aleph::Dnode< T >::data_to_node(), Aleph::find_position(), get_offset(), RingFileCache< T >::is_valid_offset(), Aleph::DynHashTable< Key, HashTable, Cmp >::key_to_bucket(), Aleph::ODhashTable< Key, Cmp >::key_to_bucket(), Aleph::OLhashTable< Key, Cmp >::key_to_bucket(), Aleph::parallel_zip_detail::make_iterators_at(), Aleph::Init_Arc< GT >::operator()(), Aleph::pall(), Aleph::pcount_if(), Aleph::penumerate_for_each(), Aleph::penumerate_for_each(), Aleph::penumerate_maps(), Aleph::pexists(), Aleph::pfilter(), Aleph::pfind(), Aleph::pfoldl(), Aleph::pfor_each(), Aleph::pfor_each(), Aleph::pmaps(), Aleph::pmax(), Aleph::pmin(), Aleph::pminmax(), Aleph::ppartition(), process_text_arc(), Aleph::pzip_all_n(), Aleph::pzip_count_if_n(), Aleph::pzip_exists_n(), Aleph::pzip_foldl(), Aleph::pzip_foldl_n(), Aleph::pzip_for_each(), Aleph::pzip_for_each_n(), Aleph::pzip_maps(), Aleph::pzip_maps_n(), Aleph::ArrayQueue< T >::rear_item(), Aleph::DynLhashTable< Key, Record, Cmp >::record_to_bucket(), and Aleph::sift_down_subrange().
|
inline |
Output stream pointer for btreepic commands.
Definition at line 176 of file huffman_btreepic.H.
Referenced by adjust_nodes(), huffman_to_btreepic(), huffman_to_btreepic(), main(), HuffmanBtreepicTest::SetUp(), HuffmanBtreepicTest::TearDown(), 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_freq(), and write_leaves().
|
inline |
Predecessor node in level-order traversal.
Definition at line 179 of file huffman_btreepic.H.
Referenced by Aleph::DynSkipList< Key, Compare >::all(), Aleph::DynSkipList< Key, Compare >::all(), Aleph::all_of_tuple(), Aleph::zip_index_detail::all_of_tuple_impl(), Aleph::all_perm(), Aleph::all_perm(), Aleph::any_of_tuple(), Aleph::zip_index_detail::any_of_tuple_impl(), GraphCommon< GT, Node, Arc >::collect_arcs_if(), Aleph::count_if(), Aleph::FunctionalMixin< Derived, Type >::count_if(), Aleph::drop_while(), Aleph::DynSkipList< Key, Compare >::exists(), Aleph::DynSkipList< Key, Compare >::exists(), Aleph::exists_perm(), Aleph::exists_perm(), Aleph::detail::fallback_all_of(), Aleph::detail::fallback_any_of(), Aleph::detail::fallback_none_of(), Aleph::filter_perm(), Aleph::filter_perm(), Aleph::Filter_Iterator< Container, It, Show_Item >::find_if(), Aleph::FunctionalMixin< Derived, Type >::find_index(), Aleph::find_opt(), Aleph::find_opt(), Aleph::find_ptr(), Aleph::find_ptr(), Aleph::find_ptr(), Aleph::find_ptr(), Aleph::GenTdRbTree< NodeType, Key, Compare >::findPredAndSwap(), Aleph::GenTdRbTreeRk< NodeType, Key, Compare >::findPredAndSwap(), Aleph::DynSkipList< Key, Compare >::none(), Aleph::DynSkipList< Key, Compare >::none(), Aleph::FunctionalMixin< Derived, Type >::none(), Aleph::FunctionalMixin< Derived, Type >::none(), Aleph::none_of_tuple(), Aleph::none_perm(), Aleph::none_perm(), Aleph::pall(), Aleph::pcount_if(), Aleph::pexists(), Aleph::pfilter(), Aleph::pfind(), Aleph::pfind_value(), Aleph::pnone(), Aleph::ppartition(), Aleph::pzip_all_n(), Aleph::pzip_count_if_n(), Aleph::pzip_exists_n(), Aleph::detail::ranges_all_of(), Aleph::detail::ranges_any_of(), Aleph::detail::ranges_count_if(), Aleph::detail::ranges_none_of(), GraphCommon< GT, Node, Arc >::remove_arcs_if(), reset_huffman_btreepic_state(), save_level_pos_2(), Aleph::stl_adjacent_all(), Aleph::stl_adjacent_exists(), Aleph::stl_all(), Aleph::stl_any(), Aleph::stl_count(), Aleph::stl_drop_while(), Aleph::stl_exists(), Aleph::stl_filter(), Aleph::stl_filteri(), Aleph::stl_find(), Aleph::stl_find_index(), Aleph::stl_find_last(), Aleph::stl_none(), Aleph::stl_partition(), Aleph::stl_reject(), Aleph::stl_span(), Aleph::stl_take_while(), Aleph::stl_zip_all(), Aleph::stl_zip_all_eq(), Aleph::stl_zip_any(), Aleph::stl_zip_count(), Aleph::stl_zip_drop_while(), Aleph::stl_zip_exists(), Aleph::stl_zip_filter(), Aleph::stl_zip_filteri(), Aleph::stl_zip_find_first(), Aleph::stl_zip_find_index(), Aleph::stl_zip_find_last(), Aleph::stl_zip_none(), Aleph::stl_zip_partition(), Aleph::stl_zip_take_while(), Aleph::stl_zip_traverse(), Aleph::stl_zip_traverse_eq(), Aleph::take_while(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), Aleph::uni_all(), Aleph::uni_any(), Aleph::uni_count(), Aleph::uni_drop_while(), Aleph::uni_exists(), Aleph::uni_filter(), Aleph::uni_filteri(), Aleph::uni_find(), Aleph::uni_find_index(), Aleph::uni_none(), Aleph::uni_partition(), Aleph::uni_take_while(), Aleph::uni_zip_all(), Aleph::uni_zip_all_eq(), Aleph::uni_zip_any(), Aleph::uni_zip_count(), Aleph::uni_zip_drop_while(), Aleph::uni_zip_exists(), Aleph::uni_zip_filter(), Aleph::uni_zip_filteri(), Aleph::uni_zip_find_first(), Aleph::uni_zip_none(), Aleph::uni_zip_partition(), Aleph::uni_zip_take_while(), Aleph::uni_zip_traverse(), Aleph::zip_drop_while(), and Aleph::zip_take_while().