-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs_post_order.py
59 lines (54 loc) · 1.77 KB
/
dfs_post_order.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
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
return True
else:
temp = self.root
while True:
if new_node.value == temp.value:
return False
elif new_node.value < temp.value:
if temp.left is not None:
temp = temp.left
else:
temp.left = new_node
return True
elif new_node.value > temp.value:
if temp.right is not None:
temp = temp.right
else:
temp.right = new_node
return True
def contains(self, value):
if self.root is None:
return False
else:
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 #it means that value doesn't exist
def dfs_post_order(self):
results = []
def traverse(current_node):
if current_node.left is not None:
traverse(current_node.left)
if current_node.right is not None:
traverse(current_node.right)
results.append(current_node.value)
traverse(self.root)
return results
t1 = BinarySearchTree()