Skip to content

Commit 8fe09a4

Browse files
committed
more
0 parents  commit 8fe09a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1329
-0
lines changed

0-binary_tree_node.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_node - creates a binary tree node
5+
* @parent: pointer to the parent node of the node to create
6+
* @value: value to put in the new node
7+
*
8+
* Return: Pointer to the newly created node
9+
* NULL on failure
10+
*/
11+
binary_tree_t *binary_tree_node(binary_tree_t *parent, int value)
12+
{
13+
binary_tree_t *new;
14+
15+
new = malloc(sizeof(binary_tree_t));
16+
if (!new)
17+
return (NULL);
18+
new->n = value;
19+
new->parent = parent;
20+
new->left = NULL;
21+
new->right = NULL;
22+
return (new);
23+
}

1-binary_tree_insert_left.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_insert_left - inserts a node as the left-child of another node
5+
* @parent: pointer to the node to insert the left-child in
6+
* @value: value to store in the new node
7+
*
8+
* Return: Pointer to the newly created node
9+
* NULL on failure
10+
* NULL if parent is NULL
11+
*/
12+
binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value)
13+
{
14+
binary_tree_t *new;
15+
16+
if (!parent)
17+
return (NULL);
18+
19+
new = malloc(sizeof(binary_tree_t));
20+
if (!new)
21+
return (NULL);
22+
23+
new->n = value;
24+
new->parent = parent;
25+
new->right = NULL;
26+
new->left = parent->left;
27+
parent->left = new;
28+
if (new->left)
29+
new->left->parent = new;
30+
return (new);
31+
}

10-binary_tree_depth.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_depth - measures the depth of a node in a binary tree
5+
* @tree: pointer to node to measure the depth of
6+
*
7+
* Return: depth of the node
8+
* 0 if tree is NULL
9+
*/
10+
size_t binary_tree_depth(const binary_tree_t *tree)
11+
{
12+
size_t depth = 0;
13+
14+
if (!tree)
15+
return (0);
16+
17+
while (tree->parent)
18+
{
19+
depth++;
20+
tree = tree->parent;
21+
}
22+
23+
return (depth);
24+
}

100-binary_trees_ancestor.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_trees_ancestor - finds the lowest common ancestor of two nodes
5+
* @first: a pointer to the first node to find the ancestor
6+
* @second: a pointer to the second node to find the ancestor
7+
*
8+
* Return: pointer to the ancestor node
9+
* NULL if there is no ancestor node
10+
*/
11+
binary_tree_t *binary_trees_ancestor(const binary_tree_t *first,
12+
const binary_tree_t *second)
13+
{
14+
size_t depth_first, depth_second;
15+
16+
if (!first || !second)
17+
return (NULL);
18+
19+
depth_first = binary_tree_depth(first);
20+
depth_second = binary_tree_depth(second);
21+
22+
while (depth_first > depth_second)
23+
{
24+
first = first->parent;
25+
depth_first--;
26+
}
27+
while (depth_second > depth_first)
28+
{
29+
second = second->parent;
30+
depth_second--;
31+
}
32+
while (first && second)
33+
{
34+
if (first == second)
35+
return ((binary_tree_t *)first);
36+
first = first->parent;
37+
second = second->parent;
38+
}
39+
return ((binary_tree_t *)first);
40+
}

101-binary_tree_levelorder.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_levelorder - traverse a binary tree using level-order traverse
5+
* @tree: tree to traverse
6+
* @func: pointer to a function to call for each node
7+
*/
8+
void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int))
9+
{
10+
size_t level, maxlevel;
11+
12+
if (!tree || !func)
13+
return;
14+
15+
maxlevel = binary_tree_height(tree) + 1;
16+
17+
for (level = 1; level <= maxlevel; level++)
18+
btlo_helper(tree, func, level);
19+
}
20+
21+
/**
22+
* btlo_helper - goes through a binary tree using post-order traverse
23+
* @tree: tree to traverse
24+
* @func: pointer to a function to call for each node
25+
* @level: the level of the tree to call func upon
26+
*/
27+
void btlo_helper(const binary_tree_t *tree, void (*func)(int), size_t level)
28+
{
29+
if (level == 1)
30+
func(tree->n);
31+
else
32+
{
33+
btlo_helper(tree->left, func, level - 1);
34+
btlo_helper(tree->right, func, level - 1);
35+
}
36+
}

102-binary_tree_is_complete.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_is_complete - checks if a binary tree is complete
5+
* @tree: a pointer to the root node of the tree to check
6+
*
7+
* Return: 1 if the tree is complete
8+
* 0 if the tree is not complete
9+
* 0 if tree is NULL
10+
*/
11+
int binary_tree_is_complete(const binary_tree_t *tree)
12+
{
13+
size_t size;
14+
15+
if (!tree)
16+
return (0);
17+
size = binary_tree_size(tree);
18+
19+
return (btic_helper(tree, 0, size));
20+
}
21+
22+
/**
23+
* btic_helper - checks if a binary tree is complete
24+
* @tree: a pointer to the root node of the tree to check
25+
* @index: node index to check
26+
* @size: number of nodes in the tree
27+
*
28+
* Return: 1 if the tree is complete
29+
* 0 if the tree is not complete
30+
* 0 if tree is NULL
31+
*/
32+
int btic_helper(const binary_tree_t *tree, size_t index, size_t size)
33+
{
34+
if (!tree)
35+
return (1);
36+
37+
if (index >= size)
38+
return (0);
39+
40+
return (btic_helper(tree->left, 2 * index + 1, size) &&
41+
btic_helper(tree->right, 2 * index + 2, size));
42+
}

