0% found this document useful (0 votes)
29 views5 pages

Unit-4 2

Uploaded by

ramketha07
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)
29 views5 pages

Unit-4 2

Uploaded by

ramketha07
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/ 5

Binary Search Tree:

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.

From the above properties it naturally follows that:

 Each node (item in the tree) has a distinct key.

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.

The procedure for performing the operations of BST are:

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.

So, the procedure is,

1. Compare the element with the root of the tree.


2. If the item is matched then return the location of the node.
3. Otherwise check if item is less than the element present on root, if so then move to
the left sub-tree.
4. If not, then move to the right sub-tree.
5. Repeat this procedure recursively until match found.
6. If element is not found then return NULL.
algorithm search(t,x) // where t is the root of tree, x is the element to be searched
{
if(t=NULL) then return 0;
else if (x=t->data) then return 1;
else if ( x<t->data) then
return search(t->left,x);
else return search(t->right,x);
}

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.

Three cases for delete( x ):

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;
}

You might also like