-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathbinary_search_tree.py
More file actions
53 lines (42 loc) · 1.39 KB
/
binary_search_tree.py
File metadata and controls
53 lines (42 loc) · 1.39 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
47
48
49
50
51
52
53
class BinarySearchTree:
def __init__(self):
self.root_node = None
def isEmpty(self):
return self.root_node is None
def insert(self, value):
if self.isEmpty():
self.root_node = BSTNode(value)
else:
self.root_node.insert(value)
def contains(self, target):
if self.isEmpty():
return False
else:
return self.root_node.contains(target)
# BSTNode class
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
#Insert the given value into the tree
def insert(self, value):
if value < self.value:
if self.left is None:
self.left = BSTNode(value)
else:
self.left.insert(value)
elif value >= self.value:
if not self.right:
self.right = BSTNode(value)
else:
self.right.insert(value)
# Return True if the tree contains the value
# False if it does not
def contains(self, target):
if target == self.value:
return True
elif target < self.value:
return self.left.contains(target) if self.left else False
else:
return self.right.contains(target) if self.right else False