-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_object.py
More file actions
51 lines (41 loc) · 1.72 KB
/
tree_object.py
File metadata and controls
51 lines (41 loc) · 1.72 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
## Structure of the tree.
class BinaryTree:
def __init__(self, data=None, left=None, right=None):
"""Create a binary tree."""
self.data = data
self.left = left
self.right = right
def getLeft(self):
"""Return the left child of the node."""
return self.left
def getRight(self):
"""Return the right child of the node."""
return self.right
def getData(self):
"""Return the data of the node."""
return self.data
def setLeft(self, left):
"""Set the left child of the node, which must be a BinaryTree type object."""
if type(left) is BinaryTree:
self.left = left
def setRight(self, right):
"""Set the right child of the node, which must be a BinaryTree type object."""
if type(right) is BinaryTree:
self.right = right
def setData(self, data):
"""Set the data of the node, which can not be a BinaryTree type object."""
if type(data) is not BinaryTree:
self.data = data
def size(self):
"""Return the size of the binary tree, so the number of nodes if this method is applied to the root of the tree."""
if self.left is None and self.right is None:
return 1
elif self.left is not None and self.right is None:
return 1+self.left.size()
elif self.left is None and self.right is not None:
return 1+self.right.size()
else:
return 1+self.left.size()+self.right.size()
def __len__(self):
"""Return the result of the binary tree, so the result of self.size(). You should consider using self.size() to avoid confusions when using this method."""
return self.size()