2) Binary Search Tree PDF
2) Binary Search Tree PDF
• In the above tree, the value of root node is 40, which is greater than its left
child 30 but smaller than right child of 30, i.e., 55. So, the above tree does
not satisfy the property of Binary search tree. Therefore, the above tree is not
a binary search tree.
• 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.
• Now, the creation of binary search tree is completed. After that, let's
move towards the operations that can be performed on Binary search
tree.
• We can perform insert, delete and search operations on the binary search
tree.
Searching in Binary search tree
• Step2:
• Step3:
Algorithm to search an element in Binary search tree
• Search (root, item)
• Step 1 - if (item = root → data) or (root = NULL)
• 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.
• We can see the process to delete a leaf node from BST in the below
image. In below image, suppose we have to delete node 90, as the
node to be deleted is a leaf node, so it will be replaced with NULL,
and the allocated space will free.
• 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.
• We can see the process of deleting a node with one child from BST in
the below image. In the below image, suppose we have to delete the
node 79, as the node to be deleted has only one child, so it will be
replaced with its child 55.
• So, the replaced node 79 will now be a leaf node that can be easily
deleted.
• 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.
• We can see the process of deleting a node with two children from
BST in the below image. In the below image, suppose we have to
delete node 45 that is the root node, as the node to be deleted has two
children, so it will be replaced with its inorder successor. Now, node
45 will be at the leaf of the tree so that it can be deleted easily.
• 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.
• we will see the implementation of the operations of binary search tree.
Here, we will see the creation, inorder traversal, insertion, and
deletion operations of tree.
• Here, we will see the inorder traversal of the tree to check whether the
nodes of the tree are in their proper location or not. We know that the
inorder traversal always gives us the data in ascending order. So, after
performing the insertion and deletion operations, we perform the
inorder traversal, and after traversing, if we get data in ascending
order, then it is clear that the nodes are in their proper location.
• Program