Add a visualize_heapq(heap: list) function into the `heapq` module

Hi, very new contributor-wannabe here

Problem - heapq might be difficult for beginners to understand and visualize

Solution - in the heapq module, add a visualize_heapq(heap: list) to visualize the heap (which is actually a list) as an actual binary tree repr

Example: GitHub - zlliu246/visualize_heapq (have written a simple module for this on pypi)

Thoughts?

Meta question: have not contributed to this repo before, would this kind of ideas be too trivial to be considered?

Thanks & cheers

4 Likes

Why not make it more general:.

def print_complete_btree(btree: Sequence, *, print_func: Callable[[str],Any] = print):
    """Prints complete binary tree in human readable format.
    
    Args:
        btree: Sequence containing elements of complete binary tree to be printed.
        print_func: Function to which human readable lines are passed (``str`` without line break).
    """

there’s a general function print_btree that prints some binary tree, which this visualize_heapq function uses - that’s probably the more general version

What I mean is drooping unneeded requierments.

  1. Why list[Any]? U are not modifing the sequence, so why can’t someone pass on a tuple. Why must they pass build-in type.
  2. Why heap? U are not checking if it’s a heap, so why can’t someone pass on not yet heapified tree. For example, to show what heapify does.
  3. Why only std output? With Ur code using print variable, someone can already override this global, so why not put it in locals. Makeing it easier to print (for example) to file.

All of that without significantly changeing Ur code. So just why?

Btw, U don’t need to build node based tree