forked from tearf001/Training-Python-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiTree1.py
More file actions
executable file
·162 lines (120 loc) · 3.4 KB
/
BiTree1.py
File metadata and controls
executable file
·162 lines (120 loc) · 3.4 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
""" classes and functions for binary trees
"""
class BiTNodeError(ValueError):
pass
class BiTNode:
def __init__(self, dat, left, right):
self.data = dat
self.left = left
self.right = right
def count_BiTNodes(t):
if t is None:
return 0
else:
return 1 + count_BiTNodes(t.left) + count_BiTNode(t.right)
def sum_BiTNodes(t):
if t is None:
return 0
else:
return t.dat + sum_BiTNodes(t.left) + sum_BiTNodes(t.right)
def preorder(t, proc):
if t is None: return
assert (isinstence(t, BiTNode))
proc(t.data)
preorder(t.left)
preorder(t.right)
def inorder(t, proc):
if t is None: return
inorder(t.left)
proc(t.data)
inorder(t.right)
def postorder(t, proc):
if t is None: return
postorder(t.left)
postorder(t.right)
proc(t.data)
from myQueue.SQueue import *
def levelorder(t, proc):
q = SQueue()
q.enqueue(t)
while not q.is_empty():
n = q.dequeue()
if t is None: continue
q.enqueue(t.left)
q.enqueue(t.right)
proc(t.data)
from stack_SStack import *
def preorder_nonrec(t, proc):
s = SStack()
while t is not None or not s.is_empty():
while t is not None: # go down along left chain
s.push(t.right) # push right branch into stack
proc(t.data)
t = t.left
t = s.pop() # left chain ends, backtrack
def preorder_iter(t):
s = SStack()
while t is not None or not s.is_empty():
while t is not None:
s.push(t.right)
yield t.data
t = t.left
t = s.pop()
def inorder_nonrec(t, proc):
s = SStack()
while t is not None or not s.is_empty():
while t is not None:
s.push(t)
t = t.left
t = s.pop()
proc(t.data)
t = t.right
# 非递归的后根序遍历算法
def postorder_nonrec(t, proc):
s = SStack()
while t is not None or not s.is_empty():
while t is not None: # iterate until top has no child
s.push(t)
t = t.left if t.left is not None else t.right
# if we can go left, go, otherwise, go right
t = s.pop() # get the node to be access
proc(t.data)
if not s.is_empty() and s.top().left == t:
t = s.top().right # end of left visit, turn right
else:
t = None # end of right visit, force to backtrack
def print_BiTNodes(t):
if t is None:
print("^", end="")
return
print("(" + str(t.data), end="")
print_BiTNodes(t.left)
print_BiTNodes(t.right)
print(")", end="")
class BiTree:
def __init__(self):
self._root = None
def is_empty(self):
return self._root == None
def set_root(self, rootnode):
self._root = rootnode
def set_left(self, leftchild):
self._root.left = leftchild
def set_right(self, rightchild):
self._root.right = rightchild
def root(self):
return self._root
def leftchild(self):
return self._root.left
def rightchild(self):
return self._root.right
def preorder_iter(self):
t, s = self._root, SStack()
while t is not None or not s.is_empty():
while t is not None:
s.push(t.right)
yield t.data
t = t.left
t = s.pop()
if __name__ == '__main__':
pass