SlideShare une entreprise Scribd logo
1  sur  4
AVL TREE
#include<stdio.h>
#include<conio.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;
clrscr();
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
while(1)
{
printf("1.Insertn");
printf("2.Displayn");
printf("3.Quitn");
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 ignoredn");
break;
case 2:
if(root==NULL)
{
printf("Tree is emptyn");
continue;
}
printf("Tree is :n");
display(root, 1);
printf("nn");
printf("n");
break;
case 3:
exit(1);
default:
printf("Wrong choicen");
}/*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 Rotationn");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotationn");
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 Rotationn");
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Right to Left Rotationn");
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()*/

Contenu connexe

Tendances

Tendances (20)

Double linked list
Double linked listDouble linked list
Double linked list
 
Array imp of list
Array imp of listArray imp of list
Array imp of list
 
C++ programs
C++ programsC++ programs
C++ programs
 
StackArray stack3
StackArray stack3StackArray stack3
StackArray stack3
 
Array list
Array listArray list
Array list
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Cquestions
Cquestions Cquestions
Cquestions
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Data Structures : array operations in c program
Data Structures : array operations in c program Data Structures : array operations in c program
Data Structures : array operations in c program
 
week-14x
week-14xweek-14x
week-14x
 
week-18x
week-18xweek-18x
week-18x
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
week-11x
week-11xweek-11x
week-11x
 
week-10x
week-10xweek-10x
week-10x
 
week-15x
week-15xweek-15x
week-15x
 
Tu1
Tu1Tu1
Tu1
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 

Similaire à Avl tree

Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdf
eyelineoptics
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
gordienaysmythe
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
kostikjaylonshaewe47
 
#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdf#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdf
contact34
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
galagirishp
 
โครงงาน เครื่องคิดเลข
โครงงาน เครื่องคิดเลขโครงงาน เครื่องคิดเลข
โครงงาน เครื่องคิดเลข
Bung Lfkglialbmk
 
Ugly code
Ugly codeUgly code
Ugly code
Odd-e
 

Similaire à Avl tree (20)

Avl tree
Avl treeAvl tree
Avl tree
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdf
 
C++ adt c++ implementations
C++   adt c++ implementationsC++   adt c++ implementations
C++ adt c++ implementations
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)
 
include.docx
include.docxinclude.docx
include.docx
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdf#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdf
 
week-17x
week-17xweek-17x
week-17x
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdf
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Tugas1
Tugas1Tugas1
Tugas1
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
 
pROgRAN C++ ApLiKaSI Binery tree
pROgRAN C++ ApLiKaSI Binery treepROgRAN C++ ApLiKaSI Binery tree
pROgRAN C++ ApLiKaSI Binery tree
 
โครงงาน เครื่องคิดเลข
โครงงาน เครื่องคิดเลขโครงงาน เครื่องคิดเลข
โครงงาน เครื่องคิดเลข
 
Data structures
Data structuresData structures
Data structures
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Ugly code
Ugly codeUgly code
Ugly code
 
#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docx#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docx
 

Dernier

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Dernier (20)

HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 

Avl tree

  • 1. AVL TREE #include<stdio.h> #include<conio.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; clrscr(); struct node *root = (struct node *)malloc(sizeof(struct node)); root = NULL; while(1) { printf("1.Insertn"); printf("2.Displayn"); printf("3.Quitn"); 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 ignoredn"); break; case 2: if(root==NULL) { printf("Tree is emptyn"); continue; } printf("Tree is :n"); display(root, 1); printf("nn"); printf("n"); break;
  • 2. case 3: exit(1); default: printf("Wrong choicen"); }/*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 Rotationn");
  • 3. pptr->lchild= aptr->rchild; aptr->rchild = pptr; pptr->balance = 0; aptr->balance=0; pptr = aptr; } else { printf("Left to right rotationn"); 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 Rotationn"); pptr->rchild= aptr->lchild; aptr->lchild = pptr; pptr->balance = 0;
  • 4. aptr->balance=0; pptr = aptr; } else { printf("Right to Left Rotationn"); 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()*/