-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearchTree.py
58 lines (37 loc) · 1.34 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Node:
def __init__(self,x):
self.val = x
self.left = None
self.right = None
self.counter = 1
def bstMaker(list):
root = Node(list[0])
currentNode = root
for val in list:
currentNode = root
while currentNode != None:
#If currentNode val == list val, add to currentNodes counter
if currentNode.val == val : #== add to counter
currentNode.counter+=1 #if it already exists, add 1 to node
currentNode = None
elif val > currentNode.val: #if val being inputed is greater than currentNode
if currentNode.right == None: #if right is nonexistant,
currentNode.right = Node(val) #set right to a new node containing a value
currentNode = None #reset the while loop, thus resetting the for loop
elif currentNode.right != None: #if right exists, set currentNode to right
currentNode = currentNode.right
elif val < currentNode.val: #Same as above, going left
if currentNode.left == None:
currentNode.left = Node(val)
currentNode = None
elif currentNode.left != None:
currentNode = currentNode.left
else: #If not of the requirements are met, set currentNode to none
currentNode = None
return(root)
#if val > currentNode.val:
#currentNode = currentNode.right
def main():
list = [4,3,7,5,9,1,1,1,1,1,1]
print(bstMaker(list).left.val)
main()