0

I'm doing a programming assignment for my class. One of my programs includes creating a BST with Python within a BST class, and I'm trying to display using preorder() and inorder() functions. My professor gave us certain parameters for the assignment, so I've had to code in constraints, but I am struggling with the implementation. I'm not allowed to use self in preorder() and inorder() based on given parameters, nor am I allowed to insert parameters for display(). I want to use the f/function in preorder() and inorder()to call print inside my display() method when calling the functions. X is the value of the root inside the lambda function. However, I know there's a gap in my understanding based on my logic and the problems I'm encountering.

For reference, here is a snippet of my code.

def inorder(f):
   return tree.inorderHelper(root, f)

def inorderHelper(root, f):
    if root:
        tree.inorderHelper(root.left, f)
        f(root.x)
        tree.inorderHelper(root.right, f)  

def preorder(f):
    return tree.preorderHelper(root, f)

def preorderHelper(root, f):
    if root:
        f(root.x)
        tree.preorderHelper(root.left, f)
        tree.preorderHelper(root.right, f)

@staticmethod
def display():
    if random.randint(0,1) == 0:
        tree.preorder(lambda x: print(x, end=" "))
    elif random.randint(0,1) == 1:
        tree.inorder(lambda x: print(x, end=" "))

if __name__ == "__main__":
  print("Hello world!")
  tree = tree()
  root = None
  for i in range(4):
      print(i)
      tree.ins(i)
  tree.display()
  print("Testing preorder")

I've tried creating a printInorder(root) or printPreorder(root) traversal method with similar logic instead of preorder() and inorder() directly, but I always get an error saying that I'm having issues with using too many arguments. I also tried using lambda directly, but I know that's not correct either; I'm getting the same error with arguments. Any help would be appreciated.

5
  • 1
    This code is not valid. @staticmethod can only be used in a class, but there is none. What is the tree class? Also self is just a name. An instruction to not use self is useless, as it is just whatever you name the first parameter. So if you call it this or that or tree, ... its all the same purpose. Commented Sep 2 at 21:07
  • 1
    This question is not clear. Please provide the literal instructions you got, with all the code that was provided as part of the code challenge. Commented Sep 2 at 21:12
  • Unrelated, but what is this second call of randint good for? What if neither the if, nor the elif condition is true? Are you really using a method that accesses tree as a global variable? If that is what your teacher told you to do, I would strongly advise to look for a different course. That's bad practice. Commented Sep 2 at 21:14
  • It looks as though you are missing a class definition, as @trincot mentioned. One way to avoid confusion would be to avoid using variable names in functions that match variables out of scope (i.e. tree). If you are using an IDE, try commenting out everything within your if __name__ == "__main__", and see what turns red. Ideally, no function in this script would rely on the global scope... but depending on how literal "... nor am I allowed to insert parameters for display() ..." is, you may be forced to Commented Sep 2 at 21:34
  • This isn’t reproducible yet. Please include the full class (or a minimal example) with the Node definition and the ins method. Right now inorder/preorder and display reference tree/root as globals, and @staticmethod prevents using self. Also display() calls randint twice which can pick different branches. Post a small runnable example and the exact error message you get. Commented Sep 3 at 13:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.