遍历算法总结

1 概念

先序遍历:节点 - 左孩子 - 右孩子
中序遍历:左孩子 - 根结点 - 右孩子
后序遍历:左孩子 - 右孩子 - 根结点

前序遍历:- + a * b – c d / e f
中序遍历:a + b * c – d – e / f
后序遍历:a b c d – * + e f / -

 

2 python的前中后序递归算法实现

class BinTree():
        def __init__(self, value, left=None, right=None):
                self.value = value
                self.left = left
                self.right = right

def initial_tree():
        a = BinTree(1)
        b = BinTree(2)
        c = BinTree(7, a, b)
        d = BinTree(4)
        e = BinTree(3, c, d)
        return e

def pre_traversal(bin_tree):
        if bin_tree is None:
                return
        print bin_tree.value
        if bin_tree.left is not None:
                pre_traversal(bin_tree.left)
        #print bin_tree.value 
        if bin_tree.right is not None:
                pre_traversal(bin_tree.right)
        #print bin_tree.value
test = initial_tree()
pre_traversal(test)

如果把print bin_tree.value放到前边就是前序遍历;放到中间就是中序遍历;放到后边就是后序遍历。

 

3 python的前中后序非递归算法实现

前序遍历实现:

def pre_traversal_no_cur(bin_tree):
    if bin_tree is None:
        return
    tree_stack = []
    tree_stack.append(bin_tree)
    while len(tree_stack) > 0:
        tmp = tree_stack.pop()
        print tmp.value
        if tmp.right is not None:
            tree_stack.append(tmp.right)
        if tmp.left is not None:
            tree_stack.append(tmp.left)

 

中序遍历实现:

def mid_traversal_no_cur(bin_tree):
    if bin_tree is None:
                return
    tree_stack = []
    tmp = bin_tree
    while tmp is not None or len(tree_stack) > 0:
        while tmp is not None:
            tree_stack.append(tmp)
            tmp = tmp.left
        if len(tree_stack) > 0:
            tmp = tree_stack.pop()
            print tmp.value
            tmp = tmp.right

 

后序遍历非递归的实现的关键点,在于判断出这个节点的右节点有没有已经被遍历过一遍了,所以实现1和实现2其实都是用来记住是否被遍历过一遍了。
实现2抓住的一点是,假设节点x,则x的右子节点遍历输出后,接着的一定是开始输出x自己,所以可以用个q来保存上个输出的节点,然后用x.right判断上个输出的是不是右节点
后序遍历实现1:

def after_traversal_two_stack(bin_tree):
    if bin_tree is None:
                return
    s1 = []
    s2 = []
    tmp = bin_tree
    while tmp is not None or len(s1) > 0:
        while tmp is not None:
            s1.append(tmp)
            s2.append(0)
            tmp = tmp.left
        if len(s1) > 0:
            tmp = s1[-1]
            if s2[-1] == 1 or tmp.right is None:
                tmp = s1.pop()
                s2.pop()
                print tmp.value
                tmp = None
            else:
                s2[-1] = 1
                tmp = tmp.right

 

后序遍历实现2:

def after_traversal_single_stack(bin_tree):
    if bin_tree is None:
        return
    s1 = []
    q = None
    tmp = bin_tree
    while tmp is not None or len(s1) > 0:
        while tmp.left is not None:
            s1.append(tmp)
            tmp = tmp.left
        while tmp.right is None or tmp.right == q:
            print tmp.value
            q = tmp
            if len(s1) <= 0:
                return
            tmp = s1.pop()
        s1.append(tmp)
        tmp = tmp.right

 

4 层次遍历

这里用到了deque模块,这里其实可以相当于是栈和队列,因为pop默认是pop出最后一个,popleft则是pop出第一个。
也可以直接像数组那样访问,d[0]、d[-1]等

from collections import deque
def level_traversal(bin_tree):
        if bin_tree is None:
                return
        queue = deque([])
        queue.append(bin_tree)
        while len(queue) > 0:
                tmp = queue.popleft()
                print tmp.value
                if tmp.left is not None:
                        queue.append(tmp.left)
                if tmp.right is not None:
                        queue.append(tmp.right)

转载于:https://www.cnblogs.com/luohaixian/p/10616949.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值