data_structures
bst_height.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* bst_height.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/18 14:31:04 by unite #+# #+# */
10 /* Updated: 2020/07/18 16:55:40 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "bst.h"
15 
16 static size_t bst_height_recur(const t_bst *bst, const t_bst_node *node)
17 {
18  size_t left_height;
19  size_t right_height;
20 
21  if (node == NULL)
22  return (0);
23  left_height = bst_height_recur(bst, node->left);
24  right_height = bst_height_recur(bst, node->right);
25  return (1 + (left_height > right_height ? left_height : right_height));
26 }
27 
28 size_t bst_height(const t_bst *bst)
29 {
30  return (bst_height_recur(bst, bst->root));
31 }
s_bst_node
A node in a binary search tree.
Definition: bst.h:36
bst_height
size_t bst_height(const t_bst *bst)
Returns the number of tiers in the tree.
Definition: bst_height.c:28
s_bst_node::right
struct s_bst_node * right
The right child.
Definition: bst.h:41
s_bst_node::left
struct s_bst_node * left
The left child.
Definition: bst.h:40
bst.h
s_bst
A binary search tree.
Definition: bst.h:57
s_bst::root
t_bst_node * root
The root of the tree.
Definition: bst.h:59