-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
46 lines (40 loc) · 1.17 KB
/
node.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
class Node:
value = None
left = None
right = None
def __init__(self, val):
self.value = val
def add_node(self, val):
if val < self.value:
if self.left is None:
self.left = Node(val)
else:
self.left.add_node(val)
if val > self.value:
if self.right is None:
self.right = Node(val)
else:
self.right.add_node(val)
def traverse(self):
if self.left is not None:
self.left.traverse()
if self.value is not None:
print(self.value)
if self.right is not None:
self.right.traverse()
def search(self, n):
if self.value == n:
print("Match Found")
return True
elif n < self.value:
if self.left is not None:
self.left.search(n)
else:
print("No match was found")
return False
elif n > self.value:
if self.right is not None:
self.right.search(n)
else:
print("No match was found")
return False