-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.py
More file actions
66 lines (57 loc) · 2.44 KB
/
Copy pathbinarySearch.py
File metadata and controls
66 lines (57 loc) · 2.44 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
class Node:
"""Node class for a binary search tree (BST). Each node has a value, and references to left and right child nodes."""
def __init__(self, key):
self.left = None
self.right = None
self.val = key
class BinarySearchTree:
"""Binary search tree class. Supports insert and search operations."""
def __init__(self):
self.root = None # Initially, the tree is empty
def insert(self, key):
"""Insert a new element into the BST."""
if self.root is None:
self.root = Node(key) # If tree is empty, the new element becomes the root
else:
self._insert_recursive(self.root, key) # Otherwise, insert it in the correct position
def _insert_recursive(self, node, key):
"""Helper method to insert a new element into the BST recursively."""
if key < node.val:
# Insert in the left subtree
if node.left is None:
node.left = Node(key)
else:
self._insert_recursive(node.left, key)
else:
# Insert in the right subtree
if node.right is None:
node.right = Node(key)
else:
self._insert_recursive(node.right, key)
def search(self, key):
"""Search for an element in the BST."""
return self._search_recursive(self.root, key)
def _search_recursive(self, node, key):
"""Helper method to search for an element in the BST recursively."""
if node is None or node.val == key:
return node # Element found or not in the tree
if key < node.val:
return self._search_recursive(node.left, key) # Search in the left subtree
return self._search_recursive(node.right, key) # Search in the right subtree
def in_order_traversal(self):
"""Perform in-order traversal of the BST and print elements."""
self._in_order_recursive(self.root)
def _in_order_recursive(self, node):
"""Helper method for in-order traversal of the BST."""
if node is not None:
self._in_order_recursive(node.left)
print(node.val, end=' ') # Print the node value
self._in_order_recursive(node.right)
# Example usage
bst = BinarySearchTree()
bst.insert(3)
bst.insert(7)
bst.insert(1)
bst.insert(5)
bst.in_order_traversal() # should print 1 3 5 7 in sorted order
print("\nSearch for 5:", "Found" if bst.search(5) else "Not Found")