You are on page 1of 14

AIM:

To write a C program for implementation of Binary tree and traversing.

ALGORITHM:

INSERTION:
1. Start.
2. Check whether the tree is empty or not.
3. If the tree is empty,just add the data by creating a new node.
4. Else check the right and left of the root node.
5. Check till you find the leaf node.
6. If left of root is empty, then add the element to root’s left.
7. Else add to root’s right.
8. In this tree the leafnodes need to be in the same level.
9. So add the elements simultaneously in both left and right of the roots.
10. Stop.

TRAVERSAL:
1. Start.
2. First lets do the pre order traversal.
3. Check the tree is empty or not.
4. If its not empty,then print the root first.
5. Then move to left. As the left is the root now, print it.
6. Do the same.
7. Once left is done and left of root is empty, check right.
8. If right is not empty, then move to right.
9. Now print right, which will now be the root.
10. Then lets do post order.
11. Check the tree is empty or not.
12. If its not empty, then check the left of root.
13. When left is empty, check right.
14. Once traversal through left and right are done print the corresponding roots.
15. Now lets for inorder traversal.
16. First traverse through root’s left.
17. Once root’s left is empty, then print the root.
18. Now traverse through right.
19. Stop.

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left, *right;
};
struct node *root;
struct node *create(){
struct node *temp;
int data,choice;
temp = (struct node *)malloc(sizeof(struct node));
printf("\nPress 0 to exit / 1 for new node:");
printf("\nEnter your choice : ");
scanf("%d", &choice);
if(choice==0){
return 0;
}
else{
printf("Enter the data:");
scanf("%d", &data);
temp->data = data;
printf("Enter the left child of %d", data);
temp->left = create();
printf("Enter the right child of %d", data);
temp->right = create();
return temp;
}
}
void pre_order_traversal(struct node* root) {
if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->left);
pre_order_traversal(root->right);
}
}
void inorder_traversal(struct node* root) {
if(root != NULL) {
inorder_traversal(root->left);
printf("%d ",root->data);
inorder_traversal(root->right);
}
}
void post_order_traversal(struct node* root) {
if(root != NULL) {
post_order_traversal(root->left);
post_order_traversal(root->right);
printf("%d ", root->data);
}
}
void main(){
root = create();
printf("\nPreorder traversal: ");
pre_order_traversal(root);
printf("\nInorder traversal: ");
inorder_traversal(root);
printf("\nPost order traversal: ");
post_order_traversal(root);
printf("\n");
}
OUTPUT:

RESULT:
Thus the C program to implement binary tree and traversing is completed successfully and
Verified.
AIM:
To write a C program for implementation of Binary search tree and its operation.

ALGORITHM:
INSERTION:
1. Start.
2. Check whether the tree is empty or not.
3. If its empty then just add the element by creating a new node.
4. If its not empty them check the value to enter with the root node.
5. If the value is less than the root then add the element to root’s left when its empty.
6. Else add it to root’s right.
7. Stop.
SEARCH:
1. Start.
2. Check whether the tree is empty or not.
3. If its not empty check the search element with the root.
4. If its greater, compare with root->right.
5. If lesser, compare with root->left.
6. If its equal then print found.
7. Else print not found when left/right->next is null.
8. Stop.
MINIMUM:
1. Start.
2. Check if empty.
3. If not, if only one element found, that’s the minimum element.
4. Else reach the leaf node in left.
5. It is the minimum element.
6. Stop.
MAXIMUM:
1. Start.
2. Check if empty.
3. If not, if only one element found, that’s the maximum element.
4. Else reach the leaf node in right.
5. It is the maximum element.
6. Stop.
COUNT:
1. Start.
2. Check if empty.
3. If not, count the no.of nodes by,
4. Count=count(T->left)+ count(T->right)+1
5. Stop.
HEIGHT:
1. Start.
2. Check if empty.
3. If not, calculate height(T->left) and store in’ left’.
4. Calculate height(T->right) and store in ‘right’.
5. If left>right, then left is height.
6. Else right is the height.
7. Height is the longest path from root to leaf node.
8. Stop.
TRAVERSAL:

