Unit-4 2
Unit-4 2
A binary search tree (BST) is a node based binary tree which has the following
properties:
The left sub-tree of a node contains only nodes with keys less than the node's key.
The right sub-tree of a node contains only nodes with keys greater than the node's
key.
Both the left and right sub-trees must also be binary search trees.
Example:
The various operations that can perform on binary search tree are:
Insertion : the process of inserting a node into the binary search tree.
Deletion : The process of deleting a node from Binary Search Tree.
Search: The process of verifying whether a particular node exist in the BST or not.
Traversal : The process of visiting each and every node once.
Search:
Whenever an element is to be searched, start searching from the root node. Then if the
data is less than the key value, search for the element in the left subtree. Otherwise,
search for the element in the right subtree.
Insertion:
Insertion is used to add a new element in a binary search tree at appropriate location.
Insertion should be done in such a way that, it must not violate the property of binary
search tree at each value.
1. Allocate the memory for tree.
2. Set the data part to the value and set the left and right pointer of tree, point to
NULL.
3. If the item to be inserted, will be the first element of the tree, then the left and right
of this node will point to NULL.
4. Else, check if the item is less than the root element of the tree, if this is true, then
recursively perform this operation with the left of the root.
5. If this is false, then perform this operation recursively with the right sub-tree of the
root.
algorithm insert(t,x) // where t is the root of tree, x is the element to be inserted
{
if(t=NULL) then return (t=node(x));
else if (x=t->data) then return 0;
else if ( x<t->data) then
return insert(t->left,x);
else return insert(t->right,x);
}
Deletion:
Delete function is used to delete the specified node from a binary search tree. However,
we must delete a node from a binary search tree in such a way, that the property of binary
search tree doesn't violate. There are three situations of deleting a node from binary
search tree.
1. x occurs at a leaf: Simply remove the node containing x making the appropriate
reference at the parent (if any) a null reference.
2. x occurs at a node with one child v: Remove the node containing x and make v
a direct child of x’s parent (if any).
3. x occurs at a node with two children: First replace x with smallest value in right
subtree of x. (This value occurs at a node with no left child.) So we can delete
this node using one of the two previous cases.
Each operation requires going down the tree along one path… so the cost is O( height
of tree ).
algorithm delete(r,x) // where r is the root of tree, x is the element to be deleted
{
// local variables t,temp& child are nodes
t=r;
if ( x < t->data)
t->left = delet(t->left,x);
else if(x > t->data)
t->right=delet(t->right,x);
else if((t->left)&&(t->right))
{
temp = findmin(t->right);
t->data=temp->data;
t->right=delet(t->right,t->data);
}
else
{
temp=t;
if(t->left==NULL)
child=t->right;
if(t->right==NULL)
child=t->left;
free(temp);
return(child);
}
return r;
}