-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexerciese1.py
132 lines (105 loc) · 4.21 KB
/
exerciese1.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class BinarySearchTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def add_child(self, data):
# If the data already exists, do nothing
if data == self.data:
return
# If the data is less than the current node's data, go to the left subtree
if data < self.data:
if self.left:
self.left.add_child(data)
else:
self.left = BinarySearchTreeNode(data)
else:
# If the data is greater than the current node's data, go to the right subtree
if self.right:
self.right.add_child(data)
else:
self.right = BinarySearchTreeNode(data)
def find_sum(self):
#start with the value of current node
total = self.data
if self.left:#check if there is a right child of current node
total += self.left.find_sum()#add the left child to the total
if self.right:#check if there is a left child of current node
total += self.right.find_sum()#add the right child to the total
return total
def search(self, value):
# If the current node's data matches the value, return True
if self.data == value:
return True
# If the value is less than the current node's data, search in the left subtree
if value < self.data:
if self.left:
return self.left.search(value)
else:
return False
# If the value is greater than the current node's data, search in the right subtree
if value > self.data:
if self.right:
return self.right.search(value)
else:
return False
def in_order_traversal(self):
elements = []
# Traverse the left subtree
if self.left:
elements += self.left.in_order_traversal()
# Visit the current node
elements.append(self.data)
# Traverse the right subtree
if self.right:
elements += self.right.in_order_traversal()
return elements
def pre_order_traversal(self):
elements = [self.data]#visit the base node first
#visit left tree
if self.left:
elements += self.left.pre_order_traversal()
#visit right tree
if self.right:
elements += self.right.pre_order_traversal()
return elements
def post_order_traversal(self):
elements = []
#visit the left tree
if self.left:
elements += self.left.post_order_traversal()
#visit the right tree
if self.right:
elements += self.right.post_order_traversal()
#visit the base node
elements.append(self.data)
return elements
# as we know that left tree node is always smaller than the parent node (In Binary search tree), so the minimum element will be the leftmost of the tree
def find_min(self):
current = self
#loop down to find the leftmost leaf
while current.left:
current = current.left
return current.data
def find_max(self):
current = self
#loop down to find the rightmost leaf
while current.right:
current = current.right
return current.data
def build_tree(elements):
print("Building tree with these elements:", elements)
root = BinarySearchTreeNode(elements[0])
# Insert remaining elements into the tree
for i in range(1, len(elements)):
root.add_child(elements[i])
return root
if __name__ == '__main__':
elements = [17,4,1,20,9,23,18,34]
tree = build_tree(elements)
print("In order traversal of the tree:",tree.in_order_traversal())
print("The minimum value in the tree is",tree.find_min())
print("The maximum value in the tree is",tree.find_max())
print("The sum of this tree is ",tree.find_sum())
print("The pre order traversal of this tree is",tree.pre_order_traversal())
print("Post order traversal of this tree will be",tree.post_order_traversal())