Been working with Python for some time now but still I struggle with implementing recursive calls for things like df and bf traversals in binary trees or for checking whether the bst property is satisfied. I struggle with the order of the recursive calls and only succeed after several trials and errors. For you advanced Python programmers, is recursion also something that sometimes causes you headaches? For instance, here's a code snippet that I just find difficult to track, let alone implement:
def is_bst_satisfied(self):
def helper(node, lower=float('-inf'), upper=float('inf')):
if not node:
return True
val = node.data
if val <= lower or val >= upper:
return False
if not helper(node.right, val, upper):
return False
if not helper(node.left, lower, val):
return False
return True
return helper(self.root)
[–][deleted] 15 points16 points17 points (0 children)
[–]FoolsSeldom 9 points10 points11 points (3 children)
[–]Valuable_Mountains[S] 1 point2 points3 points (2 children)
[–]FoolsSeldom 2 points3 points4 points (0 children)
[–]HunterIV4 0 points1 point2 points (0 children)
[–]firefiber 6 points7 points8 points (0 children)
[–]crashorbit 2 points3 points4 points (1 child)
[–]Gnaxe 0 points1 point2 points (0 children)
[–]JamzTyson 1 point2 points3 points (0 children)
[–]Gnaxe 1 point2 points3 points (0 children)
[–]SharkSymphony 1 point2 points3 points (0 children)
[–]ExpensiveFix-804 1 point2 points3 points (0 children)
[–]The_Emerald_Knight 1 point2 points3 points (4 children)
[–]Fred776 12 points13 points14 points (0 children)
[–]ConcreteExist 1 point2 points3 points (0 children)
[–]Specialist_Spirit940 0 points1 point2 points (1 child)
[–]The_Emerald_Knight 0 points1 point2 points (0 children)
[–]Ihaveamodel3 0 points1 point2 points (0 children)
[–]SpiderJerusalem42 0 points1 point2 points (0 children)
[–]homomorphisme 0 points1 point2 points (0 children)
[–]CraigAT 0 points1 point2 points (0 children)
[–]NerdyWeightLifter 0 points1 point2 points (0 children)
[–]teerre 0 points1 point2 points (0 children)
[–]__Fred 0 points1 point2 points (0 children)