一、树的几种遍历算法
1.先序遍历
首先访问 T 的根,然后递归的访问子树的根。如果这棵树是有序的,则根据孩子的顺序遍历子树。对处于 p 位置处子树的根先序遍历,其伪代码为:
Algorthm preorder(T, p):
perform the visit action for positon p
for each child c in T.children(p) do
preorder(T, c)
2. 后序遍历
可看作相反的先序遍历,优先遍历子树,再访问根
Algorithm postorder(T, p):
for each child c in T.children(p) do
postorder(T, c)
perform the visit action for position p
运行时间都为 O(n)
3. 广度优先遍历
在访问深度 d+1 的位置之前先访问深度 d 的位置。广度优先遍历广泛的应用于游戏软件上。
Algorithm breadthfirst(T):
Intialize queue Q to contain T.root()
while Q not empty do:
p = Q.dequeue() # p is the oldest entry in the queue
performm the visit action for position p
for each child c in T.children(p) do
Q.enqueue(c) # add p's children to the end of the queue for later visits
4. 中序遍历
通过递归遍历左右子树区访问一个位置。对于一个位置p,p将其左子树之后及其右子树之前被中序访问
Algorithm inorder(p):
if p has a left child lc then
inorder(lc)
perform the visit action for position p
if p has a right child rc then
inorder(rc)
应用: 算术表达式 二叉搜索树
二叉搜索树
中序遍历把有序序列的元素存储在二叉树中,这种结构称为二叉搜索树。设 S 为一个 集合,其独特的元素存在次序关系。例如,S 可能是一组整数。S的二叉搜索树是T,对于T的每一个位置p,有:
- 位置p 存储 S 的一个元素,记作 e(p)
- 存储在 p 的左子树的元素(若有)小于 e(p)
- 存储在 p 的右子树的元素(若有)大于 e(p)
二、 用 Python 实现树的遍历
树T 应该支持下列方法:
- T.positions(): 为树 T 的所有位置生成一个迭代器
- iter(T): 用树 T 的所有元素生成一个迭代器
树的所有 元素很容易产出一个迭代器,前提是一练一个恶所有位置的假定迭代器。iter(T) 语法可以通过抽象基本树类的特殊方法的iter的具体实现给出。
def __iter__(self):
""" Generate an iteration of the tree's elements."""
for p in self.positions(): # use same order as positions()
yield p.element() # but yield each element
1. 先序遍历
通过调用树T的 T.preorder() 来给出一个公共的方法,该方法生成一个关于树的所有位置的先序迭代器。
使用上面的先序遍历算法,必须由树的特定位置参数化作为子树的根来遍历。对于这种情况,标准的解决方案是用所需的递归参数化来定义非公开的应用程序方法,然后由公共方法 preorder 在树根上调用非公开方法。
def preorder(self):
""" Generate a preorder iteration of positions in the tree."""
if not self.is_empty():
for p in self._subtree_preorder(self.root()):
yield p
def _subtree_preorder(self, p):
""" Generate a preorder iteration of positions in subtree rooted at p."""
yield p # visit p before its subtree
for c in self.childern(p)
for other in self._subtree_preorder(c):
yield other # yielding each to caller
官方树ADT要求所有树支持positions方法,这里将先序遍历作为默认的迭代顺序。将整个迭代作为对象返回,而不是先序遍历的调用的返回值的循环。
def positions(self):
""" Generate an iteration of the tree's positions."""
return self.preoreder()
2. 后序遍历
def postorder(self):
""" Generate a postorder iteration of positions in the tree."""
if not self.is_empty():
for p in self._subtree_postorder(self.root()):
yield p
def _subtree_postorder(self, p):
""" Generate a postorder iteration of positions in subtree at p."""
for c in self.children(p):
for other in self._subtree_postorder(c):
yield other # yielding each to caller
yield p # visit p after its subtrees
3. 广度优先遍历
广度优先遍历算法不是递归的,它借助位置队列来管理递归程序,这里使用 LinkedQueue类来实现
def breadthfirst(self):
""" Generate a breadth-first iteration of the positions of the tree."""
if not self.is_empty():
fringe = LinkedQueue()
fringe.enqueue(self.root())
while not fringe.is_empty():
p = fringe.dequeue()
yield p
for c in self.children(p):
fringe.enqueue()
4. 中序遍历
先序、后序和广度优先遍历算法可应用于所有树,因此在Tree的抽象基类中包含了它们的所有实现。这些方法可以被抽象二叉树类、具体的链二叉树类和其他派生的类继承。
中序遍历算法显式地依赖于左和右孩子节点的概念,只适用于二叉树,因此在BinaryTree类实现。
def inorder(self):
""" Generate an iterations of positions in the tree."""
if not self.is_empty():
for p in self._subtree_inorder(self.root()):
yield p
def _subtree_inorder(self, p):
""" Generate an inerations of positions in subtree rooted at p."""
if self.left(p) is not None:
for other in self._subtree_inorder(self.left(p)):
yield other
yield p
if self.right(p) is not None:
for othe in self._subtree_inorder(self.right(p)):
yield other
本文介绍了树的四种遍历算法:先序、后序、中序和广度优先遍历,并详细阐述了每种算法的工作原理。此外,还讨论了二叉搜索树及其性质,以及如何用Python实现这些遍历算法。

99

被折叠的 条评论
为什么被折叠?



