-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice05.py
More file actions
108 lines (93 loc) · 3.53 KB
/
practice05.py
File metadata and controls
108 lines (93 loc) · 3.53 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
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
"""
Docstring for practice05:
Write a Python program to create a class representing a binary search tree.
Include methods for inserting and searching for elements in the binary tree.
"""
# Define the Node class
class Node:
def __init__(self, data): # Initialize the node with a value and optional left and right children
self.data = data # Node value
self.left = None # Left child
self.right = None # Right child
# String representation of the node for easier debugging
def __str__(self):
return f"Node({self.data})"
def add_child(self, data):
if data == self.data:
return # Duplicate values are not added in this BST implementation
if data < self.data: # If data is less than current node's data
# Insert data in the left subtree
if self.left:
self.left.add_child(data)
# If left child doesn't exist, create a new node
else:
self.left = Node(data)
else:
# Insert data in the right subtree
if self.right:
self.right.add_child(data)
# If right child doesn't exist, create a new node
else:
self.right = Node(data)
def in_order_traversal(self):
elements = []
# Visit left subtree
if self.left:
elements += self.left.in_order_traversal()
# Visit base node
elements.append(self.data)
# Visit right subtree
if self.right:
elements += self.right.in_order_traversal()
return elements
def pre_order_traversal(self):
elements = []
# Visit base node
elements.append(self.data)
# Visit left subtree
if self.left:
elements += self.left.pre_order_traversal()
# Visit right subtree
if self.right:
elements += self.right.pre_order_traversal()
return elements
def post_order_traversal(self):
elements = []
# Visit left subtree
if self.left:
elements += self.left.post_order_traversal()
# Visit right subtree
if self.right:
elements += self.right.post_order_traversal()
# Visit base node
elements.append(self.data)
return elements
def search(self, value):
if self.data == value:
return True
if value < self.data:
# Search in the left subtree
if self.left:
return self.left.search(value)
else:
return False
if value > self.data:
# Search in the right subtree
if self.right:
return self.right.search(value)
else:
return False
# build the binary search tree from a list of elements
def build_tree(elements):
root = Node(elements[0]) # Create the root node
for element in elements[1:]:
root.add_child(element) # Insert each element into the BST
return root
if __name__ == "__main__":
elements = [17, 4, 1, 20, 9, 23, 18, 34]
elements_tree = build_tree(elements)
print("In-order Traversal of the BST: ", elements_tree.in_order_traversal())
print("Pre-order Traversal of the BST: ", elements_tree.pre_order_traversal())
print("Post-order Traversal of the BST:", elements_tree.post_order_traversal())
print("Search for 20 in the BST:", elements_tree.search(20)) # True
print("Search for 5 in the BST: ", elements_tree.search(5)) # False