-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.py
99 lines (80 loc) · 2.45 KB
/
binary_search_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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):
new_node = Node(value)
if self.root is None:
self.root = new_node
temp = self.root
while temp is not None:
if new_node.value == temp.value:
return False
if new_node.value < temp.value:
if temp.left is None:
temp.left = new_node
return True
else:
temp = temp.left
else:
if temp.right is None:
temp.right = new_node
return True
else:
temp = temp.right
def contains(self, value):
if self.root is None:
return False
temp = self.root
while temp is not None:
if value == temp.value:
return True
if value < temp.value:
temp = temp.left
else:
temp = temp.right
return False
def BreathFirstSearch(self):
"""
returns all the elements of Tree, from the top
"""
current_node = self.root
queues = []
results = []
queues.append(current_node)
while len(queues) > 0:
current_node = queues.pop(0)
results.append(current_node.value)
if current_node.left is not None:
queues.append(current_node.left)
if current_node.right is not None:
queues.append(current_node.right)
return results
def DFSPreOrder(self):
"""
Depth First Search - PreOder method
"""
results = []
def traverse(current_node):
results.append(current_node.value)
if current_node.left is not None:
traverse(current_node.left)
if current_node.right is not None:
traverse (current_node.right)
traverse(self.root)
return results
bst = BinarySearchTree()
bst.insert(2)
bst.insert(1)
bst.insert(3)
print(bst.root.value)
print(bst.root.left.value)
print(bst.root.right.value)
print(bst.contains(3))
print(bst.contains(5))
print(bst.BreathFirstSearch())
print(bst.DFSPreOrder())