-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path120-binary_tree_is_avl.c
More file actions
81 lines (69 loc) · 1.5 KB
/
120-binary_tree_is_avl.c
File metadata and controls
81 lines (69 loc) · 1.5 KB
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
#include "binary_trees.h"
/**
* btl - auxiliary level
*
* @tree: pointer to root
* Return: integer with level
*/
int btl(const binary_tree_t *tree)
{
int h_left, h_right;
if (tree == NULL)
return (0);
if (tree->left == NULL && tree->right == NULL)
return (1);
h_left = btl(tree->left);
h_right = btl(tree->right);
if (h_left > h_right)
return (h_left + 1);
else
return (h_right + 1);
}
/**
* bt_balance - factor calculate
*
* @tree: pointer to root
* Return: integer with factor
*/
int bt_balance(const binary_tree_t *tree)
{
if (tree == NULL)
return (0);
return (btl(tree->left) - btl(tree->right));
}
/**
* bavl - Check level
*
* @tree: pointer to root
* @min: min value
* @max: max value
* Return: 1 if is AVL, 0 otherwise
*/
int bavl(const binary_tree_t *tree, int min, int max)
{
int balance_left, balance_right, balance;
if (tree == NULL)
return (1);
if (tree->n > max || tree->n < min)
return (0);
balance = bt_balance(tree);
if (balance < -1 || balance > 1)
return (0);
balance_left = bavl(tree->left, min, tree->n - 1);
balance_right = bavl(tree->right, tree->n + 1, max);
if (balance_left == 1 && balance_right == 1)
return (1);
return (0);
}
/**
* binary_tree_is_avl - checks if a binary tree is a valid AVL Tree
* @tree: pointer to the root node of the tree to check
*
* Return: 1 if tree is a valid AVL Tree, and 0 otherwise
*/
int binary_tree_is_avl(const binary_tree_t *tree)
{
if (tree == NULL)
return (0);
return (bavl(tree, INT_MIN, INT_MAX));
}