-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL Tree.cpp
60 lines (57 loc) · 1011 Bytes
/
AVL Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
using namespace std;
struct node{
node*left;
int item;
node*right;
};
class AVL{
private:
node * root;
public:
AVL();
void balanceFactor(int );
node* search(int );
};
node *AVL::search(int data)
{
node *ptr = root;
while (ptr)
{
if (ptr->item == data)
return ptr;
if (ptr->item > data)
ptr = ptr->left;
else
ptr = ptr->right;
}
return ptr;
}
void AVL::balanceFactor(int data){
node* ptr = search(data);
node* L=ptr->left;
node* R=ptr->right;
int l=0,r=0;
while(L->left && L->right!=NULL){
if(L->left==NULL)
L=L->right;
else
L=L->left;
l=l+1;
}
while(R->left && R->right!=NULL){
if(R->left==NULL)
R=R->right;
else
R=R->left;
r=r+1;
}
int diff=l-r;
cout<<diff;
}
AVL::AVL(){
root=NULL;
}
int main(){
return 0;
}