dsa.pretty_print

Module to access functions for a clearer visual representation of certain data structures.

  1""" Module to access functions for a clearer visual representation of certain data structures. """
  2import math
  3
  4def heap_print(heap):
  5    """
  6    Print a heap from root to leaves.
  7
  8    Args:
  9        heap: The heap object to print.
 10    """
 11    if not heap or heap.count() == 0:
 12        return
 13
 14    _array_print(heap.count(), heap.enumerate())
 15
 16
 17def _array_print(node_count, enum_sequence):
 18    """
 19    Print a heap from root to leaves.
 20
 21    Args:
 22        heap: The heap object to print.
 23    """
 24    height = math.floor(math.log2(node_count))
 25    level_str = ""
 26    current_level = 0
 27    value_width = 3
 28    max_width = 2 ** (height - 1) * value_width
 29
 30    for index, node in enum_sequence:
 31        level = int(math.log2(index + 1))
 32        columns = 2 ** (level - 1)
 33        column_width = int(max_width / columns)
 34        if current_level != level:
 35            current_level = level
 36            print(level_str)
 37            level_str = ""
 38        level_str += f"{node:^{column_width}}"
 39    print(level_str)
 40    print()
 41
 42
 43def tree_to_array(node, index: int=0, tree_array=None):
 44    """
 45    (helper function) Create an array filled with index and value pairs from a node based tree.
 46
 47    Args:
 48        node: The starting node.
 49        index (int): The starting index.
 50        tree_array: The destination array.
 51
 52    Returns:
 53        Array filled with tree values.
 54    """
 55    if not tree_array:
 56        tree_array = []
 57    if node is None:
 58        return
 59    tree_array.append((index, node.value))
 60    tree_to_array(node.left, index * 2 + 1, tree_array)
 61    tree_to_array(node.right, index * 2 + 2, tree_array)
 62    
 63    return tree_array
 64
 65def get_tree_height(node) -> int:
 66    """
 67    (helper function) Calculate the height of a tree.
 68
 69    Args:
 70        node: The starting node.
 71
 72    Returns:
 73        The height of a tree.
 74    """
 75    if node is None:
 76        return 0
 77    else:
 78        return max(get_tree_height(node.left) + 1, get_tree_height(node.right) + 1)
 79
 80def fill_complete_tree(tree):
 81    """
 82    (helper function) Force a binary tree to be a complete tree by filling any empty nodes.
 83
 84    Args:
 85        tree: The tree to fill.
 86
 87    Returns:
 88        A new tree that is complete.
 89    """
 90    if tree is None:
 91        return None
 92    
 93    tree_array = tree_to_array(tree.root)
 94    if tree_array is None:
 95        return
 96    
 97    tree_height = get_tree_height(tree.root)
 98
 99    # build empty complete tree
100    array_size = (2 ** tree_height) - 1
101    new_tree = [ "" ] * array_size
102
103    # fill the complete tree array
104    for index, value in tree_array:
105        new_tree[index] = value
106    return new_tree
107
108def tree_print(tree):
109    """
110    Print a tree from root to leaves.
111
112    Args:
113        tree: The tree object to print.
114
115    Notes:
116        Reuses heap_print() by converting tree into a complete tree array.
117    """
118    complete_tree = fill_complete_tree(tree)
119    if complete_tree:
120        _array_print(len(complete_tree), enumerate(complete_tree))
def heap_print(heap):
 5def heap_print(heap):
 6    """
 7    Print a heap from root to leaves.
 8
 9    Args:
10        heap: The heap object to print.
11    """
12    if not heap or heap.count() == 0:
13        return
14
15    _array_print(heap.count(), heap.enumerate())

Print a heap from root to leaves.

Args: heap: The heap object to print.

def tree_to_array(node, index: int = 0, tree_array=None):
44def tree_to_array(node, index: int=0, tree_array=None):
45    """
46    (helper function) Create an array filled with index and value pairs from a node based tree.
47
48    Args:
49        node: The starting node.
50        index (int): The starting index.
51        tree_array: The destination array.
52
53    Returns:
54        Array filled with tree values.
55    """
56    if not tree_array:
57        tree_array = []
58    if node is None:
59        return
60    tree_array.append((index, node.value))
61    tree_to_array(node.left, index * 2 + 1, tree_array)
62    tree_to_array(node.right, index * 2 + 2, tree_array)
63    
64    return tree_array

(helper function) Create an array filled with index and value pairs from a node based tree.

Args: node: The starting node. index (int): The starting index. tree_array: The destination array.

Returns: Array filled with tree values.

def get_tree_height(node) -> int:
66def get_tree_height(node) -> int:
67    """
68    (helper function) Calculate the height of a tree.
69
70    Args:
71        node: The starting node.
72
73    Returns:
74        The height of a tree.
75    """
76    if node is None:
77        return 0
78    else:
79        return max(get_tree_height(node.left) + 1, get_tree_height(node.right) + 1)

(helper function) Calculate the height of a tree.

Args: node: The starting node.

Returns: The height of a tree.

def fill_complete_tree(tree):
 81def fill_complete_tree(tree):
 82    """
 83    (helper function) Force a binary tree to be a complete tree by filling any empty nodes.
 84
 85    Args:
 86        tree: The tree to fill.
 87
 88    Returns:
 89        A new tree that is complete.
 90    """
 91    if tree is None:
 92        return None
 93    
 94    tree_array = tree_to_array(tree.root)
 95    if tree_array is None:
 96        return
 97    
 98    tree_height = get_tree_height(tree.root)
 99
100    # build empty complete tree
101    array_size = (2 ** tree_height) - 1
102    new_tree = [ "" ] * array_size
103
104    # fill the complete tree array
105    for index, value in tree_array:
106        new_tree[index] = value
107    return new_tree

(helper function) Force a binary tree to be a complete tree by filling any empty nodes.

Args: tree: The tree to fill.

Returns: A new tree that is complete.

def tree_print(tree):
109def tree_print(tree):
110    """
111    Print a tree from root to leaves.
112
113    Args:
114        tree: The tree object to print.
115
116    Notes:
117        Reuses heap_print() by converting tree into a complete tree array.
118    """
119    complete_tree = fill_complete_tree(tree)
120    if complete_tree:
121        _array_print(len(complete_tree), enumerate(complete_tree))

Print a tree from root to leaves.

Args: tree: The tree object to print.

Notes: Reuses heap_print() by converting tree into a complete tree array.