-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_tree.py
More file actions
46 lines (35 loc) · 1.07 KB
/
Copy pathbinary_search_tree.py
File metadata and controls
46 lines (35 loc) · 1.07 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
from data_structures.binary_tree import BinaryTree, Node
class BinarySearchTree(BinaryTree):
"""
Put docstring here
"""
def __init__(self):
super().__init__()
def add(self, value):
if self.root is None:
self.root =Node(value)
return
current = self.root
while current:
if value < current.value:
if current.left is None:
current.left = Node(value)
return
current = current.left
else:
if current.right is None:
current.right = Node(value)
return
current =current.right
def contains(self, value):
if self.root is None:
return False
current = self.root
while current is not None:
if current.value == value:
return True
elif current.value > value:
current = current.left
else:
current = current.right
return False