0% found this document useful (0 votes)
2 views

Binary Search Tree.pptx

A Binary Search Tree (BST) is a data structure where each node's left child is smaller and the right child is larger than the parent node, facilitating efficient searching, insertion, and deletion. Searching in a BST is straightforward due to its ordered structure, and deletion can occur in three scenarios: deleting a leaf node, a node with one child, or a node with two children. Insertion of new keys always occurs at the leaf, maintaining the BST property throughout the process.

Uploaded by

OML series
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary Search Tree.pptx

A Binary Search Tree (BST) is a data structure where each node's left child is smaller and the right child is larger than the parent node, facilitating efficient searching, insertion, and deletion. Searching in a BST is straightforward due to its ordered structure, and deletion can occur in three scenarios: deleting a leaf node, a node with one child, or a node with two children. Insertion of new keys always occurs at the leaf, maintaining the BST property throughout the process.

Uploaded by

OML series
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Binary Search Tree

What is a Binary Search tree?

• In a Binary search tree, the value of left node must be smaller than
the parent node, and the value of right node must be greater than the
parent node. This rule is applied recursively to the left and right
subtrees of the root.
Advantages of Binary search tree

• Searching an element in the Binary search tree is easy as we always


have a hint that which subtree has the desired element.
• As compared to array and linked lists, insertion and deletion
operations are faster in BST.
Example of creating a binary search tree

45, 15, 79, 90, 10, 55, 12, 20, 50


Example of creating a binary search tree
Example of creating a binary search tree
Example of creating a binary search tree
Example of creating a binary search tree
Searching in Binary search tree

• Searching means to find or locate a specific element or node in a data


structure.
• In Binary search tree, searching a node is easy because elements in BST are
stored in a specific order.
• The steps of searching a node in Binary Search tree are listed as follows –
1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root element, if
it is smaller than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
5. Repeat the above procedure recursively until the match is found.
6. If the element is not found or not present in the tree, then return NULL.
Searching in Binary search tree
Searching in Binary search tree

• Algorithm to search an element


Search (root, item)
Step 1 - if (item = root → data) or (root = NUL
L)
return root
else if (item < root → data)
return Search(root → left, item)
else
return Search(root → right, item)
END if
Step 2 - END
Deletion in Binary Search tree

• In a binary search tree, we must delete a node from the tree by


keeping in mind that the property of BST is not violated. To delete a
node from BST, there are three possible situations occur -
• The node to be deleted is the leaf node, or,
• The node to be deleted has only one child, and,
• The node to be deleted has two children
When the node to be deleted is the leaf
node
• It is the simplest case to delete a node in BST. Here, we have to
replace the leaf node with NULL and simply free the allocated space.
When the node to be deleted has only one child
• In this case, we have to replace the target node with its child, and
then delete the child node. It means that after replacing the target
node with its child node, the child node will now contain the value to
be deleted. So, we simply have to replace the child node with NULL
and free up the allocated space.
When the node to be deleted has two
children
• This case of deleting a node in BST is a bit complex among other two
cases. In such a case, the steps to be followed are listed as follows -
• First, find the inorder successor of the node to be deleted.
• After that, replace that node with the inorder successor until the target node
is placed at the leaf of tree.
• And at last, replace the node with NULL and free up the allocated space.
• The inorder successor is required when the right child of the node is
not empty. We can obtain the inorder successor by finding the
minimum element in the right child of the node.
To delete node 45 that is the root node
Insertion in Binary Search tree

• A new key in BST is always inserted at the leaf.


• To insert an element in BST, we have to start searching from the root
node; if the node to be inserted is less than the root node, then
search for an empty location in the left subtree.
• Else, search for the empty location in the right subtree and insert the
data.
• Insert in BST is similar to searching, as we always have to maintain the
rule that the left subtree is smaller than the root, and right subtree is
larger than the root.
Inserting a node 60 into BST

You might also like