103-binary_tree_rotate_left.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_rotate_left - performs a left-rotation on a binary tree
5+
* @tree: a pointer to the root node of the tree to check
6+
*
7+
* Return: Pointer to the new root node of the tree once rotated
8+
* NULL on failure
9+
*/
10+
binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree)
11+
{
12+
binary_tree_t *tmp = NULL, *parent;
13+
14+
if (!tree || !tree->right)
15+
return (NULL);
16+
tmp = tree;
17+
parent = tree->parent;
18+
tree = tree->right;
19+
tree->parent = NULL;
20+
if (tree->left)
21+
{
22+
tmp->right = tree->left;
23+
tree->left->parent = tmp;
24+
}
25+
else
26+
tmp->right = NULL;
27+
tmp->parent = tree;
28+
tree->left = tmp;
29+
if (parent)
30+
parent->right = tree;
31+
tree->parent = parent;
32+
return (tree);
33+
}

104-binary_tree_rotate_right.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_rotate_right - performs a right-rotation on a binary tree
5+
* @tree: a pointer to the root node of the tree to check
6+
*
7+
* Return: Pointer to the new root node of the tree once rotated
8+
* NULL on failure
9+
*/
10+
binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree)
11+
{
12+
binary_tree_t *tmp = NULL, *parent;
13+
14+
if (!tree || !tree->left)
15+
return (NULL);
16+
tmp = tree;
17+
parent = tree->parent;
18+
tree = tree->left;
19+
tree->parent = NULL;
20+
if (tree->right)
21+
{
22+
tmp->left = tree->right;
23+
tree->right->parent = tmp;
24+
}
25+
else
26+
tmp->left = NULL;
27+
tmp->parent = tree;
28+
tree->right = tmp;
29+
if (parent)
30+
parent->left = tree;
31+
tree->parent = parent;
32+
return (tree);
33+
}

11-binary_tree_size.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_size - measures the size of a binary tree
5+
* @tree: pointer to root node of tree to measure
6+
*
7+
* Return: size of the tree
8+
* 0 if tree is NULL
9+
*/
10+
size_t binary_tree_size(const binary_tree_t *tree)
11+
{
12+
if (!tree)
13+
return (0);
14+
15+
return (binary_tree_size(tree->left) + binary_tree_size(tree->right) + 1);
16+
}

110-binary_tree_is_bst.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* binary_tree_is_bst - checks if a binary tree is a valid Binary Search Tree
5+
* @tree: a pointer to the root node of the tree to check
6+
*
7+
* Return: 1 if tree is a valid BST
8+
* 0 otherwise
9+
*/
10+
int binary_tree_is_bst(const binary_tree_t *tree)
11+
{
12+
if (!tree)
13+
return (0);
14+
return (btib_helper(tree, INT_MIN, INT_MAX));
15+
}
16+
17+
/**
18+
* btib_helper - checks if a binary tree is a valid Binary Search Tree
19+
* @tree: a pointer to the root node of the tree to check
20+
* @min: Lower bound of checked nored
21+
* @max: Upper bound of checked nodes
22+
*
23+
* Return: 1 if tree is a valid BST
24+
* 0 otherwise
25+
*/
26+
int btib_helper(const binary_tree_t *tree, int min, int max)
27+
{
28+
if (!tree)
29+
return (1);
30+
31+
if (tree->n < min || tree->n > max)
32+
return (0);
33+
34+
return (btib_helper(tree->left, min, tree->n - 1) &&
35+
btib_helper(tree->right, tree->n + 1, max));
36+
}

111-bst_insert.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* bst_insert - inserts a value in a Binary Search Tree
5+
* @tree: a double pointer to the root node of the BST to insert the value
6+
* @value: the value to store in the node to be inserted
7+
* Return: A pointer to the created node
8+
* NULL on failure
9+
*/
10+
bst_t *bst_insert(bst_t **tree, int value)
11+
{
12+
bst_t *new = NULL;
13+
bst_t *second = NULL;
14+
bst_t *tmp = NULL;
15+
16+
if (!tree)
17+
return (NULL);
18+
if (*tree == NULL)
19+
return (*tree = binary_tree_node(NULL, value));
20+
21+
tmp = *tree;
22+
while (tmp)
23+
{
24+
second = tmp;
25+
if (value < tmp->n)
26+
tmp = tmp->left;
27+
else if (value > tmp->n)
28+
tmp = tmp->right;
29+
else if (value == tmp->n)
30+
return (NULL);
31+
}
32+
33+
new = binary_tree_node(NULL, value);
34+
if (second == NULL)
35+
second = new;
36+
else if (value < second->n)
37+
{
38+
second->left = new;
39+
new->parent = second;
40+
}
41+
else
42+
{
43+
second->right = new;
44+
new->parent = second;
45+
}
46+
47+
return (new);
48+
}

112-array_to_bst.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "binary_trees.h"
2+
3+
/**
4+
* array_to_bst - builds a Binary Search Tree from an array
5+
* @array: pointer to the first element of the array
6+
* @size: the number of elements in the array
7+
* Return: A pointer to the root node of the created BST
8+
* NULL on failure
9+
*/
10+
bst_t *array_to_bst(int *array, size_t size)
11+
{
12+
unsigned int i;
13+
bst_t *root = NULL;
14+
15+
for (i = 0; i < size; i++)
16+
bst_insert(&root, array[i]);
17+
18+
return (root);
19+
}

0 commit comments

Comments
 (0)