All source code in C/ C++ Ask a C/ C++ Pro Discussion Forum Categories All jobs in C/ C++
AVL,Illustrates,tree,rotation
   Code/Articles � |  Newest/Best � |  Community � |  Jobs � |  Other � |  Goto � | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 1,244,848. lines
 Jobs: 173. postings

 How to support the site

 
Sponsored by:
Quick Search for:  in language:    
You are in:
 
Login





Latest postings for C/ C++.
Click here to see a screenshot of this code!Simple Payroll Syste Using Object Oriented Programming
By Jake Rodriguez Pomperada on 3/8

(Screen Shot)

Black Jack OOP
By Micu Danie on 3/8


Click here to see a screenshot of this code!Tasks
By Ali Moussawi on 3/7

(Screen Shot)

Click here to see a screenshot of this code!vijendra's car
By vjendra singh on 3/6

(Screen Shot)

Taj Lahal
By vjendra singh on 3/6


To Find Minimum and Maximum Height of a Binary Tree
By jeyaveerapandiy an on 3/3


Click here to see a screenshot of this code!ATM Machine
By arifliminto86 on 3/3

(Screen Shot)

Click here to see a screenshot of this code!Gap Buffer varient without a gap
By Joe Smith on 3/2

(Screen Shot)

Click here to see a screenshot of this code!Simple calculator
By walid brahem on 3/2

(Screen Shot)

File Editor
By Prasad Priyadarshana Fernando on 3/2


Text Finder
By Prasad Priyadarshana Fernando on 3/2


Simple TCP/IP Client & Server
By Prasad Priyadarshana Fernando on 3/1


LUTTAPI-1
By melwin jose on 2/27


Prime Factorization
By SergiuXxCracker on 2/27


netcmdx
By sam lee on 6/18


Click here to see a screenshot of this code!Yard To Feet Converter 1.0 Using Classes
By Jake Rodriguez Pomperada on 2/24

(Screen Shot)

Click here to see a screenshot of this code!Addition of Numbers Using Classes and Pointers
By Jake Rodriguez Pomperada on 2/24

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

AVL Rotation

Print
Email
 
VB icon
Submitted on: 11/17/2009 9:02:42 AM
By: Adri Jovin  
Level: Advanced
User Rating: Unrated
Compatibility:C++ (general)

Users have accessed this code 1377 times.
 
author picture
(About the author)
 
     Illustrates AVL tree rotation
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: AVL Rotation
// Description:Illustrates AVL tree rota
//     tion
// By: Adri Jovin
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=12927&lngWId=3//for details.//**************************************
//     

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class avltree;
class avlnode


    {
    public:
    int element,ht;
    avlnode *left,*right;
    friend class avltree;
}*root;

class avltree { public: int max(int,int); int height(avlnode *t); void inorder(avlnode *&t); void preorder(avlnode *&t); void insert(avlnode *&t,int x); void s_rotate_left(avlnode *&t); void s_rotate_right(avlnode *&t); void d_rotate_left(avlnode *&t); void d_rotate_right(avlnode *&t); };
int avltree::max(int v1,int v2) { return((v1>v2)?v1:v2); }
int avltree::height(avlnode *t) { return(t==NULL?-1:t->ht); }
void avltree::insert(avlnode *&t,int x) { if(t==NULL) { t=new avlnode; t->left=NULL; t->right=NULL; t->ht=NULL; t->element=x; }
else if(x<t->element) { insert(t->left,x); if(height(t->left)-height(t->right)==2) { if(x<t->left->element) s_rotate_left(t); else d_rotate_left(t); }
}
else if(x>t->element) { insert(t->right,x); if(height(t->right)-height(t->left)==2) { if(x>t->right->element) s_rotate_right(t); else d_rotate_right(t); }
}
t->ht=max(height(t->left),height(t->right))+1; }
void avltree::s_rotate_left(avlnode *&k2) { avlnode *k1; k1=k2->left; k2->left=k1->right; k1->right=k2; k2->ht=max(height(k2->left),height(k2->right))+1; k1->ht=max(height(k1->left),k2->ht)+1; k2=k1; cout<<"\nSINGLE ROATION LEFT\n"; }
void avltree::s_rotate_right(avlnode *&k2) { avlnode *k1; k1=k2->right; k2->right=k1->left; k1->left=k2; k2->ht=max(height(k1->right),height(k1->left))+1; k1->ht=max(height(k1->right),k2->ht)+1; k2=k1; cout<<"\n SINGLY ROTATION RIGHT:\n"; }
void avltree::d_rotate_left(avlnode *&k3) { s_rotate_right(k3->left); s_rotate_left(k3); }
void avltree::d_rotate_right(avlnode *&k3) { s_rotate_left(k3->right); s_rotate_right(k3); }
void avltree::inorder(avlnode *&t) { if(t!=NULL) { cout<<t->element<<"\t"; inorder(t->right); }
}
void avltree::preorder(avlnode *&t) { if(t!=NULL) { cout<<t->element<<"\t"; preorder(t->left); preorder(t->right); }
}
void main() { avltree a1; int n,ch,opt,num; root=NULL; clrscr(); do { cout<<"\n AVL ROTATIONS\n"; cout<<"\n1.Insertion\t2.Display\t3.Exit\n"; cout<<"Enter your choice:\n"; cin>>ch; switch(ch) { case 1: cout<<"Enter the number to insert\n"; cin>>num; a1.insert(root,num); break; case 2: cout<<"Display\n"; cout<<"\n1.Inorder 2.Preorder 3.Back to main menu\n"; do { cout<<"Enter your choice\n"; cin>>opt; switch(opt) { case 1: a1.inorder(root); break; case 2: a1.preorder(root); break; case 3: cout<<"Back to main menu\n"; break; }
}
while(opt!=3); break; case 3: exit(0); }
}
while(ch!=4); getch(); }


Other 22 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Games | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright� 1997-2010 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.