DEV Community

Discussion on: Demystifying Depth-First Search

Collapse
 
zhentzhou profile image
Zhenting Zhou

I think the tree you show has a mistake when you did the in-order depth search the tree should be like this graph.

                                F
                            /         \
                         D            G
                       /   \          /    \
                    B     E        H      L
                   /  |             /
                 A   C            I
                                     \
                                      J
                                    /
                                    K
Enter fullscreen mode Exit fullscreen mode

Or the order wouldn't be A,B,C,D,E,F,G,H,I,J,K,L.
From the way you graphed it...

                                F
                            /         \
                         D             K
                       /   \          /    \
                    B     E        G      L
                   /  |             /
                 A   C           H
                                     \
                                     J
                                    /
                                   I
Enter fullscreen mode Exit fullscreen mode

The in-order result would be A,B,C,D,E,F,I,J,H,G,K,L.

Collapse
 
vandanav profile image
Vandana-V

I think the right order would be A,B,C,D,E,F,H,I,J,G,K,L. As H doesn't have any left child, H would be printed first and then comes I and J because the in-order goes Left Data Right. Correct me if I'm wrong.