-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.py
More file actions
47 lines (44 loc) · 1.13 KB
/
BinarySearchTree.py
File metadata and controls
47 lines (44 loc) · 1.13 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
class Node():
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BinarySearchTree():
def __init__(self):
self.root = None
def insert(self, value):
newNode = Node(value)
if self.root == None:
self.root = newNode
else:
currentNode = self.root
while True:
if value < currentNode.value:
if currentNode.left == None:
currentNode.left = newNode
break
else:
currentNode = currentNode.left
else:
if currentNode.right == None:
currentNode.right = newNode
break
else:
currentNode = currentNode.right
def find(self, value):
if self.root == None:
return False
currentNode = self.root
while True:
if value == currentNode.value:
return True
elif value < currentNode.value:
if currentNode.left:
currentNode = currentNode.left
else:
return False
else:
if currentNode.right:
currentNode = currentNode.right
else:
return False