/
traversal.py
421 lines (347 loc) · 13.4 KB
/
traversal.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Copyright © 2021 by Shun Huang. All rights reserved.
# Licensed under MIT License.
# See LICENSE in the project root for license information.
"""Traversal module for traversing binary trees."""
from typing import Any, Iterator, Union
from forest.binary_trees import binary_search_tree
from forest.binary_trees import avl_tree
# Alias for the supported node types. For type checking.
SupportedNode = Union[None, binary_search_tree.Node, avl_tree.Node]
SupportedTree = Union[binary_search_tree.BinarySearchTree, avl_tree.AVLTree]
"""Alias for the supported tree types. For type checking."""
Pairs = Iterator[tuple[Any, Any]]
"""Iterator of Key-Value pairs, yield by traversal functions. For type checking"""
def inorder_traverse(tree: SupportedTree, recursive: bool = True) -> Pairs:
"""Perform In-Order traversal.
In-order traversal traverses a tree by the order:
left subtree, current node, right subtree (LDR)
Parameters
----------
tree : `SupportedTree`
An instance of the supported binary tree types.
recursive: `bool`
Perform traversal recursively or not.
Yields
------
`Pairs`
The next (key, data) pair in the in-order traversal.
Examples
--------
>>> from forest.binary_trees import binary_search_tree
>>> from forest.binary_trees import traversal
>>> tree = binary_search_tree.BinarySearchTree()
>>> tree.insert(key=23, data="23")
>>> tree.insert(key=4, data="4")
>>> tree.insert(key=30, data="30")
>>> tree.insert(key=11, data="11")
>>> tree.insert(key=7, data="7")
>>> tree.insert(key=34, data="34")
>>> tree.insert(key=20, data="20")
>>> tree.insert(key=24, data="24")
>>> tree.insert(key=22, data="22")
>>> tree.insert(key=15, data="15")
>>> tree.insert(key=1, data="1")
>>> [item for item in traversal.inorder_traverse(tree)]
[(1, '1'), (4, '4'), (7, '7'), (11, '11'), (15, '15'), (20, '20'),
(22, '22'), (23, '23'), (24, '24'), (30, '30'), (34, '34')]
"""
if recursive:
return _inorder_traverse(node=tree.root)
return _inorder_traverse_non_recursive(root=tree.root)
def preorder_traverse(tree: SupportedTree, recursive: bool = True) -> Pairs:
"""Perform Pre-Order traversal.
Pre-order traversal traverses a tree by the order:
current node, left subtree, right subtree (DLR)
Parameters
----------
tree : `SupportedTree`
An instance of the supported binary tree types.
recursive: `bool`
Perform traversal recursively or not.
Yields
------
`Pairs`
The next (key, data) pair in the pre-order traversal.
Examples
--------
>>> from forest.binary_trees import binary_search_tree
>>> from forest.binary_trees import traversal
>>> tree = binary_search_tree.BinarySearchTree()
>>> tree.insert(key=23, data="23")
>>> tree.insert(key=4, data="4")
>>> tree.insert(key=30, data="30")
>>> tree.insert(key=11, data="11")
>>> tree.insert(key=7, data="7")
>>> tree.insert(key=34, data="34")
>>> tree.insert(key=20, data="20")
>>> tree.insert(key=24, data="24")
>>> tree.insert(key=22, data="22")
>>> tree.insert(key=15, data="15")
>>> tree.insert(key=1, data="1")
>>> [item for item in traversal.preorder_traverse(tree)]
[(23, '23'), (4, '4'), (1, '1'), (11, '11'), (7, '7'), (20, '20'),
(15, '15'), (22, '22'), (30, '30'), (24, '24'), (34, '34')]
"""
if recursive:
return _preorder_traverse(node=tree.root)
return _preorder_traverse_non_recursive(root=tree.root)
def postorder_traverse(tree: SupportedTree, recursive: bool = True) -> Pairs:
"""Perform Post-Order traversal.
Post-order traversal traverses a tree by the order:
left subtree, right subtree, current node (LRD)
Parameters
----------
tree : `SupportedTree`
An instance of the supported binary tree types.
recursive: `bool`
Perform traversal recursively or not.
Yields
------
`Pairs`
The next (key, data) pair in the post-order traversal.
Examples
--------
>>> from forest.binary_trees import binary_search_tree
>>> from forest.binary_trees import traversal
>>> tree = binary_search_tree.BinarySearchTree()
>>> tree.insert(key=23, data="23")
>>> tree.insert(key=4, data="4")
>>> tree.insert(key=30, data="30")
>>> tree.insert(key=11, data="11")
>>> tree.insert(key=7, data="7")
>>> tree.insert(key=34, data="34")
>>> tree.insert(key=20, data="20")
>>> tree.insert(key=24, data="24")
>>> tree.insert(key=22, data="22")
>>> tree.insert(key=15, data="15")
>>> tree.insert(key=1, data="1")
>>> [item for item in traversal.postorder_traverse(tree)]
[(1, '1'), (7, '7'), (15, '15'), (22, '22'), (20, '20'), (11, '11'),
(4, '4'), (24, '24'), (34, '34'), (30, '30'), (23, '23')]
"""
if recursive:
return _postorder_traverse(node=tree.root)
return _postorder_traverse_non_recursive(root=tree.root)
def reverse_inorder_traverse(tree: SupportedTree, recursive: bool = True) -> Pairs:
"""Perform reversed In-Order traversal.
Reversed in-order traversal traverses a tree by the order:
right subtree, current node, left subtree (RNL)
Parameters
----------
tree : `SupportedTree`
An instance of the supported binary tree types.
recursive: `bool`
Perform traversal recursively or not.
Yields
------
`Pairs`
The next (key, data) pair in the reversed in-order traversal.
Examples
--------
>>> from forest.binary_trees import binary_search_tree
>>> from forest.binary_trees import traversal
>>> tree = binary_search_tree.BinarySearchTree()
>>> tree.insert(key=23, data="23")
>>> tree.insert(key=4, data="4")
>>> tree.insert(key=30, data="30")
>>> tree.insert(key=11, data="11")
>>> tree.insert(key=7, data="7")
>>> tree.insert(key=34, data="34")
>>> tree.insert(key=20, data="20")
>>> tree.insert(key=24, data="24")
>>> tree.insert(key=22, data="22")
>>> tree.insert(key=15, data="15")
>>> tree.insert(key=1, data="1")
>>> [item for item in traversal.reverse_inorder_traverse(tree)]
[(34, '34'), (30, '30'), (24, '24'), (23, '23'), (22, '22'), (20, '20'),
(15, '15'), (11, '11'), (7, '7'), (4, '4'), (1, '1')]
"""
if recursive:
return _reverse_inorder_traverse(node=tree.root)
return _reverse_inorder_traverse_non_recursive(root=tree.root)
def levelorder_traverse(tree: SupportedTree) -> Pairs:
"""Perform Level-Order traversal.
Level-order traversal traverses a tree:
level by level, from left to right, starting from the root node.
Parameters
----------
tree : `SupportedTree`
An instance of the supported binary tree types.
Yields
------
`Pairs`
The next (key, data) pair in the level-order traversal.
Examples
--------
>>> from forest.binary_trees import binary_search_tree
>>> from forest.binary_trees import traversal
>>> tree = binary_search_tree.BinarySearchTree()
>>> tree.insert(key=23, data="23")
>>> tree.insert(key=4, data="4")
>>> tree.insert(key=30, data="30")
>>> tree.insert(key=11, data="11")
>>> tree.insert(key=7, data="7")
>>> tree.insert(key=34, data="34")
>>> tree.insert(key=20, data="20")
>>> tree.insert(key=24, data="24")
>>> tree.insert(key=22, data="22")
>>> tree.insert(key=15, data="15")
>>> tree.insert(key=1, data="1")
>>> [item for item in traversal.levelorder_traverse(tree)]
[(23, '23'), (4, '4'), (30, '30'), (1, '1'), (11, '11'), (24, '24'),
(34, '34'), (7, '7'), (20, '20'), (15, '15'), (22, '22')]
"""
queue = [tree.root]
while len(queue) > 0:
temp = queue.pop(0)
if temp:
yield (temp.key, temp.data)
if temp.left:
queue.append(temp.left)
if temp.right:
queue.append(temp.right)
def _inorder_traverse(node: SupportedNode) -> Pairs:
if node:
yield from _inorder_traverse(node.left)
yield (node.key, node.data)
yield from _inorder_traverse(node.right)
def _inorder_traverse_non_recursive(root: SupportedNode) -> Pairs:
if root is None:
raise StopIteration
stack = []
if root.right:
stack.append(root.right)
stack.append(root)
current = root.left
while True:
if current:
if current.right:
stack.append(current.right)
stack.append(current)
current = current.left
continue
stack.append(current)
current = current.left
else: # current is None
if len(stack) > 0:
current = stack.pop()
if current.right is None:
yield (current.key, current.data)
current = None
continue
else: # current.right is not None
if len(stack) > 0:
if current.right == stack[-1]:
yield (current.key, current.data)
current = stack.pop() if len(stack) > 0 else None
continue
else: # current.right != stack[-1]:
# This case means there are more nodes on the right
# Keep the current and go back to add them.
continue
else: # stack is empty
break
def _reverse_inorder_traverse(node: SupportedNode) -> Pairs:
if node:
yield from _reverse_inorder_traverse(node.right)
yield (node.key, node.data)
yield from _reverse_inorder_traverse(node.left)
def _reverse_inorder_traverse_non_recursive(root: SupportedNode) -> Pairs:
if root is None:
raise StopIteration
stack = []
if root.left:
stack.append(root.left)
stack.append(root)
current = root.right
while True:
if current:
if current.left:
stack.append(current.left)
stack.append(current)
current = current.right
continue
stack.append(current)
current = current.right
else: # current is None
if len(stack) > 0:
current = stack.pop()
if current.left is None:
yield (current.key, current.data)
current = None
continue
else: # current.right is not None
if len(stack) > 0:
if current.left == stack[-1]:
yield (current.key, current.data)
current = stack.pop() if len(stack) > 0 else None
continue
else: # current.right != stack[-1]:
# This case means there are more nodes on the right
# Keep the current and go back to add them.
continue
else: # stack is empty
break
def _preorder_traverse(node: SupportedNode) -> Pairs:
if node:
yield (node.key, node.data)
yield from _preorder_traverse(node.left)
yield from _preorder_traverse(node.right)
def _preorder_traverse_non_recursive(root: SupportedNode) -> Pairs:
if root is None:
raise StopIteration
stack = [root]
while len(stack) > 0:
temp = stack.pop()
yield (temp.key, temp.data)
# Because stack is FILO, insert right child before left child.
if temp.right:
stack.append(temp.right)
if temp.left:
stack.append(temp.left)
def _postorder_traverse(node: SupportedNode) -> Pairs:
if node:
yield from _postorder_traverse(node.left)
yield from _postorder_traverse(node.right)
yield (node.key, node.data)
def _postorder_traverse_non_recursive(root: SupportedNode) -> Pairs:
if root is None:
raise StopIteration
stack = []
if root.right:
stack.append(root.right)
stack.append(root)
current = root.left
while True:
if current:
if current.right:
stack.append(current.right)
stack.append(current)
current = current.left
continue
else: # current.right is None
if current.left:
stack.append(current)
else:
yield (current.key, current.data)
current = current.left
else: # current is None
if len(stack) > 0:
current = stack.pop()
if current.right is None:
yield (current.key, current.data)
current = None
else: # current.right is not None
if len(stack) > 0:
if current.right != stack[-1]:
yield (current.key, current.data)
current = None
else: # current.right == stack[-1]
temp = stack.pop()
stack.append(current)
current = temp
else: # stack is empty
yield (current.key, current.data)
break
else: # stack is empty
break