-
Notifications
You must be signed in to change notification settings - Fork 2
/
avl.cpp
187 lines (187 loc) · 4.28 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
182
183
184
185
186
187
#include <bits/stdc++.h>
using namespace std;
struct tree {
int data;
int size=0;
tree *left;
tree *right;
};
int findheight(tree *p) {
if(p==NULL) return 0;
return (max(findheight(p->left), findheight(p->right))+1);
}
int heightDifference(tree *p)
{
int left=findheight(p->left);
int right=findheight(p->right);
return (left-right);
}
tree *leftleft(tree *root)
{
tree *ptr;
ptr=root->left;
root->left=ptr->right;
ptr->right=root;
return ptr;
}
tree *rightright(tree *root)
{
tree *ptr;
ptr=root->right;
root->right=ptr->left;
ptr->left=root;
return ptr;
}
tree *leftright(tree *root)
{
tree *ptr;
ptr=root->left;
root->left=rightright(ptr);
return leftleft(root);
}
tree *rightleft(tree *root)
{
tree *ptr;
ptr=root->right;
root->right=leftleft(ptr);
return rightright(root);
}
tree *balancing(tree *root)
{
if(heightDifference(root)>1)
{
if(heightDifference(root->left)>0)
root=leftleft(root);
else
root=leftright(root);
}
else if(heightDifference(root)<-1)
{
if(heightDifference(root->right)>0)
root=rightleft(root);
else
root=rightright(root);
}
return root;
}
tree *insert(tree* root, int data)
{
if (root == NULL) {
root = new tree;
root->data = data;
root->left = NULL;
root->right = NULL;
return root;
}
else if (data< root->data) {
root->left = insert(root->left, data);
root = balancing(root);
} else if (data >= root->data) {
root->right = insert(root->right, data);
root = balancing(root);
}
root->size+=1;
return root;
}
tree *getsuccessor(tree *current)
{
current = current->right;
while (current != NULL && current->left != NULL)
current = current->left;
return current;
}
tree *delnode(tree *ptr, int data)
{
if (ptr == NULL)
return ptr;
if (ptr->data > data)
ptr->left = delnode(ptr->left, data);
else if (ptr->data < data)
ptr->right = delnode(ptr->right, data);
else
{
if (ptr->left == NULL)
{
tree *temp = ptr->right;
delete ptr;
return temp;
}
else if (ptr->right == NULL)
{
tree *temp = ptr->left;
delete ptr;
return temp;
}
else
{
tree *successor = getsuccessor(ptr);
ptr->data = successor->data;
ptr->right = delnode(ptr->right, successor->data);
}
}
return ptr;
}
void display(tree *root)
{
if(root==NULL)
return;
else
{
display(root->left);
cout<<root->data<<" ";
display(root->right);
}
}
int main()
{
int c, data,n;
bool res;
tree *root = NULL;
bool flag = true;
while (flag)
{
cout << "--------------------------------------" << endl;
cout << "enter 1 to insert a element in a AVL tree" << endl
<< "enter 2 to delete a element from a BST" << endl;
cout << "enter 3 to display the tree in inorder form" << endl<<"enter 4 to exit"<<endl
<< "enter your choice: ";
cin >> c;
switch (c)
{
case 1:
cout << "enter the number of elements: ";
cin >> n;
for(int i=1;i<=n;i++)
{
cin>>data;
root = insert(root, data);
}
cout << "all the "<<n<<" elements are inserted successfully" << endl;
break;
case 2:
if (root->size == 0)
cout << "the AVL tree is already empty nothing to delete" << endl;
else
{
cout << "enter the data to be deleted in a AVL tree: ";
cin >> data;
root = delnode(root, data);
root = balancing(root);
cout << "the node with data " << data << " is deleted successfully" << endl;
root->size -= 1;
}
break;
case 3:
display(root);
cout<<endl;
break;
case 4:
flag = false;
break;
default:
cout << "INVALID CHOICE" << endl;
break;
}
}
return 0;
}