-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathavl.cpp
175 lines (167 loc) · 3.13 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
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int data, height;
node *left, *right;
};
class avl
{
node* root=NULL;
public:
int height(node* root){
if(root)
return root->height;
return 0;
}
node* rotateLeft(node* root){
node* newroot = root->right;
root->right = newroot->left;
newroot->left = root;
return newroot;
}
node* rotateRight(node* root){
node* newroot = root->left;
root->left = newroot->right;
newroot->right = root;
return newroot;
}
void insert(int x){
this->root = insert(this->root, x);
}
node* insert(node* root, int x){
if(root==NULL){
root = new node;
root->data = x;
root->height = 1;
root->left = NULL;
root->right = NULL;
return root;
}
if(x < root->data)
root->left = insert(root->left,x);
else if(x > root->data)
root->right = insert(root->right,x);
else
return root;
int lh = height(root->left), rh = height(root->right);
root->height = 1 + max(lh,rh);
lh -= rh;
if(lh>1){
if(x > root->left->data)
root->left = rotateLeft(root->left);
return rotateRight(root);
}
else if(lh<-1){
if(x < root->right->data)
root->right = rotateRight(root->right);
return rotateLeft(root);
}
return root;
}
int minValue(node* root){
while(root->left)
root = root->left;
return root->data;
}
void remove(int x){
this->root = remove(this->root, x);
}
node* remove(node* root, int x){
if(root == NULL)
return NULL;
if(x < root->data)
root->left = remove(root->left, x);
else if(x > root->data)
root->right = remove(root->right, x);
else{
if(root->left == NULL && root->right == NULL){
delete root;
return NULL;
}
else if(root->right == NULL){
node* temp = root;
root = root->left;
delete temp;
return root;
}
else if(root->left == NULL){
node* temp = root;
root = root->right;
delete temp;
return root;
}
else{
root->data = minValue(root->right);
root->right = remove(root->right, root->data);
}
}
int lh = height(root->left), rh = height(root->right);
root->height = 1 + max(lh,rh);
lh -= rh;
if(lh>1){
if(x > root->left->data)
root->left = rotateLeft(root->left);
return rotateRight(root);
}
else if(lh<-1){
if(x < root->right->data)
root->right = rotateRight(root->right);
return rotateLeft(root);
}
return root;
}
void printLevel(){
printLevel(this->root);
}
void printLevel(node* root){
if(root){
queue<node*> q;
q.push(root);
q.push(NULL);
node* temp;
while(!q.empty()){
temp = q.front();
q.pop();
if(temp){
cout << temp->data << " ";
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
else if(q.front()){
cout << endl;
q.push(NULL);
}
}
}
}
void print(){
print(this->root);
}
void print(node* root){
if(root){
print(root->left);
cout << root->data << " ";
print(root->right);
}
}
};
int main()
{
avl a;
a.insert(5);
a.insert(6);
a.insert(7);
a.insert(8);
a.insert(9);
a.insert(4);
a.printLevel();
a.remove(7);
a.remove(5);
cout << endl;
a.printLevel();
return 0;
}