-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradoDiSquilibrio.c
71 lines (62 loc) · 1.22 KB
/
gradoDiSquilibrio.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
#include <stdio.h>
#include <stdlib.h>
/*
Grado di squilibrio di un albero
*/
typedef struct node
{
int key;
struct node *p;
struct node *left;
struct node *right;
} * Node;
Node createNode(int val)
{
Node new_node = (Node)malloc(sizeof(struct node));
new_node->key = val;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
int gradosquilaux(Node u, int *max)
{
int buffer = 0;
int sommaSx;
int sommaDx;
if (u == NULL)
{
return 0;
}
if (u->left == NULL && u->right == NULL)
{
return u->key;
}
else
{
sommaSx = gradosquilaux(u->left, max);
sommaDx = gradosquilaux(u->right, max);
buffer = abs(sommaSx - sommaDx);
if (buffer > *max)
{
*max = buffer;
}
}
return sommaSx + sommaDx;
}
int gradosquil(Node u)
{
int max = 0;
gradosquilaux(u, &max);
return max;
}
void main()
{
Node root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->left->right->left = createNode(6);
int i = gradosquil(root);
printf("Result: %d", i);
}