Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inorder Traversal of Tree in Python Returning a List

I learnt implementing inorder traversal of a binary search tree:

def inorder(root): # root has val, left and right fields
    if root==None:
        return

    inorder(root.left)
    print(root.val)
    inorder(root.right)

Now, the problem is I do not want console output. I want to get the values in a list. I can't find a way to make the function return a list.

I tried s = [inorder(root)] but it doesn't work.

So, my question is:

  1. Any way this could be done inside the inorder function, i.e. it should return a list rather than just print values.

  2. Is there some generic way to make recusive functions return data structures instead of just outputting a print to console?

like image 312
mourinho Avatar asked Mar 02 '18 05:03

mourinho


People also ask

How do you write inorder traversal in Python?

In-order Traversal Then we create a insert function to add data to the tree. Finally the Inorder traversal logic is implemented by creating an empty list and adding the left node first followed by the root or parent node. At last the left node is added to complete the Inorder traversal.

What is the result of inorder traversal?

As the root node is traversed between the left and right subtree, it is named inorder traversal. So, in the inorder traversal, each node is visited in between of its subtrees. It is used to get the BST nodes in increasing order. It can also be used to get the prefix expression of an expression tree.

How do you traverse through a tree in Python?

Now, there are 3 main methods for tree traversal in python with recursion using DFS which are: Inorder Traversal (left, root, right) Preorder Traversal (root, left, right) Postorder Traversal (left, right, root)


2 Answers

You can build up the list recursivly. Simply add the returned list from the left and right trees together with the value in the current node.

def inorder(root):
    if root==None:
        return []

    left_list = inorder(root.left)
    right_list = inorder(root.right)
    return left_list + [val] + right_list 
like image 172
Shaido Avatar answered Sep 30 '22 19:09

Shaido


You can pass a list, and then append the values to it, like this-

def inorder(root,ans): # root has val, left and right fields
    if root==None:
        return

    inorder(root.left)
    ans.append(root.val)
    inorder(root.right)
ans=[]
inorder(root,ans)
print(ans)

Answering your second query:

Passing the data structure itself is the simplest solution. If you really want the function to "return" the output, One way is using list concatenation as @Shaido suggested, but it is slightly heavier on memory by unnecessarily creating a new singleton list at every recursive call.

A better solution would be using some static list(i. e. a fixed list that would be declared only once). But it's not available directly in python, because python suggests doing it by declaring it inside a class. (A good discussion here)

like image 34
Udayraj Deshmukh Avatar answered Sep 30 '22 19:09

Udayraj Deshmukh