Skip to content

Commit

Permalink
function that check if the ree is balanced or not
Browse files Browse the repository at this point in the history
  • Loading branch information
whalay committed Mar 29, 2023
1 parent 225381a commit 8078f89
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 10-binary_tree_depth.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "binary_trees.h"

/**
* binary_tree_depth - Measures the depth of a node in a binary tree.
* @tree: A pointer to the node to measure the depth.
*
* Return: If tree is NULL, your function must return 0, else return the depth.
*/
size_t binary_tree_depth(const binary_tree_t *tree)
{
return ((tree && tree->parent) ? 1 + binary_tree_depth(tree->parent) : 0);
}
20 changes: 20 additions & 0 deletions 11-binary_tree_size.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "binary_trees.h"

/**
* binary_tree_size - Measures the size of a binary tree.
* @tree: A pointer to the root node of the tree to measure the size of.
*
* Return: The size of the tree.
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
size_t size = 0;

if (tree)
{
size += 1;
size += binary_tree_size(tree->left);
size += binary_tree_size(tree->right);
}
return (size);
}
20 changes: 20 additions & 0 deletions 12-binary_tree_leaves.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "binary_trees.h"

/**
* binary_tree_leaves - Counts the leaves in a binary tree.
* @tree: A pointer to the root node of the tree to count the leaves of.
*
* Return: The number of leaves in the tree.
*/
size_t binary_tree_leaves(const binary_tree_t *tree)
{
size_t leaves = 0;

if (tree)
{
leaves += (!tree->left && !tree->right) ? 1 : 0;
leaves += binary_tree_leaves(tree->left);
leaves += binary_tree_leaves(tree->right);
}
return (leaves);
}
20 changes: 20 additions & 0 deletions 13-binary_tree_nodes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "binary_trees.h"

/**
* binary_tree_nodes - Counts the nodes with at least 1 child in a binary tree.
* @tree: A pointer to the root node of the tree to count the number of nodes.
*
* Return: If tree is NULL, the function must return 0, else return node count.
*/
size_t binary_tree_nodes(const binary_tree_t *tree)
{
size_t nodes = 0;

if (tree)
{
nodes += (tree->left || tree->right) ? 1 : 0;
nodes += binary_tree_nodes(tree->left);
nodes += binary_tree_nodes(tree->right);
}
return (nodes);
}
34 changes: 34 additions & 0 deletions 14-binary_tree_balance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "binary_trees.h"

/**
* binary_tree_balance - Measures the balance factor of a binary tree.
* @tree: A pointer to the root node of the tree to measure the balance factor.
*
* Return: If tree is NULL, return 0, else return balance factor.
*/
int binary_tree_balance(const binary_tree_t *tree)
{
if (tree)
return (binary_tree_height(tree->left) - binary_tree_height(tree->right));

return (0);
}

/**
* binary_tree_height - Measures the height of a binary tree.
* @tree: A pointer to the root node of the tree to measure the height.
*
* Return: If tree is NULL, your function must return 0, else return height.
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
if (tree)
{
size_t l = 0, r = 0;

l = tree->left ? 1 + binary_tree_height(tree->left) : 1;
r = tree->right ? 1 + binary_tree_height(tree->right) : 1;
return ((l > r) ? l : r);
}
return (0);
}
20 changes: 20 additions & 0 deletions 9-binary_tree_height.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "binary_trees.h"

/**
* binary_tree_height - Measures the height of a binary tree.
* @tree: A pointer to the root node of the tree to measure the height.
*
* Return: If tree is NULL, your function must return 0, else return height.
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
if (tree)
{
size_t l = 0, r = 0;

l = tree->left ? 1 + binary_tree_height(tree->left) : 0;
r = tree->right ? 1 + binary_tree_height(tree->right) : 0;
return ((l > r) ? l : r);
}
return (0);
}

0 comments on commit 8078f89

Please sign in to comment.