|
| 1 | +""" |
| 2 | +Binary Search Tree Implementation |
| 3 | +
|
| 4 | +This implementation provides a binary search tree (BST) with basic operations |
| 5 | +including insertion, search, deletion, and in-order traversal. Each operation |
| 6 | +leverages recursive helper functions. |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +class BSTNode: |
| 11 | + """ |
| 12 | + A node in a binary search tree. |
| 13 | +
|
| 14 | + Attributes |
| 15 | + ---------- |
| 16 | + key : int |
| 17 | + The key value stored in the node. |
| 18 | + left : BSTNode or None |
| 19 | + The left child node. |
| 20 | + right : BSTNode or None |
| 21 | + The right child node. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, key: int): |
| 25 | + self.key = key |
| 26 | + self.left = None |
| 27 | + self.right = None |
| 28 | + |
| 29 | + |
| 30 | +class BinarySearchTree: |
| 31 | + """ |
| 32 | + Binary Search Tree (BST) class that supports basic operations such as |
| 33 | + insertion, search, deletion, and in-order traversal. |
| 34 | +
|
| 35 | + Methods |
| 36 | + ------- |
| 37 | + insert(key: int) -> None |
| 38 | + Inserts a new key into the BST. |
| 39 | + search(key: int) -> BSTNode or None |
| 40 | + Searches for a key in the BST and returns the node if found. |
| 41 | + delete(key: int) -> None |
| 42 | + Deletes a key from the BST if it exists. |
| 43 | + inorder_traversal() -> list |
| 44 | + Returns a list of keys representing the in-order traversal of the BST. |
| 45 | + """ |
| 46 | + |
| 47 | + def __init__(self): |
| 48 | + """ |
| 49 | + Initializes an empty Binary Search Tree. |
| 50 | + """ |
| 51 | + self.root = None |
| 52 | + |
| 53 | + def insert(self, key: int) -> None: |
| 54 | + """ |
| 55 | + Inserts a new key into the BST. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + key : int |
| 60 | + The key to be inserted into the BST. |
| 61 | + """ |
| 62 | + if self.root is None: |
| 63 | + self.root = BSTNode(key) |
| 64 | + else: |
| 65 | + self._insert_recursive(self.root, key) |
| 66 | + |
| 67 | + def _insert_recursive(self, node: BSTNode, key: int) -> None: |
| 68 | + """ |
| 69 | + Recursively inserts the new key into the subtree rooted at the given node. |
| 70 | +
|
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + node : BSTNode |
| 74 | + The current node in the BST. |
| 75 | + key : int |
| 76 | + The key to be inserted. |
| 77 | + """ |
| 78 | + if key < node.key: |
| 79 | + if node.left is None: |
| 80 | + node.left = BSTNode(key) |
| 81 | + else: |
| 82 | + self._insert_recursive(node.left, key) |
| 83 | + else: |
| 84 | + if node.right is None: |
| 85 | + node.right = BSTNode(key) |
| 86 | + else: |
| 87 | + self._insert_recursive(node.right, key) |
| 88 | + |
| 89 | + def search(self, key: int) -> BSTNode: |
| 90 | + """ |
| 91 | + Searches for a key in the BST. |
| 92 | +
|
| 93 | + Parameters |
| 94 | + ---------- |
| 95 | + key : int |
| 96 | + The key to search for. |
| 97 | +
|
| 98 | + Returns |
| 99 | + ------- |
| 100 | + BSTNode or None |
| 101 | + The node containing the key if found, or None otherwise. |
| 102 | + """ |
| 103 | + return self._search_recursive(self.root, key) |
| 104 | + |
| 105 | + def _search_recursive(self, node: BSTNode, key: int) -> BSTNode: |
| 106 | + """ |
| 107 | + Recursively searches for a key in the tree. |
| 108 | +
|
| 109 | + Parameters |
| 110 | + ---------- |
| 111 | + node : BSTNode or None |
| 112 | + The current node in the BST. |
| 113 | + key : int |
| 114 | + The key to search for. |
| 115 | +
|
| 116 | + Returns |
| 117 | + ------- |
| 118 | + BSTNode or None |
| 119 | + The node containing the key if found, otherwise None. |
| 120 | + """ |
| 121 | + if node is None or node.key == key: |
| 122 | + return node |
| 123 | + if key < node.key: |
| 124 | + return self._search_recursive(node.left, key) |
| 125 | + else: |
| 126 | + return self._search_recursive(node.right, key) |
| 127 | + |
| 128 | + def delete(self, key: int) -> None: |
| 129 | + """ |
| 130 | + Deletes a key from the BST if it exists. |
| 131 | +
|
| 132 | + Parameters |
| 133 | + ---------- |
| 134 | + key : int |
| 135 | + The key to be deleted. |
| 136 | + """ |
| 137 | + self.root = self._delete_recursive(self.root, key) |
| 138 | + |
| 139 | + def _delete_recursive(self, node: BSTNode, key: int) -> BSTNode: |
| 140 | + """ |
| 141 | + Recursively deletes a key from the subtree rooted at the given node. |
| 142 | +
|
| 143 | + Parameters |
| 144 | + ---------- |
| 145 | + node : BSTNode or None |
| 146 | + The current node in the BST. |
| 147 | + key : int |
| 148 | + The key to be deleted. |
| 149 | +
|
| 150 | + Returns |
| 151 | + ------- |
| 152 | + BSTNode or None |
| 153 | + The new root of the subtree after deletion. |
| 154 | + """ |
| 155 | + if node is None: |
| 156 | + return node |
| 157 | + |
| 158 | + if key < node.key: |
| 159 | + node.left = self._delete_recursive(node.left, key) |
| 160 | + elif key > node.key: |
| 161 | + node.right = self._delete_recursive(node.right, key) |
| 162 | + else: |
| 163 | + # Node found; handle deletions for nodes with 0 or 1 child. |
| 164 | + if node.left is None: |
| 165 | + return node.right |
| 166 | + elif node.right is None: |
| 167 | + return node.left |
| 168 | + |
| 169 | + # Node with two children: get the inorder successor (smallest in the right subtree) |
| 170 | + temp = self._find_min(node.right) |
| 171 | + node.key = temp.key |
| 172 | + node.right = self._delete_recursive(node.right, temp.key) |
| 173 | + return node |
| 174 | + |
| 175 | + def _find_min(self, node: BSTNode) -> BSTNode: |
| 176 | + """ |
| 177 | + Finds the node with the minimum key in the subtree. |
| 178 | +
|
| 179 | + Parameters |
| 180 | + ---------- |
| 181 | + node : BSTNode |
| 182 | + The root of the subtree. |
| 183 | +
|
| 184 | + Returns |
| 185 | + ------- |
| 186 | + BSTNode |
| 187 | + The node with the smallest key in the subtree. |
| 188 | + """ |
| 189 | + current = node |
| 190 | + while current.left is not None: |
| 191 | + current = current.left |
| 192 | + return current |
| 193 | + |
| 194 | + def inorder_traversal(self) -> list: |
| 195 | + """ |
| 196 | + Performs an in-order traversal of the BST. |
| 197 | +
|
| 198 | + Returns |
| 199 | + ------- |
| 200 | + list |
| 201 | + A list of keys in sorted order. |
| 202 | + """ |
| 203 | + result = [] |
| 204 | + self._inorder_recursive(self.root, result) |
| 205 | + return result |
| 206 | + |
| 207 | + def _inorder_recursive(self, node: BSTNode, result: list) -> None: |
| 208 | + """ |
| 209 | + Recursively performs an in-order traversal of the BST and appends keys to the result list. |
| 210 | +
|
| 211 | + Parameters |
| 212 | + ---------- |
| 213 | + node : BSTNode or None |
| 214 | + The current node in the BST. |
| 215 | + result : list |
| 216 | + The list that accumulates the keys. |
| 217 | + """ |
| 218 | + if node is not None: |
| 219 | + self._inorder_recursive(node.left, result) |
| 220 | + result.append(node.key) |
| 221 | + self._inorder_recursive(node.right, result) |
| 222 | + |
| 223 | + |
| 224 | +if __name__ == "__main__": |
| 225 | + """ |
| 226 | + Testing the Binary Search Tree (BST) implementation. |
| 227 | +
|
| 228 | + Example: |
| 229 | + -------- |
| 230 | + 1. Insert nodes with keys: [50, 30, 70, 20, 40, 60, 80] |
| 231 | + 2. Display the in-order traversal after insertion. |
| 232 | + 3. Search for a key (e.g., 40). |
| 233 | + 4. Delete a key (e.g., 30) and display the new in-order traversal. |
| 234 | + """ |
| 235 | + bst = BinarySearchTree() |
| 236 | + |
| 237 | + # Insert nodes into the BST |
| 238 | + nodes_to_insert = [50, 30, 70, 20, 40, 60, 80] |
| 239 | + for key in nodes_to_insert: |
| 240 | + bst.insert(key) |
| 241 | + print("In-order traversal after insertion:", bst.inorder_traversal()) |
| 242 | + |
| 243 | + # Search for a key in the BST |
| 244 | + search_key = 40 |
| 245 | + result = bst.search(search_key) |
| 246 | + if result: |
| 247 | + print(f"Node with key {search_key} found.") |
| 248 | + else: |
| 249 | + print(f"Node with key {search_key} not found.") |
| 250 | + |
| 251 | + # Delete a node from the BST |
| 252 | + bst.delete(30) |
| 253 | + print("In-order traversal after deleting 30:", bst.inorder_traversal()) |
0 commit comments