-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path递归和非递归实现二叉搜索树的三种遍历.py
91 lines (86 loc) · 2.72 KB
/
递归和非递归实现二叉搜索树的三种遍历.py
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
# -*- coding:UTF-8 -*-
'''
利用递归以及非递归的方式实现二叉搜索树的前序遍历、中序遍历和后序遍历
'''
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Tranversal:
# preorder without recursion
def preOrder(self, root):
if root == None:
return None
pNode, treeStack = root, []
while pNode or len(treeStack) > 0:
while pNode:
print(pNode.val)
treeStack.append(pNode)
pNode = pNode.left
if len(treeStack) > 0:
pNode = treeStack.pop()
pNode = pNode.right
# preorder with recursion
def preOrderRec(self, root):
if root != None:
print(root.val)
self.preOrderRec(root.left)
self.preOrderRec(root.right)
# inorder without recursion
def inOrder(self, root):
if root == None:
return
pNode, treeStack = root, []
while pNode or len(treeStack) > 0:
while pNode:
treeStack.append(pNode)
pNode = pNode.left
if len(treeStack) > 0:
pNode = treeStack.pop()
print(pNode.val)
pNode = pNode.right
# inorder with recursion
def inOrderRec(self, root):
if root != None:
self.inOrderRec(root.left)
print(root.val)
self.inOrderRec(root.right)
# postorder without recursion
def postOrder(self, root):
if root == None:
return
cur, pre, treeStack = root, None, [] # cur:current Node, pre: pre visited Node
treeStack.append(root)
while len(treeStack) > 0:
cur = treeStack[-1]
# current node doesn't have child nodes or child nodes have been visited
if (cur.left == None and cur.right == None) or (pre != None and (pre == cur.left or pre == cur.right)):
print(cur.val)
pre = treeStack.pop()
else:
if cur.right != None:
treeStack.append(cur.right)
if cur.left != None:
treeStack.append(cur.left)
# postorder with cursion
def postOrderRec(self, root):
if root:
self.postOrderRec(root.left)
self.postOrderRec(root.right)
print(root.val)
pNode1 = TreeNode(10)
pNode2 = TreeNode(6)
pNode3 = TreeNode(14)
pNode4 = TreeNode(4)
pNode5 = TreeNode(8)
pNode6 = TreeNode(12)
pNode7 = TreeNode(16)
pNode1.left = pNode2
pNode1.right = pNode3
pNode2.left = pNode4
pNode2.right = pNode5
pNode3.left = pNode6
pNode3.right = pNode7
S = Tranversal()
S.postOrder(pNode1)