0% found this document useful (0 votes)
40 views23 pages

Binary Search Tree (1)

The document provides an overview of Binary Search Trees (BST), detailing their structure, advantages, and operations such as insertion, searching, and deletion. It explains the properties that define a BST and outlines algorithms for performing various operations on the tree. Additionally, it includes pseudocode for inserting and searching nodes, as well as handling different cases for node deletion.

Uploaded by

krishnapspk2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views23 pages

Binary Search Tree (1)

The document provides an overview of Binary Search Trees (BST), detailing their structure, advantages, and operations such as insertion, searching, and deletion. It explains the properties that define a BST and outlines algorithms for performing various operations on the tree. Additionally, it includes pseudocode for inserting and searching nodes, as well as handling different cases for node deletion.

Uploaded by

krishnapspk2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Binary Search Tree

19CSE212 : Data Structures and Algorithms

Dr. Chandan Kumar

Department of Computer Science and Engineering

Jan - Jun 2024

Dr Chandan Kumar Jan - Jun 2024


Contents

1 Introduction to BST

2 Why Binary Search Tree?


3 Operations on Binary Search Tree (BST)

Dr Chandan Kumar Jan - Jun 2024


Binary Search Tree

• A binary search tree (BST) is a binary tree that is


either empty or in which every node contains a key
(value) and satisfies the following conditions:
i. All keys in the left sub-tree of the root are smaller
than the key in the root node
ii. All keys in the right sub-tree of the root are greater
than the key in the root node
iii.The left and right sub-trees of the root are again
binary search trees

Dr Chandan Kumar Jan - Jun 2024


Binary Search Tree

• A binary search tree is basically a binary tree, and therefore it can be


traversed in inorder, preorder and postorder.
• If we traverse a binary search tree in inorder and print the identifiers
contained in the nodes of the tree, we get a sorted list of identifiers in
ascending order.
Dr Chandan Kumar Jan - Jun 2024
Binary Search Tree

• State whether the binary trees in Fig. are binary search trees or not.

NO YES NO

• Create a binary search tree using the following data elements:


45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81

Dr Chandan Kumar Jan - Jun 2024


Binary Search Tree
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81

Dr Chandan Kumar Jan - Jun 2024


Why Binary Search Tree?
• Since the nodes in a binary search tree are ordered, the time needed to search
an element in the tree is greatly reduced. Whenever we search for an element,
we do not need to traverse the entire tree.
• Binary search trees also speed up the insertion and deletion operations. The
tree has a speed advantage when the data in the structure changes rapidly.
• Binary search trees are considered to be efficient data structures especially
when compared with sorted linear arrays and linked lists.

Dr Chandan Kumar Jan - Jun 2024


Operations on Binary Search Tree (BST)
Following operations can be done in BST:
• Search(k, T): Search for key k in the tree T. If k is found in some node of tree
then return true otherwise return false.
• Insert(k, T): Insert a new node with value k in the info field in the tree T such
that the property of BST is maintained.
• Delete(k, T):Delete a node with value k in the info field from the tree T such
that the property of BST is maintained.
• FindMin(T), FindMax(T): Find minimum and maximum element from the
given nonempty BST.

Dr Chandan Kumar Jan - Jun 2024


Inserting a New Node in a BST
Algorithm:
1. Start at the root of the BST.
2. If the tree is empty, create a new node with the given value and
set it as the root.
3. If the given value is less than the value of the current node, move
to the left child.
4. If the given value is greater than the value of the current node,
move to the right child.
5. Repeat steps 3-4 until reaching a null child pointer.
6. Create a new node with the given value and attach it as the left or
right child of the last visited node, depending on whether the
value is less than or greater than the value of the last visited node.

Dr Chandan Kumar Jan - Jun 2024


Pseudocode: Inserting a New Node in a BST
function insert(root, value):
// If the tree is empty, create a new node and set it as the root
if root is null:
root = new Node(value)
return root
// Traverse the tree to find the appropriate position to insert the new
node
if value < root.data:
root.left = insert(root.left, value)
else if value > root.data:
root.right = insert(root.right, value)
// Return the modified root
return root
Dr Chandan Kumar Jan - Jun 2024
Inserting a New Node in a BST

Dr Chandan Kumar Jan - Jun 2024


Searching for a Node in a BST
Algorithm:
1. Start at the root of the BST.
2. Compare the given value with the value of the current node.
3. If the given value is equal to the value of the current node, return
true (value found).
4. If the given value is less than the value of the current node,
recursively search the left subtree.
5. If the given value is greater than the value of the current node,
recursively search the right subtree.
6. Repeat steps 2-5 until the value is found or until reaching a leaf
node (end of the tree).
7. If the value is not found after reaching a leaf node, return false
(value not found).

Dr Chandan Kumar Jan - Jun 2024


Searching for a Node in a BST
• The search function is used to find whether a given value is present
in the tree or not.
Pseudocode:
function search(node, value):
If the node is null, value not found
if node is null:
return false
ii. If value found at current node, return true
if node.value is equal to value:
return true
iii. If value is less than current node value, search left subtree
if value is less than node.value:
return search(node.left, value)
iv. If value is greater than current node value, search right subtree
if value is greater than node.value:
return search(node.right, value)
Dr Chandan Kumar Jan - Jun 2024
Searching for a Node in a BST

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
While deleting a node from BST, there may be three cases:
1. The node to be deleted may be a leaf node:
• In this case simply delete a node and set null pointer to its
parents those side at which this deleted node exist.

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
2. The node to be deleted has one child
• In this case the child of the node to be deleted is appended to its
parent node.

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
3. The node to be deleted has two children
• In this case the node to be deleted is replaced by its in-order
successor node. OR
• if the node to be deleted is either replaced by its right sub-tree
leftmost node or its left sub-tree rightmost node.

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
Algorithm:
1. Start at the root of the BST.
2. Search for the node to be deleted.
3. If the node is not found, return.
4. If the node has no children (leaf node), simply remove the node
from the tree.
5. If the node has only one child, replace the node with its child.
6. If the node has two children. Here we have to delete the node is
such a way, that the resulting tree follows the properties of a BST.
The trick is to find the in-order successor of the node. Copy
contents of the in-order successor to the node, and delete the in-
order successor. Note: In-order predecessor can also be used.

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
Deleting node 78 from the given binary search tree

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
Deleting node 54 from the given binary search tree

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
Deleting node 56 from the given binary search tree:
To handle this case, replace the node’s value with its in-order successor
or predecessor.

Dr Chandan Kumar Jan - Jun 2024


Deleting a Node from a BST
Deleting node 56 from the given binary search tree:

Dr Chandan Kumar Jan - Jun 2024


List of References

https://www.javatpoint.com/
https://www.tutorialspoint.com
https://www.geeksforgeeks.org/
https://www.programiz.com/dsa/
https://chat.openai.com/

Dr Chandan Kumar Jan - Jun 2024

You might also like