-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
178 lines (153 loc) · 5.06 KB
/
tree.py
File metadata and controls
178 lines (153 loc) · 5.06 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from linked_queue import LinkedQueue
class Tree:
"""
Abstract base class representing a Tree structure
"""
#---------------------Nested Position class-----------------------------
class Position:
"""
An abstraction representing the location of a single element
"""
def element(self):
"""
returns: the element stored at the Position
"""
raise NotImplementedError('must be implemented by a sub-class')
def __eq__(self, o):
"""
o: a Position object
returns: True if o represents the same location, False otherwise
"""
raise NotImplementedError('must be implemented by a sub-class')
def __ne__(self, o):
"""
o: a Position object
returns: True if o does not represent the same location, False otherwise
"""
return not (self == other)
#------------------------------Abstract Methods--------------------------------------
def root(self):
"""
returns: the Position representing the Tree's root (or None if the Tree is empty)
"""
raise NotImplementedError('must be implemented by a sub-class')
def parent(self, p):
"""
p: a Position in the Tree
returns: the Position representing p's parent (or None if p is the root)
"""
raise NotImplementedError('must be implemented by a sub-class')
def num_children(self, p):
"""
p: a Position in the Tree
returns: the number of p's children
"""
raise NotImplementedError('must be implemented by a sub-class')
def children(self, p):
"""
p: a Position in the Tree
generates an iteration of p's children
"""
raise NotImplementedError('must be implemented by a sub-class')
def __len__(self):
"""
returns: the number of elements in the Tree
"""
raise NotImplementedError('must be implemented by a sub-class')
#----------------------------Concrete Methods-----------------------------------
def is_root(self, p):
"""
p: a Position in the Tree
returns: True if p is root, False otherwise
"""
return self.root() == p
def is_leaf(self, p):
"""
p: a Position in the Tree
returns: True if p has no children, False otherwise
"""
return self.num_children(p) == 0
def is_empty(self):
"""
returns: True if the Tree is empty, False otherwise
"""
return len(self) == 0
def depth(self, p):
"""
p: a Position in the Tree
returns: the depth of p in the Tree
"""
if self.is_root(p):
return 0
return 1 + self.depth(self.parent(p))
def _height(self, p):
"""
non-public method
p: a Position in the Tree
returns: the height of p in the Tree
"""
if self.is_leaf(p):
return 0
return 1 + max(self._height(c) for c in self.children(p))
def height(self, p=None):
"""
p: a Position in the Tree
returns: the height of p in the Tree
"""
if p is None:
p == self.root()
return self._height(p)
#-----------------------------Tree iteration methods-----------------------------------
def __iter__(self):
"""
Generates an iteration of the Tree's elements
"""
for p in self.positions():
yield p.element()
def preorder(self):
"""
Generates a preorder traversal of the Positions in the Tree
"""
if not self.is_empty():
for p in self._sub_preorder(self.root()):
yield p
def _sub_preorder(self, p):
"""
Generates a preorder traversal of the Positions in the subtree rooted at p
p: a Position in the Tree
"""
yield p
for c in self.children(p):
for other in self._sub_preorder(c):
yield other
def positions(self):
"""
Generates an iteration of the Tree's Positions using preorder
"""
return self.preorder()
def postorder(self):
"""
Genreates a postorder traversal of the Tree's Positions
"""
if not self.is_empty():
for p in self._sub_postorder(self.root()):
yield p
def _sub_postorder(self, p):
"""
Generates a postorder traversal of the subtree rooted at p
"""
for c in self.children(p):
for other in self._sub_postorder(c):
yield other
yield p
def breadth_first(self):
"""
Generates a breadth first traversal of a Tree's positions
"""
if not self.is_empty():
q = LinkedQueue()
q.enqueue(self.root())
while not q.is_empty():
for c in self.children(q.first()):
q.enqueue(c)
yield q.dequeue()