I made this print class that I can call on my initiated binary tree to print off it’s structure in my command shell. It made finding bugs, and seeing what data was doing easier for me.
You’ll probably have to modify it a bit. My root is pre-set before the tree is filled.
so to use:
import BinarySearchTree
import PrintTree
myTree = BinarySearchTree(*your root, or NONE if you don't set it here)
myPrint = PrintTree()
myPrint.graphical(myTree.root)
class PrintTree(object):
def graphical(self, branches):
if branches != None:
righty = branches.right
lefty = branches.left
print("branch")
print(f" {branches} ")
print(f" {branches.key} ")
print(" /// \\\\ ")
print(" // \\ ")
print(" / \\ ")
if righty and lefty:
print(f" \n {lefty.key} ______ ___>>>___ ______ {righty.key} \n")
elif righty and not lefty:
print(f" \n None ________ >>__________ {righty.key} \n ")
elif lefty and not righty:
print(f" \n {lefty.key} ______ ___>>>___ ______ None \n ")
else:
print(f" NONE _________________>>>___________________NONE")
if branches == None:
righty = None
lefty = None
if righty:
print("righty")
print(f" {righty.key} ")
print(" /// \\\\ ")
print(" // \\ ")
print(" / \\ ")
left_child = righty.left
right_child = righty.right
if left_child or right_child:
print(f" \n {righty.left} ___>>>___{righty.right} \n")
if not left_child and not right_child:
print(" NONE ......... ......... NONE ")
if left_child:
self.graphical(left_child)
if right_child:
self.graphical(right_child)
if lefty:
print("lefty")
print(f" {lefty.key} ")
print(" /// \\\\ ")
print(" // \\ ")
print(" / \\ ")
left_child = lefty.left
right_child = lefty.right
if left_child or right_child:
print(f" \n {lefty.left}____>>>___ {lefty.right} \n ")
if not left_child and not right_child:
print(f" {left_child} .......__________....... {right_child}")
if left_child:
self.graphical(left_child)
if right_child:
self.graphical(right_child)
if righty == None and lefty == None:
print(f"EMPTY Branch = {branches}")