深度优先遍历和广度优先遍历python_python实现树的深度优先遍历与广度优先遍历详解...

本文实例讲述了python实现树的深度优先遍历与广度优先遍历。分享给大家供大家参考,具体如下:

广度优先(层次遍历)

从树的root开始,从上到下从左到右遍历整个树的节点

数和二叉树的区别就是,二叉树只有左右两个节点

广度优先 顺序:A - B - C - D - E - F - G - H - I

代码实现

def breadth_travel(self, root):

"""利用队列实现树的层次遍历"""

if root == None:

return

queue = []

queue.append(root)

while queue:

node = queue.pop(0)

print node.elem,

if node.lchild != None:

queue.append(node.lchild)

if node.rchild != None:

queue.append(node.rchild)

深度优先

深度优先有三种算法:前序遍历,中序遍历,后序遍历

先序遍历 在先序遍历中,我们先访问根节点,然后递归使用先序遍历访问左子树,再递归使用先序遍历访问右子树

根节点->左子树->右子树

#实现 1

def preorder(self, root):

"""递归实现先序遍历"""

if root == None:

return

print root.elem

self.preorder(root.lchild)

self.preorder(root.rchild)

#实现 2

def depth_tree(tree_node):

if tree_node is not None:

print (tree_node._data)

if tree_node._left is noe None:

return depth_tree(tree_node._left)

if tree_node._right is not None:

return depth_tree(tree_node._right)

中序遍历 在中序遍历中,我们递归使用中序遍历访问左子树,然后访问根节点,最后再递归使用中序遍历访问右子树

左子树->根节点->右子树

def inorder(self, root):

"""递归实现中序遍历"""

if root == None:

return

self.inorder(root.lchild)

print root.elem

self.inorder(root.rchild)

后序遍历 在后序遍历中,我们先递归使用后序遍历访问左子树和右子树,最后访问根节点

左子树->右子树->根节点

def postorder(self, root):

"""递归实现后续遍历"""

if root == None:

return

self.postorder(root.lchild)

self.postorder(root.rchild)

print root.elem

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

时间: 2019-10-26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python实现二叉广度优先遍历,可以使用队列来实现。下面是一个示例代码: ```python class TreeNode(object): def __init__(self, value, left_child=None, right_child=None): self.left_child = left_child self.value = value self.right_child = right_child def wide_search(root): queue = [root] while queue: node = queue.pop(0) print(node.value) if node.left_child: queue.append(node.left_child) if node.right_child: queue.append(node.right_child) # 创建一个二叉 node_a = TreeNode('A') node_b = TreeNode('B') node_c = TreeNode('C') node_d = TreeNode('D') node_e = TreeNode('E') node_f = TreeNode('F') node_g = TreeNode('G') node_a.left_child = node_b node_a.right_child = node_c node_b.left_child = node_d node_b.right_child = node_e node_c.left_child = node_f node_c.right_child = node_g wide_search(node_a) ``` 这段代码中,我们首先定义了一个`TreeNode`类来表示二叉的节点。然后,我们定义了一个`wide_search`函数来实现广度优先遍历。在函数中,我们使用一个队列来存储待遍历的节点。我们从根节点开始,将根节点入队列,然后循环处理队列中的节点,每次处理一个节点时,将其值打印出来,并将其左右子节点分别入队列。这样就可以实现广度优先遍历了。最后,我们创建了一个二叉的实例,并调用`wide_search`函数来进行广度优先遍历。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python 深度、广度遍历二叉](https://blog.csdn.net/weixin_42336579/article/details/81334212)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [python基础编程:python实现深度优先遍历广度优先遍历详解](https://download.csdn.net/download/weixin_38590309/14855357)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值