Python实现二叉树的遍历

简介:

Python实现二叉树的遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class BinaryTree(object):
    def __init__(self, value=None, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right
 
    def rebuild(self, preOrder, inOrder):
        """
        根据前序列表和中序列表,重建二叉树
        :param preOrder: 前序列表
        :param inOrder: 中序列表
        :return: 二叉树
        """
        if preOrder == None or inOrder == None or len(preOrder) <=0 or len(inOrder) <=0 \
                or len(preOrder) != len(inOrder):
            return None
        cur = BinaryTree(preOrder[0])
        index = inOrder.index(preOrder[0])
        cur.left = self.rebuild(preOrder[1: index+1], inOrder[:index])
        cur.right = self.rebuild(preOrder[index+1:], inOrder[index+1:])
        return cur
 
    def preOrder(self, tree):
        """
        前序遍历
        :param tree:
        :return:
        """
        if tree == None:
            return None
        print(tree.value, end=' ')
        self.preOrder(tree.left)
        self.preOrder(tree.right)
 
    def preOrderLoop(self, tree):
        """
        前序遍历的循环实现
        :param tree:
        :return:
        """
        if tree == None:
            return None
        lst = []
        node = tree
        while node != None or len(lst) > 0:
            if node != None:
                lst.append(node)
                print(node.value, end=' ')
                node = node.left
            else:
                item = lst[len(lst)-1]
                lst = lst[:-1]
                node = item.right
 
    def inOrder(self, tree):
        """
        中序遍历
        :param tree:
        :return:
        """
        if tree == None:
            return None
        self.inOrder(tree.left)
        print(tree.value, end=' ')
        self.inOrder(tree.right)
 
    def inOrderLoop(self, tree):
        """
        中序遍历循环实现
        :param tree:
        :return:
        """
        if tree == None:
            return
        lst = []
        node = tree
 
        while node != None or len(lst) > 0:
            if node != None:
                lst.append(node)
                node = node.left
            else:
                item = lst[len(lst)-1]
                lst = lst[:-1]
                print(item.value, end=' ')
                node = item.right
 
    def postOrder(self, tree):
        """
        后序遍历
        :param tree:
        :return:
        """
        if tree == None:
            return None
        self.postOrder(tree.left)
        self.postOrder(tree.right)
        print(tree.value, end=' ')
 
    def postOrderLoop(self, tree):
        """
        后续遍历的循环实现
        :param tree:
        :return:
        """
        if tree == None:
            return None
 
        visited = set()
        lst = []
        node = tree
 
        while node != None or len(lst) > 0:
            if node != None:
                lst.append(node)
                node = node.left
            else:
                item = lst[len(lst)-1]
                if item.right != None and item.right not in visited:
                    node = item.right
                else:
                    print(item.value, end=' ')
                    visited.add(item)
                    lst = lst[:-1]
本文转自 许大树 51CTO博客,原文链接:http://blog.51cto.com/abelxu/1968898,如需转载请自行联系原作者
相关文章
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
132 6
|
9月前
|
存储 算法 Python
文件管理系统中基于 Python 语言的二叉树查找算法探秘
在数字化时代,文件管理系统至关重要。本文探讨了二叉树查找算法在文件管理中的应用,并通过Python代码展示了其实现过程。二叉树是一种非线性数据结构,每个节点最多有两个子节点。通过文件名的字典序构建和查找二叉树,能高效地管理和检索文件。相较于顺序查找,二叉树查找每次比较可排除一半子树,极大提升了查找效率,尤其适用于海量文件管理。Python代码示例包括定义节点类、插入和查找函数,展示了如何快速定位目标文件。二叉树查找算法为文件管理系统的优化提供了有效途径。
134 5
|
10月前
|
算法 定位技术 Python
震惊!Python 图结构竟然可以这样玩?DFS&BFS 遍历技巧大公开
在 Python 编程中,图是一种重要的数据结构,而深度优先搜索(DFS)和广度优先搜索(BFS)是遍历图的两种关键算法。本文将通过定义图的数据结构、实现 DFS 和 BFS 算法,并通过具体示例展示其应用,帮助读者深入理解这两种算法。DFS 适用于寻找路径和检查图连通性,而 BFS 适用于寻找最短路径。掌握这些技巧,可以更高效地解决与图相关的复杂问题。
131 2
|
10月前
|
Python
不容错过!Python中图的精妙表示与高效遍历策略,提升你的编程艺术感
本文介绍了Python中图的表示方法及遍历策略。图可通过邻接表或邻接矩阵表示,前者节省空间适合稀疏图,后者便于检查连接但占用更多空间。文章详细展示了邻接表和邻接矩阵的实现,并讲解了深度优先搜索(DFS)和广度优先搜索(BFS)的遍历方法,帮助读者掌握图的基本操作和应用技巧。
117 4
|
10月前
|
算法 Python
Python图论探索:从理论到实践,DFS与BFS遍历技巧让你秒变技术大牛
图论在数据结构与算法中占据重要地位,应用广泛。本文通过Python代码实现深度优先搜索(DFS)和广度优先搜索(BFS),帮助读者掌握图的遍历技巧。DFS沿路径深入搜索,BFS逐层向外扩展,两者各具优势。掌握这些技巧,为解决复杂问题打下坚实基础。
139 2
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
248 7
|
12月前
|
数据处理 Python
python遍历文件夹所有文件按什么排序
python遍历文件夹所有文件按什么排序
156 0
|
12月前
|
数据处理 Python
Python遍历文件夹所有文件并按指定排序
Python遍历文件夹所有文件并按指定排序
327 0
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
77 3
|
Python
【Leetcode刷题Python】剑指 Offer 32 - II. 从上到下打印二叉树 II
本文提供了一种Python实现方法,用于层次遍历二叉树并按层打印结果,每层节点按从左到右的顺序排列,每层打印到一行。
110 3

热门文章

最新文章

推荐镜像

更多