-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.py
46 lines (38 loc) · 956 Bytes
/
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
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):
node = self.root
while node is not None:
if node.value < new_val:
node = node.right
else:
node = node.left
node = new_val
def search(self, find_val):
node = self.root
while node != None:
if node.value == find_val:
return True
if node.value < find_val:
node = node.right
else:
node = node.left
return False
# Set up tree
tree = BST(4)
# Insert elements
tree.insert(2)
tree.insert(1)
tree.insert(3)
tree.insert(5)
# Check search
# Should be True
print tree.search(4)
# Should be False
print tree.search(6)