/
treeA.c
218 lines (162 loc) · 3.33 KB
/
treeA.c
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "treeA.h"
// NODES SAO DESIGNADOS POR "NOD" DEVIDO A ERRO DE PARSING DO GCC
void cleanTree(node *t) {
if(t) {
cleanTree(t->right);
cleanTree(t->left);
free(t->username);
free(t->password);
free(t);
}
}
tree* createTree() {
tree* tree = NULL;
if((tree=malloc(sizeof(tree)))==NULL)
return NULL;
tree->root = NULL;
return tree;
}
node* createNode() {
node* nod = NULL;
if((nod=malloc(sizeof(node)))==NULL)
return NULL;
nod->left = NULL;
nod->right= NULL;
nod->username= " ";
nod->password= " ";
return nod;
}
int heightTree(node* nod) {
int height_left = 0;
int height_right = 0;
if(nod->left)
height_left = heightTree(nod->left);
if(nod->right)
height_right = heightTree(nod->right);
return height_right > height_left ? ++height_right : ++height_left;
}
int balanceFactor(node* node) {
int bf = 0;
if(node->left)
bf += heightTree(node->left);
if(node->right)
bf -= heightTree(node->right);
return bf;
}
node* rotateLL(node* nod) {
node* a = nod;
node* b = a->left;
a->left = b->right;
b->right = a;
return(b);
}
node* rotateLR(node* nod) {
node* a = nod;
node* b = a->left;
node* c = b->right;
a->left = c->right;
b->right= c->left;
c->left = b;
c->right= a;
return(c);
}
node* rotateRL(node* nod) {
node* a = nod;
node* b = a->right;
node* c = b->left;
a->right= c->left;
b->left = c->right;
c->right= b;
c->left = a;
return(c);
}
node* rotateRR(node* nod) {
node* a = nod;
node* b = a->right;
a->right= b->left;
b->left = a;
return(b);
}
node* balanceNode(node* nod) {
node* newroot = NULL;
int bf = 0;
if(nod->left)
nod->left = balanceNode(nod->left);
if(nod->right)
nod->right= balanceNode(nod->right);
bf = balanceFactor(nod);
if(bf >= 2) {
if(balanceFactor(nod->left) <= -1 )
newroot = rotateLR(nod);
else
newroot = rotateLL(nod);
}else if(bf <= -2) {
if(balanceFactor(nod->right)<= 1)
newroot = rotateRL(nod);
else
newroot = rotateRR(nod);
}else
newroot = nod;
return(newroot);
}
void balanceTree(tree *t) {
node* newroot = NULL;
newroot = balanceNode(t->root);
if(newroot != t->root)
t->root = newroot;
}
void insertTree(tree *t, char *name, char *pass) {
node* nod = NULL;
node* next = NULL;
node* last = NULL;
if(t->root == NULL) {
nod = createNode();
nod->username=strdup(name);
nod->password=strdup(pass);
t->root = nod;
}else {
next = t->root;
while(next != NULL) {
last = next;
if(strcmp(next->username,name)>0)
next = next->left;
else
next = next->right;
}
nod = createNode();
nod->username = strdup(name);
nod->password = strdup(pass);
if(strcmp(last->username,name)>0)
last->left = nod;
if(strcmp(last->username,name)<0)
last->right= nod;
}
balanceTree(t);
}
void printTree(node *t, FILE *fd) {
if(t != NULL) {
printTree(t->left,fd);
fprintf(fd,"%s;%s\n",t->username,t->password);
printTree(t->right,fd);
}
}
/* 0-NAO EXISTE! 1-EXISTE! */
int existUser(node *t,char *username, char *pass) {
if(t) {
if(strcmp(t->username,username) == 0){
if(strcmp(t->password,pass) == 0)
return 1;
else
return -4;
}
if(strcmp(t->username,username) > 0)
return existUser(t->left,username,pass);
else
return existUser(t->right,username,pass);
}
return -2;
}