-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.py
61 lines (42 loc) · 2.05 KB
/
BST.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class BinarySearchTree:
def __init__(self):
"""
Creates a Binary Search Tree object, initializing all class attributes to None
"""
self._value = None
self._left = None
self._right = None
def add(self, value):
"""
Adds a value to a Binary Search Tree object
"""
if self._value == None: #if there are no nodes in the Tree
self._value = value
else:
if value > self._value: #if the value is greater than our current Node's value
if self._right == None: #if reference to right Node is None
self._right = BinarySearchTree() #creates that right Node
self._right.add(value) #recursively adds that value to the right Node
elif value < self._value: #the mirror of the above lines of code
if self._left == None:
self._left = BinarySearchTree()
self._left.add(value)
def find(self, value):
"""
Finds the Node that contains the value in the parameter and returns that Node
"""
if self._value == None: #if the value is not present in our BST
return None
else:
if self._value == value: #once we actually find our Node
return self
if self._value > value: #recursively finds our value using the right Node
return self._left.find(value)
elif self._value < value:#recursively finds our value using the left Node
return self._right.find(value)
def __str__(self):
# a String representation of our Binary Search Tree
if self._value == None:
return None
else:
return "({:d} {} {})".format(self._value, str(self._left), str(self._right))