简体   繁体   中英

Understanding Logic of Inorder traversal of BST, Python

I'm studying for coding interviews and working with a lot of different data structures.

I'm relatively new to Tree problems and have been doing problems daily to practice.

It's one thing to have formulas committed to memory, and another to truly understand them. When I understand something it is easy to apply that understanding to more difficult problems.

Recursive solutions are a little harder for me to mentally visualize and while intuitively they make sense, I am trying to get a deep understanding of what's happening on the Stack.

I have a tree and want to do in order traversal. No problem.

在此处输入图片说明

data = [] 

def checkBST(root):
    if root:
        checkBST(root.left)
        data.append(root.data)
        checkBST(root.right)
    print(data)   

I created the data variable to print out what is being stored on each pass through the method.

It printed

[]
[1]
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]

I am trying to logically look at what is happening and want to know if my Logic is correct.

There are 15 printed results and 7 Nodes. However we get to 15 because there are 8 places checked for Nodes where Node is None . That happens on Nodes 1, 3, 5, 7.

We are checking the left half of the tree before the right.

[] 
#nothing stored because we move onto Node 2 as we don't hit the base case.
[1] 
#1 stored because Node 1 doesn't have a left value. So we move onto the append call. 
[1] 
#1 returned because Node 1 doesn't have a right value.
[1, 2] 
#2 stored because because we finished checking the left side and moved onto append data.
[1, 2, 3] 
#3 is stored because we are calling the in order traversal on the right side of two now. 
[1, 2, 3]
#3 is returned again because it doesn't have a root.left
[1, 2, 3]
#3 is returned again because it doesn't have a root.right
[1, 2, 3, 4]
# we hit the append method for 4, now we move onto the in order traversal on the right
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]

The right side will be checked just like the left so I didn't write my logic as it'd be redundant.

I just want clarification if I am looking at this problem in the correct format.

Thanks for any help in understanding this!

The comments in the output are not always correct.

The first output ( [] ) happens when a function call reaches the end. The first call where this happens is when root is node 1 and from there the first recursive call is made. That call will have None as argument, and so it is the first time a call reaches the print statement.

So we have these ongoing calls:

 checkBST(4)
     checkBST(2) # left child of 4
         checkBST(1) # left child of 2
             checkBST(None) # left child of 1
                 print # --> []

When that deepest call finishes, the function with node 1 will append 1 to the data list, and then make the recursive call for the right child, which also is None and [1] is printed.

Here is a visualisation of the process. The columns represent the depth of the recursion, and the rows represent the sequence of events as time progresses (downwards). The last column is reserved for showing what the current value of data is. When it has a yellow background it means it is printed.

Light blue means that code is executed at that recursion depth. Darker blue means the corresponding function call is pending (on the stack), waiting for the nested recursive call to return.

在此处输入图片说明

From this image you can also see why the same output is sometimes repeated: it is printed at different recursion levels as the algorithm is backtracking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM