fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@@ -0,0 +1,51 @@
---
title: Breadth-First Search
localeTitle: Поиск по ширине
---
## Поиск по ширине
Давайте сначала определим класс `Tree` который будет использоваться для реализации алгоритма первого поиска Breadth.
```python
class Tree:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
```
Алгоритм поиска ширины ширины перемещается с одного уровня на другой, начиная с корня дерева. Мы будем использовать для этого `queue` .
```python
def bfs(root_node):
queue = [root_node]
while queue:
top_element = queue.pop()
print("Node processed: ",top_element)
if top_element.left:
queue.append(top_element.left)
if top_element.right:
queue.append(top_element.right)
```
Мы можем легко изменить приведенный выше код, чтобы напечатать уровень каждого узла.
```python
def bfs(root_node):
queue = [(root_node, 0)]
while queue:
top_element, level = queue.pop()
print("Node processed: {} at level {}".format(top_element, level))
if top_element.left:
queue.append((top_element.left, level + 1))
if top_element.right:
queue.append((top_element.right, level + 1))
```
| Сложность | Время | Космос | | ----- | ------ | ------ | | BFS | n | n |