You are on page 1of 6

Binary Search Tree Operations

struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE root = NULL;
// Function to insert nodes in to the tree
NODE insert(NODE root)
{
NODE temp,cur,prev;
int item;
printf("enter the value n");
scanf("%d",&item);
temp=(NODE) malloc(sizeof(NODE));
if(temp ==NULL)
{
printf("out of memory \n");
exit(0);
}
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;

if(root==NULL)
{
root=temp;
printf("%d is inserted\n",temp ->info);
return root;
}
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(item<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}
if(item < prev->info)
{
prev->llink=temp;
printf("%d is inserted as left child of %d\n",
temp->info, prev->info);
}
else
{
prev->rlink=temp;
printf("%d is inserted as right child of %d\n",
temp >info,prev->info);
}
return root;
}
// Function to find the pre-order traversal of the tree
void preorder(NODE root)
{
if(root==NULL)
return;
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
// Function to find the in-order traversal of the tree
void inorder(NODE root)
{
if(root==NULL)
return;
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
// Function to find the post-order traversal of the tree
void postorder(NODE root)
{
if(root==NULL)
return;
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->info);
}
// Function to find the smallest element of the tree
NODE findSmallestElement(NODE root)
{
if ((root == NULL) || (root->llink == NULL))
return root;
else
return findSmallestElement(root ->llink);
}
// Function to find the largest element of the tree
NODE findLargestElement(NODE root)
{
if( (root == NULL) || (root->rlink == NULL))
return root;
else
return findLargestElement(root->rlink);
}
// Function to find the total number of nodes of the tree
int totalNodes(NODE root)
{
if(root==NULL)
return 0;
else
return(totalNodes(root–>llink) + totalNodes(root–>rlink) + 1);
}

// Function to find the total external/ non-leaf nodes of the tree


int ExternalNodes(NODE root)
{
if(root==NULL)
return 0;
else if((root–>llink==NULL) && (root–>rlink==NULL))
return 1;
else
return (ExternalNodes(root–>llink) + ExternalNodes(root–>rlink));
}
// Function to find the total internal/ leaf nodes of the tree
int InternalNodes(NODE root)
{
if( (root==NULL) || ((root–>llink==NULL) && (root–>rlink==NULL)))
return 0;
else
return (InternalNodes(root–>llink) + InternalNodes(root–>rlink)+1);
}
// Function to find the height of tree
int Height(NODE root)
{
int leftheight, rightheight;
if(root==NULL)
return ;
else
{
leftheight = Height(root–>llink); //height of left subtree
rightheight = Height(root–>rlink); //height of right subtree
/*find maximum value between left height and right height and
then add 1 to it */
if(leftheight > rightheight)
return (leftheight + 1);
else
return (rightheight + 1);
}
}

// Function to delete node from a BST


NODE deleteNode(Node root, int key)
{
// pointer to store parent node of curent node
NODE parent = NULL;

// start with root node


NODE cur = root;

// search key in BST and set its parent pointer


while (cur != NULL && cur->info != key)
{
// update parent node as current node
parent = cur;

/*if given key is less than the current node, go to llink


subtree else go to rlink subtree*/
if (key < cur->info)
cur = cur->llink;
else
cur = cur->rlink;
}

// return if key is not found in the tree


if (cur == NULL)
{
printf(“Key Not Found\n”);
return root;
}
// Case 1: node to be deleted has no children i.e. it is a leaf node
if (cur->llink == NULL && cur->rlink == NULL)
{
/*if node to be deleted is not a root node, then set its
parent llink/rlink child to null */
if (cur != root)
{
if (parent->llink == cur)
parent->llink = NULL;
else
parent->rlink = NULL;
}
// if tree has only root node, delete it and set root to null
else
root = NULL;
// deallocate the memory
free(cur); // or delete cur;
}

// Case 2: node to be deleted has two children


else if (cur->llink!=NULL && cur->rlink!=NULL)
{
// find its in-order successor node (minimum value node)
NODE s=cur->rlink;
while(s->llink!=NULL)
{
s=s->llink;
}
// Copy the value of successor to current node
cur->info = s->info;

// recursively delete the successor. Note that the successor


// will have at-most one child (rlink child)
deleteNode(root, s->info);

// Case 3: node to be deleted has only one child


else
{
// find child node
Node* child;
if(cur->llink!=NULL)
child=cur->llink;
else
child=cur->rlink;

// if node to be deleted is not a root node, then set its parent


// to its child
if (cur != root)
{
if (cur == parent->llink)
parent->llink = child;
else
parent->rlink = child;
}

// if node to be deleted is root node, then set the root to child


else
root = child;

// deallocate the memory


free(cur);
}
}

You might also like