-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.py
More file actions
41 lines (34 loc) · 1.19 KB
/
Copy pathBST.py
File metadata and controls
41 lines (34 loc) · 1.19 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
# Binary Search Tree implementation in python
class Node(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BST(object):
def __init__(self, root):
self.root = Node(root)
def insert(self, new_val):
self.insert_helper(self.root, new_val)
def insert_helper(self, current, new_val):
if new_val < current.value:
if current.left:
self.insert_helper(current.left, new_val)
else:
current.left = Node(new_val)
else:
if current.right:
self.insert_helper(current.left, new_val)
else:
current.right = Node(new_val)
def search(self, find_val):
return self.search_helper(self.root, find_val)
def search_helper(self, current, find_val):
if current.value == find_val:
return True
elif find_val < current.value:
if current.left:
return self.search_helper(current.left, find_val)
else:
if current.right:
return self.search_helper(current.right, find_val)
return False