-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkedList_AVL_Tree.c
305 lines (270 loc) · 6.84 KB
/
linkedList_AVL_Tree.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//List-Tree
// Implementar um programa em linguagem C em que o usuário informe uma série de números
// inteiros que devem ser armazenados em uma lista encadeada. O usuário poderá informar tantos
// números quanto desejar, na ordem em que desejar, e todos eles devem ser armazenados dentro
// da lista.
// Após o usuário ter informado todos os números, a lista deve ser transformada em uma árvore
// AVL. Ao final, o programa deve imprimir a lista original e árvore gerada a partir dela.
#include <stdio.h>
#include <stdlib.h>
typedef struct lstItem{ //struct da lista
int dado;
struct lstItem *next;
} lstNum;
typedef struct node{ //struct da árvore
int data;
struct node *left, *right;
int ht; //para altura da árvore ('height tree')
} node;
/*---------------------Funções-Lista----------------------*/
lstNum *cria_lista();
void imprime_lista(lstNum*);
lstNum *insere_no_fim(lstNum*, int);
int novo_num();
/*--------------------Funções-Árvore----------------------*/
node *insert(node*, int); //inserir na árvore
node *Delete(node*, int); //sem uso -> deletar nó
void preorder(node*); // sem uso -> por árvore em preorder
void inorder(node*); //por árvore 'inorder'
int height(node*); //altura árvore
node *rotateRight(node*); //fç rotacionar direita
node *rotateLeft(node*); //fç rotacionar esquerda
node *LR(node*); //joga da esq pra dir
node *RL(node*); //joga dir para esq
int FatBal(node*); //calcular fator de balanceamento
/*--------------------------------------------------------*/
int main(void){
int auxiliar = 9;
node *tree = NULL;
lstNum *LST = cria_lista();
while(auxiliar != 0){
system("CLS");
printf("Especifique o comando:\n");
printf("1 -> Inserir na lista\n");
printf("2 -> Imprimir lista\n");
printf("3 -> Imprimir arvore\n");
printf("0 -> Parar\n");
scanf("%d" , &auxiliar);
system("CLS");
switch(auxiliar){
case 1:{
int novo = novo_num();
LST = insere_no_fim(LST, novo);
break;
}
case 2:{
printf("Lista: ");
imprime_lista(LST);
printf("\n");
system("PAUSE");
break;
}
case 3:{
printf("Arvore Inorder: ");
int i;
while(LST!=NULL){ //alimentar a árvore com a lista
tree = insert(tree, LST->dado);
LST = LST->next;
}
inorder(tree);
printf("\n");
system("PAUSE");
break;
}
}
}
return 0;
}
// Criação da lista: retorna uma lista vazia
lstNum *cria_lista(){
return NULL;
}
// Imprime a lista.
// Esta função é um exemplo de como percorrer uma lista encadeada.
void imprime_lista(lstNum *lista){
// "atual" é um ponteiro para o elemento da lista que está sendo examinado. Inicialmente, aponta para o primeiro elemento
lstNum *atual = lista;
while(atual!=NULL){
printf("%d; ", atual->dado);
atual = atual->next;
}
}
lstNum *insere_no_fim(lstNum *lista, int dado){
lstNum *novo = malloc(sizeof(lstNum));
novo->dado = dado;
novo->next = NULL;
if(lista == NULL)
lista = novo;
else{
lstNum *ultimo = lista;
while(ultimo->next != NULL){
ultimo = ultimo->next;
}
ultimo->next = novo;
}
return lista;
}
int novo_num(){
int novo = 0;
printf("Digite um número: ");
scanf("%d", &novo);
return novo;
}
/*-----------------------------Funções-Árovre------------------------------*/
node *insert(node *Tree, int x)
{
if (Tree == NULL){
Tree = (node*)malloc(sizeof(node));
Tree->data = x;
Tree->left = NULL;
Tree->right = NULL;
}
else if (x > Tree->data) { // inserir na sub-árvore da direita
Tree->right = insert(Tree->right, x);
if (FatBal(Tree) == -2)
if (x > Tree->right->data)
Tree = rotateLeft(Tree);
else
Tree = rotateRight(Tree);
}
else if (x < Tree->data){ // inserir na sub-árvore da esquerda
Tree->left = insert(Tree->left, x);
if (FatBal(Tree) == 2)
if (x < Tree->left->data)
Tree = rotateRight(Tree);
else
Tree = LR(Tree);
}
Tree->ht = height(Tree);
return (Tree);
}
node *Delete(node *Tree, int x)
{
node *p;
if (Tree == NULL) {
return NULL;
}
else
if (x > Tree->data) // insert in right subtree
{
Tree->right = Delete(Tree->right, x);
if (BF(T) == 2)
if (BF(Tree->left) >= 0)
Tree = LL(Tree);
else
Tree = LR(Tree);
}
else
if (x < Tree->data) {
Tree->left = Delete(Tree->left, x);
if (BF(Tree) == -2) //Rebalance during windup
if (BF(Tree->right) <= 0)
Tree = RR(Tree);
else
Tree = RL(Tree);
}
else {
//data to be deleted is found
if (Tree->right != NULL) { //delete its inorder succesor
p = Tree->right;
while (p->left != NULL)
p = p->left;
Tree->data = p->data;
Tree->right = Delete(Tree->right, p->data);
if (BF(Tree) == 2)//Rebalance during windup
if (BF(Tree->left) >= 0)
Tree = LL(Tree);
else
Tree = LR(Tree);
}
else
return (Tree->left);
}
Tree->ht = height(Tree);
return (Tree);
}
int height(node *Tree) //altura da árvore
{
int lh, rh;
if(Tree == NULL)
return 0;
if(Tree->left == NULL)
lh = 0;
else
lh = 1 + Tree->left->ht;
if(Tree->right == NULL)
rh = 0;
else
rh = 1 + Tree->right->ht;
if(lh > rh)
return lh;
return rh;
}
node *rotateRight(node *Tree)
{
node *newTree;
newTree = Tree->left;
Tree->left = newTree->right;
newTree->right = Tree;
Tree->ht = height(Tree);
newTree->ht = height(newTree);
return newTree;
}
node *rotateLeft(node *Tree)
{
node *newTree;
newTree = Tree->right;
Tree->right = newTree->left;
newTree->left = Tree;
Tree->ht = height(Tree);
newTree->ht = height(newTree);
return newTree;
}
node *LR(node *Tree)
{
Tree->left = rotateLeft(Tree->left);
Tree = rotateRight(Tree);
return (Tree);
}
node *RL(node *Tree)
{
Tree->right = rotateRight(Tree->right);
Tree = rotateLeft(Tree);
return (Tree);
}
int FatBal(node *Tree) //fator de balanceamento
{
int lh, rh; //altura esq / altura dir
if(Tree == NULL)
return (0);
if(Tree->left == NULL)
lh = 0;
else
lh = 1 + Tree->left->ht;
if(Tree->right == NULL)
rh = 0;
else
rh = 1 + Tree->right->ht;
return (lh - rh);
}
void print(node *Tree){
if(Tree != NULL){
printf("%d(FB=%d)", Tree->data, FatBal(Tree));
}
}
void preorder(node *Tree)
{
if (Tree != NULL) {
printf("%d(FB=%d)", Tree->data, FatBal(Tree));
preorder(Tree->left);
preorder(Tree->right);
}
}
void inorder(node *Tree)
{
if (Tree != NULL) {
inorder(Tree->left);
printf("%d(FB=%d)", Tree->data, FatBal(Tree));
inorder(Tree -> right);
}
}