basic programming python: python recursion depth-first search and breadth-first search algorithm simulation to achieve

This article describes the python recursion depth-first search and breadth-first search algorithm simulation to achieve, very good, has a certain value for references, a friend in need can refer to
a recursive principle of small case studies

(1 Overview

Recursion: that is, a function call their own, namely to achieve a recursive loop can do all things, recursive general can do!

(2) # write recursive process

1, write critical condition

2, to find out the relationship between this time and last time

3, assuming that the current function has been able to call itself a result of the calculation, then calculated the results of this

(3) Case Study: seeking + 1 + 2 + 3 + ... and the number n of # Overview

'''
递归:即一个函数调用了自身,即实现了递归
凡是循环能做到的事,递归一般都能做到!
'''
# 写递归的过程
'''
1、写出临界条件
2、找出这一次和上一次关系
3、假设当前函数已经能用,调用自身计算上一次的结果,再求出本次的结果
'''
# 问题:输入一个大于1 的数,求1+2+3+....
def sum(n):
 if n==1:
  return 1
 else:
  return n+sum(n-1)
n=input("请输入:")
print("输出的和是:",sum(int(n)))
'''
输出:
请输入:4
输出的和是: 10
'''

Here Insert Picture Description

#__author:"吉*佳"
#date: 2018/10/21 0021
#function:
import os
def getAllDir(path):
 fileList = os.listdir(path)
 print(fileList)
 for fileName in fileList:
  fileAbsPath = os.path.join(path,fileName)
  if os.path.isdir(fileAbsPath):
   print("$$目录$$:",fileName)
   getAllDir(fileAbsPath)
  else:
   print("**普通文件!**",fileName)
 # print(fileList)
 pass
getAllDir("G:\\")

Output results are as follows:
Here Insert Picture Description
Second, the depth and breadth traversal traversal

(A), depth-first search

Description: With depth-first search stack structure simulation

Depth schematic traversal: Here Insert Picture Description
Description:

A push into the first, while the A push the stack into the BC, while the case B so that the DE of the stack push (C to keep not processed) Similarly, in the stack when the D, HI push, and finally from the top down

Remove the stack from the stack an element yet, i.e., a depth-first traversal.

Case practice: use a stack to print out the depth of the search directory structure of Here Insert Picture Description
the program code:

#__author:"吉**"
#date: 2018/10/21 0021
#function:
# 深度优先遍历目录层级结构
import os
def getAllDirDP(path):
 stack = []
 # 压栈操作,相当于图中的A压入
 stack.append(path)
 # 处理栈,当栈为空的时候结束循环
 while len(stack) != 0:
  #从栈里取数据,相当于取出A,取出A的同时把BC压入
  dirPath = stack.pop()
  firstList = os.listdir(dirPath)
  #判断:是目录压栈,把该目录地址压栈,不是目录即是普通文件,打印
  for filename in firstList:
   fileAbsPath=os.path.join(dirPath,filename)
   if os.path.isdir(fileAbsPath):
    #是目录就压栈
    print("目录:",filename)
    stack.append(fileAbsPath)
   else:
    #是普通文件就打印即可,不压栈
    print("普通文件:",filename)
getAllDirDP(r'E:\[AAA](千)全栈学习python\18-10-21\day7\temp\dir')

Results: Here Insert Picture Description
The process is a diagram for explaining: (s-05-1 section) Here Insert Picture Description
principle analysis: Here Insert Picture Description
Description:

   队列是 先进先出的模型。A先进队,在A出队的时候,C B入队,按图示,C出队,FG 入队,B出队,DE入队,

F dequeue, JK into the team, G dequeued without the team, D dequeue, HI enqueue, dequeue EJKHI finally, into the team of no, i.e. each layer by layer process,

Therefore: FIFO queue structure to achieve the first traversal. Last-out stack structure to achieve that depth-first traversal.

Code:

In fact, depth-first and breadth-first on writing code is not very different, basically the same, but one is to use the stack structure (simulated with list)

Another (breadth first traversal) data structure is used to achieve the purpose of the queue FIFO.

#__author:"吉**"
#date: 2018/10/21 0021
#function:
# 广度优先搜索模拟
# 利用队列来模拟广度优先搜索
import os
import collections
def getAllDirIT(path):
 queue=collections.deque()
 #进队
 queue.append(path)
 #循环,当队列为空,停止循环
 while len(queue) != 0:
  #出队数据 这里相当于找到A元素的绝对路径
  dirPath = queue.popleft()
  # 找出跟目录下的所有的子目录信息,或者是跟目录下的文件信息
  dirList = os.listdir(dirPath)
  #遍历该文件夹下的其他信息
  for filename in dirList:
   #绝对路径
   dirAbsPath = os.path.join(dirPath,filename)
   # 判断:如果是目录dir入队操作,如果不是dir打印出即可
   if os.path.isdir(dirAbsPath):
    print("目录:"+filename)
    queue.append(dirAbsPath)
   else:
    print("普通文件:"+filename)
# 函数的调用
getAllDirIT(r'E:\[AAA](千)全栈学习python\18-10-21\day7\temp\dir')

Breadth-first run output structure: Here Insert Picture Description
first diagram: each layer can be implemented in a traverse from left to right. Here Insert Picture Description
Finally, I recommend a good reputation python gathering [ click to enter ], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data method, every day Python programmers explain the timing of technology, learning to share some small details need to pay attention and
summary

The above is a python recursion depth-first search and breadth of the small series to introduce the first search algorithm simulation to achieve, we want to help, if you have any questions please give me a message, Xiao Bian will promptly reply to everyone.

Published 25 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/haoxun11/article/details/104953841