Python 3 Scripts for Depth-Priority Discovery

Asked 2 years ago, Updated 2 years ago, 24 views

In Visual Studio 2019 (python), the following python 3 scripts will be displayed in the order in which depth-first searches go. Attempted to debug and step in.

 tree=[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]]

def search(pos):
    print(pos, end='')
    for i in tree [pos]:
        search(i)
search(0)

1, search(0)pos is 0, i is 1.
2, search(1) pos is 1, i is 3.
3, search(3) pos is 3, i is 7.
4, search(7)pos is 7, i is an empty list, so I don't know.
5, search(7)pos is 3, i is 7.
6, search(7)pos is 3, i is 8.
Continuing.

In search(7) above, it seems that i pointed to the empty list and went somewhere.
I wonder if that's why you went back again,
5, search(7)pos is 3, i is 7.After that,
6, search(7)pos is 3, i is 8.

4. Please tell me the reason why the pos returns to 3 when i is an empty list in search(7).
Also, please tell me why 6, search(7)pos is 3, i is 8.

python

2022-09-30 15:32

1 Answers

4. Please tell me why the pos returns to 3 when i is an empty list in search(7).

Search(7) has been called by search(3), so when search(7) is finished, search(3) will be returned.

Also, please tell me why 6, search(7)pos is 3, i is 8.

This is because search(3) handles 7 of tree[3] and then 8 of [7,8].

If you run the following code with the research print,

 tree=[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]]
def search(pos):
    print("START pos=", end='')
    print(pos)
    for i in tree [pos]:
        print("i=", end='")
        print(i)
        search(i)

    print("END pos=", end='")
    print(pos)
search(0)

The following results can be obtained:字I have corrected the indentation by hand.

START pos=0
      i = 1
      START pos = 1
            i = 3
            START pos=3
                  i = 7
                  START pos = 7
                  END pos = 7
                  i = 8
                  START pos = 8
                  END pos = 8
            END pos=3
            i = 4
            START pos=4
                  i = 9
                  START pos = 9
                  END pos = 9
                  i = 10
                 START pos = 10
                 END pos = 10
            END pos = 4
      END pos = 1
      i = 2
      START pos = 2
            i = 5
            START pos = 5
                  i = 11
                  START pos = 11
                  END pos = 11
                  i = 12
                  START pos = 12
                  END pos = 12
            END pos = 5
           i = 6
           START pos = 6
                 i = 13
                 START pos = 13
                 END pos = 13
                 i = 14
                 START pos = 14
                 END pos = 14
           END pos = 6
      END pos = 2
END pos = 0


2022-09-30 15:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.