-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.py
More file actions
75 lines (43 loc) · 1.38 KB
/
binary_tree.py
File metadata and controls
75 lines (43 loc) · 1.38 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Node:
def __init__(self,value):
self.value = value
self.right = None
self.left = None
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self,value):
newnode = Node(value)
if self.root == None:
self.root = newnode
return True
temp = self.root
while 1:
#checks if there is identical value
if newnode.value == temp.value:
return False
if newnode.value < temp. value:
if temp.left == None:
temp.left = newnode
return True
temp = temp.left
else:
if temp.right == None:
temp.right = newnode
return True
temp = temp.right
def contains(self,value):
temp = self.root
while (temp is not None):
if value < temp.value:
temp = temp.left
elif value > temp.value:
temp = temp.right
else:
return True
return False
my_tree = BinarySearchTree()
my_tree.insert(10)
my_tree.insert(20)
my_tree.insert(30)
print(my_tree.contains(30))