-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
33 lines (25 loc) · 871 Bytes
/
tree.py
File metadata and controls
33 lines (25 loc) · 871 Bytes
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
"""
Tree(root) class will create a tree object
with given root value.
tree.findLeave(leave) returns a boolean
representing whether a certain value exsits
on the branchs of a given root of a tree.
tree.grow(left, right) will create a left
branch and a right branch given the root of
a exsiting tree.
"""
class Tree(object):
def __init__(self, value):
# for a given node on the tree, there are only two branches: left and right
self.left = None
self.right = None
self.data = value
def findLeave(self, leave):
if (self.data == leave):
return True
if (self.left == None and self.right == None): # means that's the end of a tree
return False
return self.left.findLeave(leave) or self.right.findLeave(leave)
def grow(self, left, right):
self.left = Tree(left)
self.right = Tree(right)