1. Start.
2. First lets do the pre order traversal.
3. Check the tree is empty or not.
4. If its not empty,then print the root first.
5. Then move to left. As the left is the root now, print it.
6. Do the same.
7. Once left is done and left of root is empty, check right.
8. If right is not empty, then move to right.
9. Now 0print right, which will now be the root.
10. Then lets do post order.
11. Check the tree is empty or not.
12. If its not empty, then check the left of root.
13. When left is empty, check right.
14. Once traversal through left and right are done print the corresponding roots.
15. Now lets for inorder traversal.
16. First traverse through root’s left.
17. Once root’s left is empty, then print the root.
18. Now traverse through right.
19. Stop.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
struct node* newNode(int item){
struct node* temp= (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
int search(struct node* T,int val){
if(T==NULL){
return -1;
}
else if(val<T->key){
search(T->left,val);
}
else if(val>T->key){
search(T->right,val);
}
else if(val==T->key){
printf("Element found.\n");
return T->key;
}
else{
printf("Element not found.\n");
return 0;
}
}
int minimum(struct node* T){
if(T==NULL){
return 0;
}
else if(T->left==NULL){
return T->key;
}
else{
minimum(T->left);
}
}
int maximum(struct node* T){
if(T==NULL){
return 0;
}
else if(T->right==NULL){
return T->key;
}
else{
return maximum(T->right);
}
}
int delete(struct node* T, int val){
if(T==NULL){
return 0;
}
else if(val<T->key){
delete(T->left,val);
}
else if(val>T->key){
delete(T->right,val);
}
else if(T->left!=NULL && T->right!=NULL){
int temp=maximum(T->left);
T->key=temp;
delete(T->left,temp);
}
else if(T->left==NULL){
T=T->right;
}
else if(T->right==NULL){
T=T->left;
}
else{
T=NULL;
}
}
int count(struct node* T){
int counts=0;
if(T==NULL){
return 0;
}
else{
counts=count(T->left)+count(T->right)+1;
return counts;
}
}
void inorder(struct node* root){
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
int height(struct node* T){
if(T==NULL){
return -1;
}
else{
int lefth=height(T->left);
int righth=height(T->right);
if(lefth>righth){
return (lefth+1);
}
else{
return (righth+1);
}
}
}
struct node* insert(struct node* node, int key){
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
int main(){
struct node* root = NULL;
int data,choice;
do{
printf("Enter data:");
scanf("%d",&data);
root=insert(root,data);
printf("\nPress 0 to exit / 1 for new node:");
printf("\nEnter your choice : ");
scanf("%d", &choice);
}while(choice!=0);
printf("Inorder travesal of tree : ");
inorder(root);
printf("\nSearching 20..\n");
search(root, 20);
printf("Maximum element : %d\n",maximum(root));
printf("Minimum element : %d\n",minimum(root));
printf("Height : %d\n",height(root));
printf("No. of nodes : %d\n",count(root));
delete(root,25);
printf("Ater Deletion of 25.\nInorder travesal of tree : ");
inorder(root);
printf("\n");
return 0;
}
OUTPUT:
RESULT:
Thus the C program to implement binary search tree and its operation is completed
successfully and verified.

AIM:
To write a C program for implementation of AVL tree and its operation.

ALGORITHM:

INSERTION:
1. Start.
2. Check whether the AVL tree is empty or not.
3. If its empty, then add the element by creating a new node.
4. Else the insertion is done like the insertion done in binary search trees.
5. Once an element is inserted in an AVL tree, a factor called balance factor is checked.
6. If balance factor exceeds the limit -1,0,1, then the tree need to be balanced by bringing
back the balance factor to the limit.
7. Balance factor is calculated by reducing root’s longest path in the right from root’s
longest path in the left.
8. The balance factor is balanced by doing,LL,RR,LR,RL rotations depending upon the
position in which the element is inserted.
9. Stop.

DELETION:
1. Start.
2. Check whether the AVL tree is empty or not.
3. If its not empty, check the element to be deleted using traversing methods.
4. When the element is found, deletion is done as done in binary search trees.
5. Once the deletion process is over, check the balance factor.
6. If balance factor is affected then its balanced by doing,LL,RR,LR,RL rotations.
7. Stop.

SEARCH:
1. Start.
2. Check whether the tree is empty or not.
3. If its not empty check the search element with the root.
4. If its greater, compare with root->right.
5. If lesser, compare with root->left.
6. If its equal then print found.
7. Else print not found when left/right->next is null.
8. Stop.

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct Node{
int key;
struct Node *left;
struct Node *right;
int height;
};
int max(int a, int b);
int height(struct Node *N){
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b){
return (a > b)? a : b;
}
struct Node* newNode(int key){
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}
struct Node *rightRotate(struct Node *y){
struct Node *x = y->left;
struct Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
return x;
}
struct Node *leftRotate(struct Node *x){
struct Node *y = x->right;
struct Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
return y;
}
int getBalance(struct Node *N){
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
struct Node* insert(struct Node* node, int key){
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else
return node;
node->height = 1 + max(height(node->left),
height(node->right));
int balance = getBalance(node);
if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);
if (balance > 1 && key > node->left->key){
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && key < node->right->key){
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
struct Node * minValueNode(struct Node* node){
struct Node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct Node* deleteNode(struct Node* root, int key){
if (root == NULL)
return root;
if ( key < root->key )
root->left = deleteNode(root->left, key);
else if( key > root->key )
root->right = deleteNode(root->right, key);
else{
if( (root->left == NULL) || (root->right == NULL)){
struct Node *temp = root->left ? root->left :root->right;
if (temp == NULL){
temp = root;
root = NULL;
}
else
*root = *temp;
free(temp);
}
else{
struct Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),
height(root->right));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->left) < 0){
root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->right) > 0){
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
void preOrder(struct Node *root){
if(root != NULL){
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
int main(){
struct Node *root = NULL;
int data,choice,del;
do{
printf("Enter data:");
scanf("%d",&data);
root=insert(root,data);
printf("\nPress 0 to exit / 1 for new node:");
printf("\nEnter your choice : ");
scanf("%d", &choice);
}while(choice!=0);
printf("Preorder traversal of the constructed AVL tree is \n");
preOrder(root);
printf("\nEnter element to delete: ");
scanf("%d",&del);
deleteNode(root,del);
preOrder(root);
printf("Enter element to search:");
scanf("%d",&s);
search(root,s);
return 0;
}
OUTPUT:
RESULT:

Thus the C program to implement AVL tree and its operation is completed successfully and
verified.

You might also like