This document discusses binary search trees in Python. It defines a binary search tree as a data structure where every node has at most two children, with the left child being smaller than the parent and right child being larger. It describes key properties like unique values, efficient insertion/deletion, and tree traversal flexibility. It then provides code examples for a Node class, insertion/deletion implementations, and searching a tree for a node. Finally, it discusses balancing binary search trees to maintain optimal time complexities.
1. Binary Search Tree in Python
Name : Keshav Bandil
Branch : AI&DS
Year : 2nd Year ( 3rd Semister)
Submitted To : Geetika Hazra
2. Binary Search Tree in Python
A binary search tree is a data structure where every node has at most two
children, and the left child is always smaller than the parent node, while
the right child is larger. Let's explore the key properties and code
implementation in Python.
3. Key Properties of a Binary Search Tree
Unique Value (NO SAME VALUE)
Each node in a binary search tree holds a
unique value, and no two nodes in the tree
can have the same value.
Efficient Insertion and Deletion
Insertions and deletions in a binary search
tree can be performed efficiently, providing a
flexible data structure.
Tree Traversal Flexibility
Tree traversal algorithms allow you to visit nodes in specific orders, such as preorder, inorder, and
postorder.
4. Code of Binary Search Tree in Python
1 Node Class
Create a Node class
with attributes for storing
the node value and
references to the left and
right children.
2 Insertion
Implementation
Implement the logic to
insert a new value into
the binary search tree
while maintaining its
ordered structure.
3 Deletion
Implementation
Implement the logic to
delete a node from the
binary search tree while
preserving its properties.
5. Code for Binary Search Tree in Python
class Node:
def init(self, val):
self.val = val
self.left = None
self.right = None
##Defining a function that will search the given value of x in the tree
def search (root, x):
##The base conditions are If the given root is a Null Node
Or if the value we are searching for is present at the root only##
if root is None or root.val == x:
return root.val
if root.val < x: ## check If the value of x is greater than the
value root.val < x:
# We will search in the right subtree
return search(root.right, x)
##If the value of x is smaller than the value of the root node
we will search in left subtree
return search(root.left, x)
##Creating a binary search tree and searching for x in the tree
root = Node(9)
root.left = Node(1)
root.right = Node(10)
root.left.left = Node(0)
root.left.right = Node(3)
root.left.right.right = Node(4)
x = 4
v = search(root, x)
print("The node we are searching for is present in the given BST: ", v)
6. Tree Traversal Algorithms
Inorder Traversal
Visits the left subtree, then
the current node, and finally
the right subtree.
LEFT SUBTREE-ROOT
NODE -RIGHT SUBTREE
Preorder Traversal
Visits the current node, then
the left subtree, and finally the
right subtree.
ROOT NODE-LEFT
SUBTREE-RIGHT SUBTREE
Postorder Traversal
Visits the left subtree, then
the right subtree, and finally
the current node.
LEFT SUBTREE-RIGHT
SUBTREE-ROOT NODE
7. Implementation of a Binary Search Tree in Python
Creating a Tree
Use the Node class to create a
binary search tree instance and
add nodes with specific values.
Inserting Values
Use the insertion
implementation to add new
values to the binary search tree
in their appropriate positions.
Deleting Values
Use the deletion
implementation to remove
nodes from the binary search
tree.
9. Searching for a Node in a Binary Search Tree
1 Start at the Root
Begin the search from the root node,
comparing the target value with the
current node's value.
2
Follow the Path
If the target value is smaller, move to
the left child node; otherwise, move to
the right child node. 3 Found or Not Found
Repeat the process until the target
value is found or the traversal reaches
a null (not found).
10. Time Complexity of Binary Search Tree
Operation Time Complexity
Search O(log n) average, O(n) worst case
Insertion O(log n) average, O(n) worst case
Deletion O(log n) average, O(n) worst case
The time complexity of binary search tree operations depends on the tree's height and whether it is
balanced or unbalanced.
11. Balancing a Binary Search Tree
AVL Trees
AVL trees are self-balancing binary search
trees that automatically adjust their structure
to maintain efficiency.
Red-Black Trees
Red-Black trees are another type of self-
balancing binary search tree that guarantee a
balanced structure.
Balancing Algorithms
Various algorithms exist to rebalance an
unbalanced binary search tree, ensuring
optimal time complexities.
Benefits and Trade-Offs
Balancing a binary search tree improves the
time complexity of operations but may involve
additional complexity in implementation.