-
Notifications
You must be signed in to change notification settings - Fork 0
/
_traversal.py
119 lines (94 loc) · 3.31 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
"""Traversierung für Binär Bäume."""
from __future__ import annotations
__all__: Final[list[str]] = [
"inorder",
"levelorder",
"postorder",
"preorder",
]
from typing import Final, TypeVar
from nrw.datastructures import BinarySearchTree, BinaryTree, ComparableContentT, List
_T = TypeVar("_T")
def preorder(
tree: BinaryTree[_T] | BinarySearchTree[ComparableContentT],
*,
reverse: bool = False,
) -> List[_T | ComparableContentT]:
result: List[_T | ComparableContentT] = List()
if tree.is_empty:
return result
if not reverse:
result.append(tree.content)
result.concat(preorder(tree.left_tree, reverse=reverse))
result.concat(preorder(tree.right_tree, reverse=reverse))
else:
result.append(tree.content)
result.concat(preorder(tree.right_tree, reverse=reverse))
result.concat(preorder(tree.left_tree, reverse=reverse))
return result
def inorder(
tree: BinaryTree[_T] | BinarySearchTree[ComparableContentT],
*,
reverse: bool = False,
) -> List[_T | ComparableContentT]:
result: List[_T | ComparableContentT] = List()
if tree.is_empty:
return result
if not reverse:
result.concat(inorder(tree.left_tree, reverse=reverse))
result.append(tree.content)
result.concat(inorder(tree.right_tree, reverse=reverse))
else:
result.concat(inorder(tree.right_tree, reverse=reverse))
result.append(tree.content)
result.concat(inorder(tree.left_tree, reverse=reverse))
return result
def postorder(
tree: BinaryTree[_T] | BinarySearchTree[ComparableContentT],
*,
reverse: bool = False,
) -> List[_T | ComparableContentT]:
result: List[_T | ComparableContentT] = List()
if tree.is_empty:
return result
if not reverse:
result.concat(postorder(tree.left_tree, reverse=reverse))
result.concat(postorder(tree.right_tree, reverse=reverse))
result.append(tree.content)
else:
result.concat(postorder(tree.right_tree, reverse=reverse))
result.concat(postorder(tree.left_tree, reverse=reverse))
result.append(tree.content)
return result
def levelorder(
tree: BinaryTree[_T] | BinarySearchTree[ComparableContentT],
*,
reverse: bool = False,
) -> List[_T | ComparableContentT]:
if tree.is_empty:
return List()
trees: List[BinaryTree[_T] | BinarySearchTree[ComparableContentT]] = List()
trees.append(tree)
trees.to_first()
while trees.has_access:
current_tree: BinaryTree[_T] | BinarySearchTree[ComparableContentT] | None = (
trees.content
)
assert current_tree is not None
if not reverse:
if not current_tree.left_tree.is_empty:
trees.append(current_tree.left_tree)
if not current_tree.right_tree.is_empty:
trees.append(current_tree.right_tree)
else:
if not current_tree.right_tree.is_empty:
trees.append(current_tree.right_tree)
if not current_tree.left_tree.is_empty:
trees.append(current_tree.left_tree)
trees.next()
result: List[_T | ComparableContentT] = List()
trees.to_first()
while trees.has_access:
result.append(trees.content.content)
trees.next()
return result