Binary Search Tree
And its
Operations using ‘c’
BST represents Twofold Hunt Tree, which is a normally utilized information structure in software engineering. It is a kind of double tree where every hub has all things considered two kids, alluded to as the left kid and the right kid. The paired pursuit tree has the accompanying properties:
Twofold Pursuit Property: For some random hub in the tree, all hubs in its left subtree have esteems not exactly the hub's worth, and all hubs in its right subtree have values more prominent than the hub's worth.
Exceptional Key Property: No two hubs in the tree can have a similar worth.
The paired inquiry tree takes into consideration effective looking, addition, and cancellation activities. Here are the fundamental tasks performed on a double hunt tree:
Inclusion: To embed another hub into a BST, you look at the worth of the new hub with the worth of the ongoing hub. In the event that the new worth is more modest, you move to the left offspring of the ongoing hub. Assuming that the new worth is bigger, you move to the right kid. This cycle go on until a suitable void spot is found, where the new hub is embedded as a leaf hub.
Search: To look for a worth in a BST, you contrast the objective worth and the worth of the ebb and flow hub. Assuming the qualities match, you have tracked down the hub. Assuming the objective worth is more modest, you move to the left kid. Assuming that the objective worth is bigger, you move to the right kid. This cycle go on until the objective worth is found or until you arrive at a leaf hub (showing the worth is absent in the tree).
Cancellation: Erasing a hub from a BST requires taking care of various cases in light of the quantity of youngsters the hub has:
Assuming the hub has no kids, it is just eliminated.
Assuming that the hub has one youngster, the kid replaces the hub.
Assuming that the hub has two youngsters, it very well may be supplanted by either its ancestor (the biggest hub in its left subtree) or its replacement (the littlest hub in its right subtree). After the substitution, the ancestor/replacement hub is eliminated from its unique position.
Crossing: BST upholds various kinds of crossings to visit every one of the hubs in a particular request:
All together crossing: Visits the hubs in climbing request (left-subtree, current-hub, right-subtree).
Pre-request crossing: Visits the hubs in pre-request (current-hub, left-subtree, right-subtree).
Post-request crossing: Visits the hubs in post-request (left-subtree, right-subtree, current-hub).
Level-request crossing: Visits the hubs level by level, beginning from the root.
These are the fundamental tasks performed on a double hunt tree. The effectiveness of these activities relies upon the level of the tree, which can be limited through adjusted BST variations, for example, AVL trees or red-dark trees.
Program:-
#include
<stdio.h>
#include
<stdlib.h>
struct node{
int key;
struct node
*left,*right;
};
struct node
*c(int item)
{
struct node *temp=(struct node *)malloc(sizeof(struct
node*));
temp->key=item;
temp->left=temp->right=NULL;
return temp;
};
struct
node*insert(struct node*node,int val)
{
if(node==NULL)
return c(val);
if(val<node->key)
node->left=insert(node->left,val);
else
node->right=insert(node->right,val);
return node;
};
void
postorder(struct node *root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d-",root->key);
}
int main()
{
struct node *root =NULL;
int a[10],n,i,v[10],x,ch;
printf("enter the no of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
root=insert(root,a[i]);
}
printf("\npost order\n");
postorder(root);
printf("end");
printf("\nenter ur choice");
scanf("%d",&ch);
if(ch==1){
printf("\nenter no of nodes");
scanf("%d",&x);
printf("enter the values to
insert");
for(i=0;i<x;i++)
{
scanf("%d",&v[i]);
insert(root,v[i]);
}
postorder(root);
printf("end");
}
else if(ch==2)
{
printf("enter the no of nodes u
want to delete");
scanf("%d",&x);
printf("enter value to insert ");
for(i=0;i<x;i++)
{
scanf("%d",&v[i]);
root=delnode(root,v[i]);
}
postorder(root);
}
return 0;
}
int
delnode(struct node*root,int val)
{
struct node*temp;
if(root==NULL)
return root;
if(val<root->key)
root->left=delnode(root->left,val);
else
if(val>root->key)
root->right=delnode(root->right,val);
else{
if(root->left==NULL)
{
temp=root->right;
free(root);
return temp;
}
else if(root->right==NULL)
{
temp=root->left;
free(root);
return temp;
}
temp=minval(root->right);
root->key=temp->key;
root->right=delnode(root->right,temp->key);
}
return root;
};
int minval(struct node*node)
{
struct node*c=node;
while(c&&c->left != NULL)
c=c->left;
return c;
};
Output:-
0 Comments