-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.py
31 lines (28 loc) · 1.01 KB
/
BinarySearchTree.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
class BinarySearchTree:
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
def insertChild(self, child):
if self.data == None:
self.data = child.data
elif child.data > self.data:
self.rightChild = child
elif child.data < self.data:
self.rightChild = child
def addChild(self, data):
if self.data == None:
self.data = data
elif data > self.data:
if self.rightChild == None:
# if we dont have a rightnode we create one
self.rightChild = BinarySearchTree(data)
else:
self.addChild(self.rightChild, data)
else:
if self.leftChild == None:
# if we dont have a rightnode we create one
self.leftChild = BinarySearchTree(data)
else:
self.addChild(self.leftChild, data)
return "the node has been successfully inserted"