-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.py
More file actions
74 lines (65 loc) · 2.14 KB
/
binary_tree.py
File metadata and controls
74 lines (65 loc) · 2.14 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
from tree import Tree
class BinaryTree(Tree):
"""
Abstract base class representing a binary tree structure
"""
#--------------------Abstract methods-------------------
def left(self, p):
"""
p: a Position in the Tree
returns: the left child of Position p
"""
raise NotImplementedError('must be implemented by a sub-class')
def right(self, p):
"""
p: a Position in the Tree
returns: the right child of Position p
"""
raise NotImplementedError('must be implemented by a sub-class')
#---------------------Concrete Methods---------------------
def sibling(self, p):
"""
p: a Position in the Tree
returns: a Position representing p's sibling (or None if p has no sibling)
"""
parent = self.parent(p)
if parent is None:
return None
if p == self.left(parent):
return self.right(parent)
else:
return self.left(parent)
def children(self, p):
"""
p: a Position in the Tree
generates an iteration of Positions representing p's children
"""
if self.left(p) is not None:
yield self.left(p)
if self.right(p) is not None:
yield self.right(p)
def inorder(self):
"""
Generates an inorder traversal of the Tree's Positions
Specific to Binary Trees
"""
if not self.is_empty():
for p in self._sub_inorder(self.root()):
yield p
def _sub_inorder(self, p):
"""
Generates an inorder traversal of the subtree rooted at p
"""
if self.left(p) is not None:
for other in self._sub_inorder(self.left(p)):
yield other
yield p
if self.right(p) is not None:
for other in self._sub_inorder(self.right(p)):
yield other
def positions(self):
"""
Generates an iteration of the Tree's Positions using inorder traversal
Overrides the preorder positions of the Tree class
"""
return self.inorder()