-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
function that check if the ree is balanced or not
- Loading branch information
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |