-
Notifications
You must be signed in to change notification settings - Fork 0
/
120-binary_tree_is_avl.c
65 lines (55 loc) · 1.47 KB
/
120-binary_tree_is_avl.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
#include "binary_trees.h"
/**
* binary_tree_is_avl - finds if a binary tree is an avl
* @tree: pointer to the root node of the tree
*
* Return: 1 if tree is avl
* 0 otherwise
*/
int binary_tree_is_avl(const binary_tree_t *tree)
{
if (!tree)
return (0);
return (btia_helper(tree, INT_MIN, INT_MAX));
}
/**
* btia_helper - helper that finds if a binary tree is an avl
* @tree: pointer to the root node of the tree
* @min: minimum value
* @max: maximum value
*
* Return: 1 if tree is avl
* 0 otherwise
*/
int btia_helper(const binary_tree_t *tree, int min, int max)
{
int path_l, path_r;
if (!tree)
return (1);
if (tree->n < min || tree->n > max)
return (0);
path_l = tree->left ? 1 + binary_tree_height(tree->left) : 0;
path_r = tree->right ? 1 + binary_tree_height(tree->right) : 0;
if (abs(path_l - path_r) > 1)
return (0);
return (btia_helper(tree->left, min, tree->n - 1) &&
btia_helper(tree->right, tree->n + 1, max));
/* This is part of the BST check logic */
}
/**
* binary_tree_height - measures the height of a binary tree
* @tree: tree to measure the height of
*
* Return: height of the tree
* 0 if tree is NULL
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t height_l = 0;
size_t height_r = 0;
if (!tree)
return (0);
height_l = tree->left ? 1 + binary_tree_height(tree->left) : 0;
height_r = tree->right ? 1 + binary_tree_height(tree->right) : 0;
return (height_l > height_r ? height_l : height_r);
}