URDINESH: Data structures in C
Showing posts with label Data structures in C. Show all posts
Showing posts with label Data structures in C. Show all posts

Thursday, May 8, 2014

IMPLEMENTATION OF AVL TREE

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};
struct node *insert (int , struct node *, int *);
struct node* search(struct node *,int);
main()
{
bool ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
while(1)
{
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
if( search(root,info) == NULL )
root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

struct node* search(struct node *ptr,int info)
{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}/*End of search()*/
struct node *insert (int info, struct node *pptr, int *ht_inc)
{
struct node *aptr;
struct node *bptr;
if(pptr==NULL)
{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}
if(info < pptr->info)
{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1: /* Right heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = 1;
break;
case 1: /* Left heavy */
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf("Left to Left Rotation\n");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;
if(bptr->balance == 1 )
pptr->balance = -1;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}/*End of switch */
}/*End of if */
}/*End of if*/
if(info > pptr->info)
{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1: /* Left heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = -1;
break;
case -1: /* Right heavy */
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf("Right to Right Rotation\n");
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Right to Left Rotation\n");
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;
if(bptr->balance == -1)
pptr->balance = 1;
else
pptr->balance = 0;
if(bptr->balance == 1)
aptr->balance = -1;
else
aptr->balance = 0;
bptr->balance=0;
pptr = bptr;
}/*End of else*/
*ht_inc = FALSE;
}/*End of switch */
}/*End of if*/
}/*End of if*/
return(pptr);
}/*End of insert()*/
display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}

}/*End of inorder()*/

                 *******Thank you for reading and All the best**************

Your comments will help me a lot...please give your comments which will encourage me to post more.....

Implement An Expression Tree. Produce Its Pre-Order, In-Order, And Post-Order Traversals.

Implement An Expression Tree. Produce Its Pre-Order, In-Order, And Post-Order Traversals.

# include<stdio.h>
# include <conio.h>
# include <malloc.h>
typedef struct bst
{
int data;
struct bst *left,*right;
}node;
void insert(node *, node *);
void inorder(node *);
void postorder(node *);
void preorder(node *);
node *get_node();
/*Main Function*/
void main()
{
int ch,ans=5;
node *New,*root;
root=NULL;
clrscr();
while(1)
{
printf("\nEnter the option:\n1-Create\n2-Preorder\n3-Inorder\n4-Postorder\n5-Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter Elements 1 by 1: (0 to stop entering)\n");
do
{
New=get_node();
scanf("%d",&New->data);
ans=New->data;
if(ans!=0)
if(root==NULL)
root=New;
else
insert(root,New);
}while(ans!=0);
break;
case 2:
if(root==NULL)
printf("\nNo element In Tree\n");
else
{
printf("\n~~~PREORDER TRAVERSALS~~~\nThe Tree is:\n");
preorder(root);
}
break;
case 3:
if(root==NULL)
printf("\nNo element In Tree\n");
else
{
printf("\n~~~INORDER TRAVERSALS~~~\nThe Tree is:\n");
inorder(root);
}
break;
case 4:
if(root==NULL)
printf("\nNo element In Tree\n");
else
{
printf("\n~~~POSTORDER TRAVERSALS~~~\nThe Tree is:\n");
postorder(root);
}
break;
default:
printf("\n~~~Exit~~~\n");
getch();
exit(0);
break;
}
}
}
/*Get node*/
node *get_node()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
return temp;
}
/*Insert Function*/
void insert(node *root,node *New)
{
if(New->data < root->data)
{
if(root->left==NULL)
root->left=New;
else
insert(root->left,New);
}
if(New->data > root->data)
{
if(root->right==NULL)
root->right=New;
else
insert(root->right,New);
}
}
/*preorder Traversals*/
void preorder(node *temp)
{
if(temp!=NULL)
{
printf("-> %d ",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
/*Inorder Traversals*/
void inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
printf("-> %d ",temp->data);
inorder(temp->right);
}
}
/*postorder Traversals*/
void postorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("-> %d ",temp->data);
}
}




OUTPUT


Enter the option:
1-Create
2-Preorder
3-Inorder
4-Postorder
5-Exit
1

Enter Elements 1 by 1: (0 to stop entering)
56
45
5
9
1
0

Enter the option:
1-Create
2-Preorder
3-Inorder
4-Postorder
5-Exit
2

~~~PREORDER TRAVERSALS~~~
The Tree is:
-> 56 -> 45 -> 5 -> 1 -> 9
Enter the option:
1-Create
2-Preorder
3-Inorder
4-Postorder
5-Exit
3

~~~INORDER TRAVERSALS~~~
The Tree is:
-> 1 -> 5 -> 9 -> 45 -> 56
Enter the option:
1-Create
2-Preorder
3-Inorder
4-Postorder
5-Exit

4

~~~POSTORDER TRAVERSALS~~~
The Tree is:
-> 1 -> 9 -> 5 -> 45 -> 56
Enter the option:
1-Create
2-Preorder
3-Inorder
4-Postorder
5-Exit
5
~~~Exit~~~



























Followers