LMPTHW question ex23 solved

I’m printing off the attributes to the master copy code (ex23) we are to memorize, and curious what the TSTreeNode, self.eq is supposed to be… I thought ‘equals’ at first, because there was a low and high, but when I print the tree stuff off, low is None, high is None, and eq leads to the next letter in a string? I’m using this print off method:
Realized I needed to add more sets to see high low in action.

class PrintTSTree(object):
    def graphical(self, branch):
        if branch != None:
            print("branch.key = ", branch.key)
            print("branch.eq = ", branch.eq)
            print("branch.low = ", branch.low)
            print("branch.high = ", branch.high)
            print("branch.value = ", branch.value)
            if branch.eq != None:
                self.graphical(branch.eq)
            if branch.high != None:
                print(" \n >>>>>>>>>>>>>>>>> greater >>>> \n")
                print("branch high = ")
                self.graphical(branch.high)
            if branch.low != None:
                print("\n <<<<<<<<<<<<<<<<< lower <<<<<< \n")
                print("branch low = ")
                self.graphical(branch.low)
        else:
            print("<<<< branch end >>>>")

Yes, that’s the correct way to use it. eq is equals, and you won’t see the other branches (lo/hi) unless there’s a string that goes that direction. What the TST does is as long as you follow eq you’ll find the string. If it does eq then you know that the string diverges at the point, either lower in alphabet or higher.

1 Like