forked from namigoel/Hacktober-Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.cpp
181 lines (142 loc) · 3.42 KB
/
AVL.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* left=nullptr;;
Node* right=nullptr;
int bal=0;
int h=0;
//
Node(int data){
this->data=data;
}
Node(int b, int h){
this->left=nullptr;
this->right=nullptr;
this->bal=b;
this->h=h;
}
};
void display(Node* root){
if(root==nullptr) return;
string s="";
s+= root->left!=nullptr ? to_string(root->left->data) : ".";
s+="<- " + to_string(root->data) + " ->";
s+= root->right!=nullptr ? to_string(root->right->data) : ".";
cout<<s<<"\n";
display(root->left);
display(root->right);
}
int getMax(Node* root){
int ans=root->data;
while(root->right!=nullptr){
ans=root->right->data;
root=root->right;
}
return ans;
}
void updateHeightAndBalance(Node* node){
int lh=-1;
int rh=-1;
if(node->left!=nullptr)
lh=node->left->h;
if(node->right!=nullptr)
rh=node->left->h;
node->h=max(lh,rh)+1;
node->bal=lh-rh;
}
Node* rightRotation(Node* A){
Node* B=A->left;
Node* BKaRight=B->right;
B->right=A;
A->left=BKaRight;
return B;
}
Node* leftRotation(Node* A){
Node* B=A->right;
Node* BkaLeft=B->left;
B->left=A;
A->right=BkaLeft;
return B;
}
Node* rightLeftRotation(Node* A){
Node* B=A->right;
Node* C=B->left;
Node* CkaLeft=C->left;
C->left=B;
B->left=CkaLeft;
A->right=C;
return leftRotation(A);
}
Node* leftRightRotation(Node* A){
Node* B=A->left;
Node* C=B->right;
Node* CKaLeft=C->left;
C->left=B;
B->right=CKaLeft;
A->left=C;
return rightRotation(A);
}
Node* getRotation(Node* root){
updateHeightAndBalance(root);
if(root->bal==2){ //ll,lr // root->bal<=2 if we are calculating height in terms of node;
if(root->left->bal==1){
return rightRotation(root);
} else if(root->left->bal== -1){
root->left=leftRotation(root->left);
return rightRotation(root);
}
} else if(root->bal== -2){
if(root->right->bal==1){
root->right=rightRotation(root->right);
return leftRotation(root);
} else if(root->right->bal == -1){
return leftRotation(root);
}
}
return root;
}
Node* insert(Node* root, int val){
if(root==nullptr) return new Node(val);
if(val<root->data){
root->left=insert(root->left,val);
} else {
root->right=insert(root->right,val);
}
root=getRotation(root);
return root;
}
Node* deleteNode(Node *root, int val) //O(logn)
{
if (root == nullptr)
return nullptr;
if (val < root->data)
root->left = deleteNode(root->left, val);
else if (val > root->data)
root->right = deleteNode(root->right, val);
else
{
if (root->left == nullptr || root->right == nullptr)
{
Node *node = root->left != nullptr ? root->left : root->right;
delete root; // not for java.
return node;
}
int mVal = getMax(root->left);
root->data = mVal;
root->left = deleteNode(root->left, mVal);
}
root = getRotation(root);
return root;
}
void solve()
{
Node *root = nullptr;
for (int i = 1; i <= 14; i++)
root = insert(root, i * 10);
display(root);
}
int main(){
return 0;
}