Skip to content

Commit 4bb519d

Browse files
Added AVL tree
1 parent 563d9cb commit 4bb519d

File tree

1 file changed

+286
-0
lines changed

1 file changed

+286
-0
lines changed

Trees/AVL Tree.cpp

+286
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <unordered_map>
4+
using namespace std;
5+
6+
struct node {
7+
struct node *left;
8+
int data;
9+
int height;
10+
struct node *right;
11+
};
12+
13+
class AVL
14+
{
15+
private:
16+
17+
public:
18+
struct node * root;
19+
AVL(){
20+
this->root = NULL;
21+
22+
}
23+
24+
int calheight(struct node *p){
25+
26+
if(p->left && p->right){
27+
if (p->left->height < p->right->height)
28+
return p->right->height + 1;
29+
else return p->left->height + 1;
30+
}
31+
else if(p->left && p->right == NULL){
32+
return p->left->height + 1;
33+
}
34+
else if(p->left ==NULL && p->right){
35+
return p->right->height + 1;
36+
}
37+
return 0;
38+
39+
}
40+
41+
int bf(struct node *n){
42+
if(n->left && n->right){
43+
return n->left->height - n->right->height;
44+
}
45+
else if(n->left && n->right == NULL){
46+
return n->left->height;
47+
}
48+
else if(n->left== NULL && n->right ){
49+
return -n->right->height;
50+
}
51+
}
52+
53+
struct node * llrotation(struct node *n){
54+
struct node *p;
55+
struct node *tp;
56+
p = n;
57+
tp = p->left;
58+
59+
p->left = tp->right;
60+
tp->right = p;
61+
62+
return tp;
63+
}
64+
65+
66+
struct node * rrrotation(struct node *n){
67+
struct node *p;
68+
struct node *tp;
69+
p = n;
70+
tp = p->right;
71+
72+
p->right = tp->left;
73+
tp->left = p;
74+
75+
return tp;
76+
}
77+
78+
79+
struct node * rlrotation(struct node *n){
80+
struct node *p;
81+
struct node *tp;
82+
struct node *tp2;
83+
p = n;
84+
tp = p->right;
85+
tp2 =p->right->left;
86+
87+
p -> right = tp2->left;
88+
tp ->left = tp2->right;
89+
tp2 ->left = p;
90+
tp2->right = tp;
91+
92+
return tp2;
93+
}
94+
95+
struct node * lrrotation(struct node *n){
96+
struct node *p;
97+
struct node *tp;
98+
struct node *tp2;
99+
p = n;
100+
tp = p->left;
101+
tp2 =p->left->right;
102+
103+
p -> left = tp2->right;
104+
tp ->right = tp2->left;
105+
tp2 ->right = p;
106+
tp2->left = tp;
107+
108+
return tp2;
109+
}
110+
111+
struct node* insert(struct node *r,int data){
112+
113+
if(r==NULL){
114+
struct node *n;
115+
n = new struct node;
116+
n->data = data;
117+
r = n;
118+
r->left = r->right = NULL;
119+
r->height = 1;
120+
return r;
121+
}
122+
else{
123+
if(data < r->data)
124+
r->left = insert(r->left,data);
125+
else
126+
r->right = insert(r->right,data);
127+
}
128+
129+
r->height = calheight(r);
130+
131+
if(bf(r)==2 && bf(r->left)==1){
132+
r = llrotation(r);
133+
}
134+
else if(bf(r)==-2 && bf(r->right)==-1){
135+
r = rrrotation(r);
136+
}
137+
else if(bf(r)==-2 && bf(r->right)==1){
138+
r = rlrotation(r);
139+
}
140+
else if(bf(r)==2 && bf(r->left)==-1){
141+
r = lrrotation(r);
142+
}
143+
144+
return r;
145+
146+
}
147+
148+
void levelorder_newline(){
149+
if (this->root == NULL){
150+
cout<<"\n"<<"Empty tree"<<"\n";
151+
return;
152+
}
153+
levelorder_newline(this->root);
154+
}
155+
156+
void levelorder_newline(struct node *v){
157+
queue <struct node *> q;
158+
struct node *cur;
159+
q.push(v);
160+
q.push(NULL);
161+
162+
while(!q.empty()){
163+
cur = q.front();
164+
q.pop();
165+
if(cur == NULL && q.size()!=0){
166+
cout<<"\n";
167+
168+
q.push(NULL);
169+
continue;
170+
}
171+
if(cur!=NULL){
172+
cout<<" "<<cur->data;
173+
174+
if (cur->left!=NULL){
175+
q.push(cur->left);
176+
}
177+
if (cur->right!=NULL){
178+
q.push(cur->right);
179+
}
180+
}
181+
182+
183+
}
184+
}
185+
186+
struct node * deleteNode(struct node *p,int data){
187+
188+
if(p->left == NULL && p->right == NULL){
189+
if(p==this->root)
190+
this->root = NULL;
191+
delete p;
192+
return NULL;
193+
}
194+
195+
struct node *t;
196+
struct node *q;
197+
if(p->data < data){
198+
p->right = deleteNode(p->right,data);
199+
}
200+
else if(p->data > data){
201+
p->left = deleteNode(p->left,data);
202+
}
203+
else{
204+
if(p->left != NULL){
205+
q = inpre(p->left);
206+
p->data = q->data;
207+
p->left=deleteNode(p->left,q->data);
208+
}
209+
else{
210+
q = insuc(p->right);
211+
p->data = q->data;
212+
p->right = deleteNode(p->right,q->data);
213+
}
214+
}
215+
216+
if(bf(p)==2 && bf(p->left)==1){ p = llrotation(p); }
217+
else if(bf(p)==2 && bf(p->left)==-1){ p = lrrotation(p); }
218+
else if(bf(p)==2 && bf(p->left)==0){ p = llrotation(p); }
219+
else if(bf(p)==-2 && bf(p->right)==-1){ p = rrrotation(p); }
220+
else if(bf(p)==-2 && bf(p->right)==1){ p = rlrotation(p); }
221+
else if(bf(p)==-2 && bf(p->right)==0){ p = llrotation(p); }
222+
223+
224+
return p;
225+
}
226+
227+
struct node* inpre(struct node* p){
228+
while(p->right!=NULL)
229+
p = p->right;
230+
return p;
231+
}
232+
233+
struct node* insuc(struct node* p){
234+
while(p->left!=NULL)
235+
p = p->left;
236+
237+
return p;
238+
}
239+
240+
~AVL(){
241+
242+
}
243+
};
244+
245+
246+
int main(){
247+
248+
AVL b;
249+
int c,x;
250+
251+
do{
252+
cout<<"\n1.Display levelorder on newline";
253+
cout<<"\n2.Insert";
254+
cout<<"\n3.Delete\n";
255+
cout<<"\n0.Exit\n";
256+
cout<<"\nChoice: ";
257+
258+
cin>>c;
259+
260+
switch (c)
261+
{
262+
case 1:
263+
b.levelorder_newline();
264+
// to print the tree in level order
265+
break;
266+
267+
case 2:
268+
cout<<"\nEnter no. ";
269+
cin>>x;
270+
b.root = b.insert(b.root,x);
271+
break;
272+
273+
case 3:
274+
cout<<"\nWhat to delete? ";
275+
cin>>x;
276+
b.root = b.deleteNode(b.root,x);
277+
break;
278+
279+
case 0:
280+
break;
281+
}
282+
283+
} while(c!=0);
284+
285+
}
286+

0 commit comments

Comments
 (0)