Pretty printing branches of a Python binary tree.
class BinTree:
def __init__(self, key, left, right):
self.key = key
self.left = left
self.right = right
Place this file next to the file in which you want to use the function:
your_folder/
|-- your_file.py
|-- pretty_print.py
Add this line to your file:
from pretty_print import *
# your code
Display your BinTree this way:
print_tree(B)
# your_file.py
from pretty_print import *
B = BinTree(1, BinTree(2, None, None), BinTree(3, None, None))
print_tree(B)
>>>>
1
/ \
2 3
- Key length is not taken into account.