-
Notifications
You must be signed in to change notification settings - Fork 1
/
zad16_ms.c
99 lines (88 loc) · 2.39 KB
/
zad16_ms.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
/*
policz srednice i najplytszy wierzachloek srednicy
od tego elementu znajdz wierzchoke, od ktorego jest rowno d/2 wysokosci
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node *bin_tree;
struct node
{
int val;
bin_tree left, right;
};
bin_tree newNode(int val)
{
bin_tree node = (bin_tree)malloc((sizeof(int) + 2 * sizeof(bin_tree)));
node->val = val;
return node;
}
int max(int a, int b)
{
if (a < b)
return b;
return a;
}
int longest(bin_tree t, int *best, bin_tree *shallowest)
{
if (!t)
return 0;
int l = longest(t->left, best, shallowest);
int p = longest(t->right, best, shallowest);
int longestPathBelow = l + p;
if (*best < longestPathBelow)
{
*best = longestPathBelow;
*shallowest = t;
}
// return longest path from this below + path to this
return max(l, p) + 1;
}
void find_diameter(bin_tree t, int *best, bin_tree *shallow)
{
int temp = longest(t, best, shallow);
}
int path_to_end(bin_tree t, int seeked, bin_tree *results)
{
if (!t)
return -1;
int l = path_to_end(t->left, seeked, results);
int p = path_to_end(t->right, seeked, results);
int m = max(l, p) + 1;
if (m == seeked)
{
*results = t;
}
return m;
}
bin_tree środek(bin_tree t)
{
int diameter = 0;
bin_tree shallow;
find_diameter(t, &diameter, &shallow);
bin_tree results;
int temp = path_to_end(shallow, (diameter + 1) / 2, &results);
return results;
}
int main()
{
bin_tree t2 = newNode(1);
t2->right = newNode(2);
t2->right->right = newNode(3);
t2->left = newNode(4);
t2->left->left = newNode(5);
t2->left->right = newNode(6);
t2->left->right->left = newNode(7);
t2->left->right->left->left = newNode(8);
t2->left->right->left->left->left = newNode(9);
t2->left->right->left->left->left->left = newNode(10);
t2->left->right->left->left->left->left->left = newNode(100);
t2->left->right->left->left->left->left->left->left = newNode(101);
t2->left->right->left->left->left->right = newNode(11);
t2->left->right->right = newNode(12);
t2->left->right->right->right = newNode(13);
t2->left->right->right->left = newNode(14);
t2->left->right->right->left->right = newNode(15);
t2->left->right->right->left->right->left = newNode(16);
bin_tree center = środek(t2);
printf("%d\n", center->val);
}