Python
Official website
The official website for Python is https://www.python.org.
Static typing
Python is not statically typed; it is dynamically typed. This means that types are determined at runtime rather than during compile time. In Python, you don’t need to explicitly declare variable types before assigning values, and a variable can be reassigned to objects of different types during execution.
However, Python does support type hints (introduced in Python 3.5 with PEP 484) that allow developers to optionally annotate variables and function signatures with type information. These annotations can help with code readability, and can be checked by static analysis tools (like mypy), but they are not enforced at runtime by the Python interpreter.
Example code
Below is a Python implementation of a binary search tree (BST) along with a function to find (search for) a value in the tree. The code includes a Node class to represent each node in the BST and a recursive search function that exploits the BST property (i.e., left child nodes are less than the parent node, and right child nodes are greater).
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def insert(root, key):
"""
Inserts a new key into the binary search tree.
Returns the new root after insertion.
"""
if root is None:
return Node(key)
if key < root.key:
root.left = insert(root.left, key)
elif key > root.key:
root.right = insert(root.right, key)
# Duplicate keys are not inserted
return root
def search(root, target):
"""
Searches for a target value in the binary search tree.
Returns the node containing the target value, or None if the target does not exist.
"""
if root is None:
# Target not found in BST
return None
if root.key == target:
return root
elif target < root.key:
return search(root.left, target)
else:
return search(root.right, target)
# Example usage
if __name__ == "__main__":
# Construct the BST by inserting keys
values = [50, 30, 70, 20, 40, 60, 80]
bst_root = None
for value in values:
bst_root = insert(bst_root, value)
# Search for specific values in the BST
target_value = 40
result = search(bst_root, target_value)
if result:
print(f"Found node with key: {result.key}")
else:
print(f"Key {target_value} not found in the BST.")
# Try searching for a value that does not exist
target_value = 100
result = search(bst_root, target_value)
if result:
print(f"Found node with key: {result.key}")
else:
print(f"Key {target_value} not found in the BST.")
Explanation
- Node Class:
- Each
Nodehas akeyas its value and two child pointersleftandright.
- Each
- insert Function:
- The
insertfunction adds a new key into the BST following the BST properties. - If the root is
None, it creates a new node. - If the key is less than the current node’s key, it goes to the left subtree; if greater, it goes to the right subtree.
- The
- search Function:
- The
searchfunction looks for the target value. - It returns the node if the key matches.
- Otherwise, it recursively searches the left or right subtree according to the BST property.
- The
- Example Usage:
- A small BST is constructed by inserting integer values.
- The algorithm searches for a few example keys and reports the results accordingly.
This implementation is complete, runnable, and demonstrates both insertion and search functionalities on a BST.