blob: 6d99e9946b268dd2af00ab83adbd52ea61a3893e (
plain)
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
|
#include "binary_tree.h"
#include <stddef.h>
#include <stdio.h>
int size(const struct binary_tree *tree)
{
if (tree == NULL)
return 0;
return 1 + size(tree->left) + size(tree->right);
}
static int max(int a, int b)
{
if (a > b)
return a;
return b;
}
int height(const struct binary_tree *tree)
{
if (tree == NULL)
{
return -1;
}
return 1 + max(height(tree->left), height(tree->right));
}
int is_perfect(const struct binary_tree *tree)
{
if (tree == NULL)
return 1;
return height(tree->left) == height(tree->right) && is_perfect(tree->right)
&& is_perfect(tree->right);
}
int is_complete(const struct binary_tree *tree)
{
if (tree == NULL)
{
return 1;
}
int hg = height(tree->left);
int hd = height(tree->right);
if (hg - hd != 0 && hg - hd != 1)
{
return 0;
}
return is_complete(tree->left) && is_complete(tree->right);
}
int is_degenerate(const struct binary_tree *tree)
{
if (tree == NULL)
{
return 1;
}
if (tree->left && tree->right)
{
return 0;
}
return is_degenerate(tree->left) && is_degenerate(tree->right);
}
int is_full(const struct binary_tree *tree)
{
if (tree == NULL)
return 1;
if ((tree->left && !tree->right) || (!tree->left && tree->right))
return 0;
return is_full(tree->right) && is_full(tree->left);
}
static int is_bzt(const struct binary_tree *tree, int min, int max)
{
if (tree == NULL)
return 1;
if (tree->data > max || tree->data <= min)
return 0;
return is_bzt(tree->left, min, tree->data)
&& is_bzt(tree->right, tree->data, max);
}
int is_bst(const struct binary_tree *tree)
{
if (tree == NULL)
return 1;
return is_bzt(tree, -2147483647, 2147483647);
}
|