-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLop.cpp
218 lines (199 loc) · 5.29 KB
/
AVLop.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include<iostream>
#include<cstdlib>
#define SPACE 10
using namespace std;
struct Node{
Node *left;
Node *right;
int data;
Node(int val) {
left = nullptr;
right = nullptr;
data = val;
}
};
int height(Node * root)
{
if (root == NULL)
return -1;
else {
int lheight = height(root -> left);
int rheight = height(root -> right);
if (lheight > rheight)
return (lheight + 1);
else
return (rheight + 1);
}
}
int BalanceFactor(Node * n)
{
if (n == NULL)
return 0;
return height(n -> left) - height(n -> right);
}
Node * rightRotate(Node * y) {
Node * x = y -> left;
Node * T2 = x -> right;
x -> right = y;
y -> left = T2;
return x;
}
Node * leftRotate(Node * x) {
Node * y = x -> right;
Node * T2 = y -> left;
y -> left = x;
x -> right = T2;
return y;
}
Node* insert_data(Node *root,int val)
{
Node* new_node = new Node(val);
new_node->data=val;
if(!root)
{
root = new_node;
return root;
}
if(val>(root->data)){
root->right=insert_data(root->right,val);
}
else if(val<(root->data)){
root->left=insert_data(root->left,val);
}
int bf =BalanceFactor(root);
if (bf > 1 && val < root -> left -> data)
return rightRotate(root);
if (bf < -1 && val > root -> right -> data)
return leftRotate(root);
if (bf > 1 && val > root -> left -> data)
{
root -> left = leftRotate(root -> left);
return rightRotate(root);
}
if (bf < -1 && val < root -> right -> data)
{
root -> right = rightRotate(root -> right);
return leftRotate(root);
}
return root;
}
void display(Node* root)
{
if (root != NULL)
{
display(root->left);
cout<<root->data<<" ";
display(root->right);
}
}
Node* search_ele(Node* root,int item){
if (root == nullptr) {
cout << "Item is not found!";
return nullptr;
}
if (root !=NULL){
if(item>(root->data)){
root->right=search_ele(root->right,item);
}
else if(item<(root->data)){
root->left=search_ele(root->left,item);
}
else if(item==(root->data)){
cout<<"\n"<<root->data<<" is found!";
}
}
else
{
cout<<"Item is not found!";
}
return root;
}
Node* delete_ele(Node* root, int val) {
if (!root) {
return nullptr;
}
if (val > root->data) {
root->right = delete_ele(root->right, val);
}
else if (val < root->data) {
root->left = delete_ele(root->left, val);
}
else if (val == root->data) {
// Case 1: No children
if (root->left == nullptr && root->right == nullptr) {
delete root;
return nullptr;
}
// Case 2: One child
else if (root->left == nullptr) {
Node* temp = root->right;
delete root;
return temp;
} else if (root->right == nullptr) {
Node* temp = root->left;
delete root;
return temp;
}
// Case 3: Two children
else {
Node* pred = root->left;
Node* predParent = nullptr;
while (pred->right != nullptr) {
predParent = pred;
pred = pred->right;
}
root->data = pred->data;
if (predParent != nullptr) {
predParent->right = pred->left;
} else {
root->left = pred->left;
}
delete pred;
}
}
else {
cout << "\nElement to be deleted is not found!";
}
int bf=BalanceFactor(root);
if (bf == 2 && BalanceFactor(root -> left) >= 0)
return rightRotate(root);
else if (bf == 2 && BalanceFactor(root -> left) == -1)
{
root -> left = leftRotate(root -> left);
return rightRotate(root);
}
else if (bf == -2 && BalanceFactor(root -> right) <= -0)
return leftRotate(root);
else if (bf == -2 && BalanceFactor(root -> right) == 1)
{
root -> right = rightRotate(root -> right);
return leftRotate(root);
}
return root;
}
void printtree(Node * root, int space) {
if (root == NULL)
return;
space += SPACE;
printtree(root -> right, space);
cout << endl;
for (int i = SPACE; i < space; i++)
cout << " ";
cout << root -> data << "\n";
printtree(root -> left, space);
}
int main()
{
Node* root = new Node(30);
insert_data(root,20);
insert_data(root,40);
insert_data(root,50);
insert_data(root, 10);
insert_data(root, 25);
display(root);
search_ele(root, 25);
delete_ele(root, 10);
cout <<"\n10 is deleted!"<<endl;
printtree(root, 5);
return 0;
}