I am having a bit of a hard time with a recursive insert function that I been studying through zybooks data structures.I didn't have any issues with a normal insert but with the recursive it is confusing me a lot. The function is gathering the tree information as a parameter. The keys are the names which are all unique. I haven't inserted anything in the parameter to put in the function in the else statement. Would appreciate some pointers if possible.
class Star:
def __init__(self, hvg_db, name, mag, spectral, habit, dist):
self.hvg_db = hvg_db
self.display_name = name #
self.magnitude = mag
self.spectral_class = spectral
self.habitable = habit #
self.distance_parsecs = dist
class TreeNode:
def __init__(self, star):
self.left = None
self.right = None
self.star_info = star
self.name = "N" + str(self.star_info.hvg_db)
self.key = star.display_name
class Tree:
def __init__(self, name):
self.name = name
self.node_num = 0
self.node_list = []
self.root = None
def insert_rec(self, star): # method receives star info (star, magnitude, habitable, etc
star = TreeNode(star)
if self.root is None:
self.root = star
print("Parent Activated")
parent = self.root
if star.key < parent.key:
if parent.left is None:
print("Left is Empty")
parent.left = star
else:
print("Else Left")
self.insert_rec(star)
if parent.right is None:
print("Right is Empty")
parent.right = star
else:
print("Else Right")
self.insert_rec(star)
def main():
# Instantiate Binary Tree to hold the stars
star_tree = Tree("Star Catalog")
with open('HabHYG_short.csv', 'r') as csvfile:
lines = csv.reader(csvfile, delimiter=',')
# skip header row
next(csvfile)
# get time in nanoseconds -- maybe OS-specific?
# See https://docs.python.org/3/library/time.html
t0 = time.perf_counter_ns()
obs_processed = 0
for row in lines:
# hvg_db, name, mag, spectral, habit, dist)
this_star = Star(row[0], row[3], row[16], row[11], row[2], row[12])
star_tree.insert_rec(this_star)