1

I need to return an ordered list of references with an empty in between them after traversing a BST, but i can't workout how to do it.

def inorder(self, Node):

    res = ""
    if Node != None:

        self.inorder(Node.LeftChild())
        res = res +(Node.key + " " + Node.payload + "\n")
        self.inorder(Node.RightChild())

    return res

this is my code, so far i can only get one reference returned, any help will be much appreciated.

3
  • 3
    you process left branch and throw the result away. Same for right branch. Your method has a return value, use it. Commented Mar 19, 2020 at 22:29
  • thanks @Lesiak, i'm new at this, won't you be able to provide an example how to do it? Commented Mar 19, 2020 at 22:34
  • Variable and function names should follow the lower_case_with_underscores style. Commented Mar 19, 2020 at 23:07

1 Answer 1

1

You almost had it right, just need to use the return value of the right and the left sub trees in your res variable

def inorder(self, Node):

    res = ""
    if Node != None:

        lres = self.inorder(Node.LeftChild())
        rres = self.inorder(Node.RightChild())
        res = lres + (Node.key + " " + Node.payload + "\n") + rres

    return res

Note: res was always empty string in your condition, so you did not need to do res = res+, also there is a shorthand notation for doing this res+= but you didn't need it anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.