Functions
heap_print
Prints a heap from root to leaves in a hierarchical format.The heap object to print (must have
count() and enumerate() methods)tree_print
Prints a tree from root to leaves in a hierarchical format.The tree object to print (must have a
root attribute)This function converts the tree into a complete tree array representation before printing, filling empty nodes with empty strings to maintain the structure.
Helper Functions
tree_to_array
Converts a node-based tree into an array representation with index-value pairs.The starting node
The starting index (used internally during recursion)
The destination array (used internally during recursion)
List of (index, value) tuples representing the tree in array form
Example
get_tree_height
Calculates the height of a tree.The starting node (root)
The height of the tree (number of levels)
Example
fill_complete_tree
Forces a binary tree to be a complete tree by filling empty nodes.The tree to fill (must have a
root attribute)Array representation of the complete tree with empty strings for missing nodes
Example
Understanding the Output Format
Understanding the Output Format
Hierarchical Level Display:The Spacing:
heap_print and tree_print functions display data structures level by level, making it easy to visualize the tree structure:- Each level has appropriate spacing to align with parent nodes
- Nodes are centered within their column width
- Column width is calculated based on tree height for proper alignment
- In
tree_print, missing nodes are shown as empty spaces - This helps visualize incomplete trees
- Debugging heap operations
- Visualizing tree structure during development
- Educational purposes for understanding tree/heap layout
- Quick console-based visualization without GUI dependencies