-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl.cpp
More file actions
195 lines (166 loc) · 3.53 KB
/
avl.cpp
File metadata and controls
195 lines (166 loc) · 3.53 KB
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
#include<iostream>
#define SPACES 10;
using namespace std;
int max(int a, int b) {
return a > b ? a : b;
}
struct tree {
int value, height;
tree *left, *right;
tree(int v) {
value = v;
height = 1;
left = NULL;
right = NULL;
}
};
int getHeight(tree *root) {
return root != NULL ? root->height : 0;
}
int getBalance(tree *root) {
return getHeight(root->left) - getHeight(root->right);
}
void updateHeight(tree *x) {
if(x != NULL)
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
}
tree *rotateLeft(tree *x) {
tree *y = x->right;
tree *t = y->left;
y->left = x;
x->right = t;
updateHeight(x);
updateHeight(y);
return y;
}
tree *rotateRight(tree *x) {
tree *y = x->left;
tree *t = y->right;
y->right = x;
x->left = t;
updateHeight(x);
updateHeight(y);
return y;
}
void inorder(tree *t) {
if(t != NULL) {
inorder(t->left);
cout << t->value;
inorder(t->right);
}
}
tree *minValueNode(tree *root) {
tree* current = root;
while (current && current->left != NULL)
current = current->left;
return current;
}
tree* insert(tree *root, int x) {
if(root == NULL) {
root = new tree(x);
} else {
if(x < root->value) {
root->left = insert(root->left, x);
} else if (x > root->value) {
root->right = insert(root->right, x);
}
}
updateHeight(root);
int b = getBalance(root);
if(b > 1 && x < root->left->value) {
root = rotateRight(root);
} else if (b < -1 && x > root->right->value) {
root = rotateLeft(root);
} else if (b > 1 && x > root->left->value) {
root->left = rotateLeft(root->left);
root = rotateRight(root);
} else if (b < -1 && x < root->right->value) {
root->right = rotateRight(root->right);
root = rotateLeft(root);
}
return root;
}
tree* deleteNode(tree *root, int x) {
if(root == NULL) {
cout << "Node not found.";
return root;
}
if(x == root->value) {
if(root->left == NULL) {
tree *t = root;
root = root->right;
delete t;
} else if(root->right == NULL) {
tree *t = root;
root = root->left;
delete t;
} else {
tree *t = minValueNode(root->right);
root->value = t->value;
root->right = deleteNode(root->right, t->value);
}
} else if(x < root->value) {
root->left = deleteNode(root->left, x);
} else if (x > root->value) {
root->right = deleteNode(root->right, x);
}
if(root == NULL) return root;
updateHeight(root);
int b = getBalance(root);
if(b > 1) {
int bl = getBalance(root->left);
if(bl == -1) {
root->left = rotateLeft(root->left);
root = rotateRight(root);
} else {
root = rotateRight(root);
}
} else if (b < -1) {
int br = getBalance(root->right);
if(br == 1) {
root->right = rotateRight(root->right);
root = rotateLeft(root);
} else {
root = rotateLeft(root);
}
}
return root;
}
void displayTree(tree *root, int space = 0) {
if(!(root == NULL)) {
space += SPACES;
displayTree(root->right, space);
cout << endl;
int i = SPACES;
for (; i < space; i++) {
cout<<" ";
}
cout << root->value;
displayTree(root->left, space);
}
}
int main() {
tree *root = NULL;
updateHeight(NULL);
int ch,n;
do {
cout << "\n1.Insert Node\n2.Delete Node\n3.Display Tree\n0. Exit\nEnter your choice: ";
cin >> ch;
switch(ch){
case 1:
cout << "\nEnter value of node:";
cin >> n;
root=insert(root,n);
break;
case 2:
cout << "\nEnter value to be deleted: ";
cin >> n;
root=deleteNode(root,n);
break;
case 3:
displayTree(root);
break;
}
} while (ch != 0);
return 0;
}