-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_serch_tree.py
61 lines (50 loc) · 1.55 KB
/
binary_serch_tree.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
59
60
61
class Node:
def __init__(self, value):
self.value = value
self.right = None
self.left = None
class Tree:
def __init__(self):
self.root = None
def insert(self, value):
if self.root == None:
self.root = Node(value)
return self.root
node = self.root
while True:
if value < node.value:
if node.left is None:
node.left = Node(value)
return node.left
else:
node = node.left
else:
if node.right is None:
node.right = Node(value)
return node.right
else:
node = node.right
def findvalue(self, value):
node = self.root
while node.value:
if value < node.value:
if node.left is None:
return ("Value {} not found!".format(value))
else:
node = node.left
# return self.left.findvalue(value)
elif value > node.value:
if node.right is None:
return ("Value {} not found!".format(value))
else:
node = node.right
elif value == node.value:
return "found {}".format(value)
return ("Value {} not found".format(value))
myTree = Tree()
myTree.insert(9)
myTree.insert(4)
myTree.insert(10)
myTree.insert(3)
print(myTree.findvalue(10))
print(myTree.findvalue(21))