0

I have to create and return a string representation of the in-order traversal of a Binary Search Tree. Below I do have the in order traversal, but I can not figure out how to change this into string form. I did try using + to concatenate them together, however it doesn't run on the terminal. I can also post my whole BST code if need. Please and thank you.

def in_order(self):
    if self.__root is not None:
        self._in(self.__root)

def _in(self, root):
    if root is None:
        return
    if root is not None:
        self._in(root.lchild)
        print(str(root.value)) 
        self._in(root.rchild)
oppa76
  • 21
  • 4

1 Answers1

0

Concatenate and return:

def in_order(self):
    if self.__root is not None:      
        return self._in(self.__root)

def _in(self, root):
    if root is None:
        return ''

    if root is not None:
        left = self._in(root.lchild)
        right = self._in(root.rchild)
        return left + ' ' + str(root.value) + ' ' + right

You have to print the result of calling obj.in_order(): print(obj.in_order()). If you want them one per line, replace ' ' with '\n' in both of its occurrences.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • So in the terminal the code I had would give back the numbers in order, but one per line. When I tried to concatenate like you told me the terminal now gives nothing back – oppa76 Jun 28 '17 at 04:44