-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl.h
223 lines (178 loc) · 4.2 KB
/
avl.h
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
219
220
221
222
223
#ifndef avl_h
#define avl_h
#include <cstring>
#include<iostream>
#include<cstdio>
#include<time.h>
#define pow2(n) (1 << (n))
using namespace std;
struct avl {//same here i recommend to add the appearances but i dont know if it would work
string d;
int app=1;
struct avl *l;
struct avl *r;
}*r;
class avl_tree {
public:
int height(avl *);
int difference(avl *);
avl *rr_rotat(avl *);
avl *ll_rotat(avl *);
avl *lr_rotat(avl*);
avl *rl_rotat(avl *);
avl * balance(avl *);
avl * insert(avl*, string);
void show(avl*, int);
void inorder(avl *);
void preorder(avl *);
void postorder(avl*);
~avl_tree();
int rtimes(avl*, string);
int search(avl*, string);
avl_tree() {
r = NULL;
}
};
avl_tree::~avl_tree() //destructor for less ram
{
r=NULL;
}
int avl_tree::height(avl *t) {//it returns the height of the avl tree
int h = 0;
if (t != NULL) {
int l_height = height(t->l);
int r_height = height(t->r);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}
int avl_tree::difference(avl *t) {//it returns the difference between left and right
int l_height = height(t->l);
int r_height = height(t->r);
int b_factor = l_height - r_height;
return b_factor;
}
avl *avl_tree::rr_rotat(avl *parent) { //right right rotation
avl *t;
t = parent->r;
parent->r = t->l;
t->l = parent;
return t;
}
avl *avl_tree::ll_rotat(avl *parent) { //left left rotation
avl *t;
t = parent->l;
parent->l = t->r;
t->r = parent;
return t;
}
avl *avl_tree::lr_rotat(avl *parent) { //left right rotation
avl *t;
t = parent->l;
parent->l = rr_rotat(t);
return ll_rotat(parent);
}
avl *avl_tree::rl_rotat(avl *parent) { //right left rotation
avl *t;
t = parent->r;
parent->r = ll_rotat(t);
return rr_rotat(parent);
}
avl *avl_tree::balance(avl *t) { //it balances the avl tree
int bal_factor = difference(t);
if (bal_factor > 1) {
if (difference(t->l) > 0)
t = ll_rotat(t);
else
t = lr_rotat(t);
} else if (bal_factor < -1) {
if (difference(t->r) > 0)
t = rl_rotat(t);
else
t = rr_rotat(t);
}
return t;
}
avl *avl_tree::insert(avl *r, string v) { //it inserts strs into the tree
if (r == NULL) {
r = new avl;
r->d = v;
r->app=1;
r->l = NULL;
r->r = NULL;
return r;
}
else if (v == r->d) {
r->app +=1 ;
r = balance(r);
}
else if (v< r->d) {
r->l = insert(r->l, v);
r = balance(r);
}
else if (v > r->d) {
r->r = insert(r->r, v);
r = balance(r);
} return r;
}
void avl_tree::show(avl *p, int l) { //prints the tree
int i;
if (p != NULL) {
show(p->r, l+ 1);
cout<<" ";
if (p == r)
cout << "Root -> ";
for (i = 0; i < l&& p != r; i++)
cout << " ";
cout << p->d;
show(p->l, l + 1);
}
}
//all printable ways
void avl_tree::inorder(avl *t) {
if (t == NULL)
return;
inorder(t->l);
cout << t->d << ", ";
inorder(t->r);
}
void avl_tree::preorder(avl *t) {
if (t == NULL)
return;
cout << t->d << ", ";
preorder(t->l);
preorder(t->r);
}
void avl_tree::postorder(avl *t) {
if (t == NULL)
return;
postorder(t ->l);
postorder(t ->r);
cout << t->d << ", ";
}
int avl_tree :: rtimes( avl* rootPtr, string key)
{
//it returns appearances of strs into the tree
if (rootPtr->app == NULL || rootPtr->d == key)
return rootPtr->app;
if (rootPtr->d < key)
return rtimes(rootPtr->r, key);
return rtimes(rootPtr->l, key);
}
int avl_tree::search(avl *r, string v) { //it returns time of a search of a str into the tree
clock_t tStart = clock(); // working
if (v == r->d) {
return ((clock() - tStart)/(CLOCKS_PER_SEC/1000));
}
else if (v< r->d) {
r->l = insert(r->l, v);
r = balance(r);
}
else if (v > r->d) {
r->r = insert(r->r, v);
r = balance(r);
}
return ((clock() - tStart)/(CLOCKS_PER_SEC/1000));
}
#endif