-
Notifications
You must be signed in to change notification settings - Fork 1
/
arv_tree_int.c
89 lines (75 loc) · 1.92 KB
/
arv_tree_int.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
#include <stdio.h>
#include <stdlib.h>
#define MAX(a,b) (a>b)? a : b
typedef struct Node{
int value;
int h; //fator de balanceamento
struct Node *left, *right;
}node;
void altura(node *);
node* right_rotate(node * a){
node *b=a->left;
node *tmp = b->right;
b->right=a;
a->left=tmp;
altura(a);
altura(b);
return b;
}
void altura(node *root){
root->h=MAX(((root->left!=NULL)? root->left->h : 0),((root->right!=NULL)? root->right->h : 0));
}
node * left_rotate(node * a){
node *b= a->right;
node *tmp = b->left;
a->right=tmp;
b->left=a;
altura(a);
altura(b);
return b;
}
node *novo(int v){
node *tmp = (node*)malloc(sizeof(node));
tmp->value=v;
tmp->h=0;
return tmp;
}
node *arv_insert(node *root, node *element){
if(root==NULL)return element;
else{
if(root->value<element->value)root->right=arv_insert(root->right,element);
else root->left=arv_insert(root->left,element);
root->h=MAX(((root->left==NULL)? 0 :root->left->h+1),((root->right==NULL)? 0 : root->right->h+1));//nova altura da árvore
int balance=((root->right==NULL)? -1 :root->right->h)-((root->left==NULL)? -1 : root->left->h);
if(balance>1 && element->value>root->right->value) return left_rotate(root);
if(balance<-1 && element->value<root->left->value) return right_rotate(root);
if(balance>1 && element->value < root->left->value){
root->right=right_rotate(root->right);
return left_rotate(root);
}
if(balance<-1 && element->value > root->right->value){
root->left=left_rotate(root->left);
return right_rotate(root);
}
}
return root;
}
void listar(node *root){
if(root==NULL)printf("Arvore vazia\n");
else{
if(root->left!=NULL)listar(root->left);
printf("[%d] : %d\n", root->value,root->h);
if(root->right!=NULL)listar(root->right);
}
}
int main(){
int vet[]={1,2,3,4};
int i;
node *mp = NULL;
for(i=0; i<4; ++i){
printf("interação %d\n" ,i);
mp=arv_insert(mp,novo(vet[i]));
}
listar(mp);
return 0;
}