-
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.
- Loading branch information
Showing
35 changed files
with
710 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,25 @@ | ||
#include <stdlib.h> | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* main - Entry point | ||
* | ||
* Return: Always 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
binary_tree_t *root; | ||
|
||
root = binary_tree_node(NULL, 98); | ||
|
||
root->left = binary_tree_node(root, 12); | ||
root->left->left = binary_tree_node(root->left, 6); | ||
root->left->right = binary_tree_node(root->left, 16); | ||
|
||
root->right = binary_tree_node(root, 402); | ||
root->right->left = binary_tree_node(root->right, 256); | ||
root->right->right = binary_tree_node(root->right, 512); | ||
|
||
binary_tree_print(root); | ||
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,31 @@ | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* binary_tree_insert_left - inserts a node as the left-child of another node | ||
* @parent: A pointer to the node to insert the left-child in | ||
* @value: Value to store in the new node | ||
* Return: A pointer to the created node | ||
*/ | ||
|
||
binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value) | ||
{ | ||
binary_tree_t *node; | ||
|
||
if (parent == NULL) | ||
return (NULL); | ||
|
||
node = binary_tree_node(parent, value); | ||
|
||
if (node == NULL) | ||
return (NULL); | ||
|
||
if (parent->left) | ||
{ | ||
node->left = parent->left; | ||
parent->left->parent = node; | ||
} | ||
|
||
parent->left = node; | ||
|
||
return (node); | ||
} |
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,23 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* main - Entry point | ||
* | ||
* Return: Always 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
binary_tree_t *root; | ||
|
||
root = binary_tree_node(NULL, 98); | ||
root->left = binary_tree_node(root, 12); | ||
root->right = binary_tree_node(root, 402); | ||
binary_tree_print(root); | ||
printf("\n"); | ||
binary_tree_insert_left(root->right, 128); | ||
binary_tree_insert_left(root, 54); | ||
binary_tree_print(root); | ||
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,27 @@ | ||
#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: size_t value indicating the depth of the node | ||
*/ | ||
|
||
|
||
size_t binary_tree_depth(const binary_tree_t *tree) | ||
{ | ||
size_t depth; | ||
const binary_tree_t *current; | ||
|
||
if (tree == NULL) | ||
return (0); | ||
|
||
current = tree; | ||
depth = 0; | ||
|
||
while (current->parent != NULL) | ||
{ | ||
depth++; | ||
current = current->parent; | ||
} | ||
return (depth); | ||
} |
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,29 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* main - Entry point | ||
* | ||
* Return: Always 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
binary_tree_t *root; | ||
size_t depth; | ||
|
||
root = binary_tree_node(NULL, 98); | ||
root->left = binary_tree_node(root, 12); | ||
root->right = binary_tree_node(root, 402); | ||
binary_tree_insert_right(root->left, 54); | ||
binary_tree_insert_right(root, 128); | ||
binary_tree_print(root); | ||
|
||
depth = binary_tree_depth(root); | ||
printf("Depth of %d: %lu\n", root->n, depth); | ||
depth = binary_tree_depth(root->right); | ||
printf("Depth of %d: %lu\n", root->right->n, depth); | ||
depth = binary_tree_depth(root->left->right); | ||
printf("Depth of %d: %lu\n", root->left->right->n, depth); | ||
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,31 @@ | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* binary_tree_insert_right - inserts a node as the right-child of another node | ||
* @parent: A pointer to the node to insert the left-child in | ||
* @value: Value to store in the new node | ||
* Return: A pointer to the created node | ||
*/ | ||
|
||
binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value) | ||
{ | ||
binary_tree_t *node; | ||
|
||
if (parent == NULL) | ||
return (NULL); | ||
|
||
node = binary_tree_node(parent, value); | ||
|
||
if (node == NULL) | ||
return (NULL); | ||
|
||
if (parent->right) | ||
{ | ||
node->right = parent->right; | ||
parent->right->parent = node; | ||
} | ||
|
||
parent->right = node; | ||
|
||
return (node); | ||
} |
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,23 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* main - Entry point | ||
* | ||
* Return: Always 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
binary_tree_t *root; | ||
|
||
root = binary_tree_node(NULL, 98); | ||
root->left = binary_tree_node(root, 12); | ||
root->right = binary_tree_node(root, 402); | ||
binary_tree_print(root); | ||
printf("\n"); | ||
binary_tree_insert_right(root->left, 54); | ||
binary_tree_insert_right(root, 128); | ||
binary_tree_print(root); | ||
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,18 @@ | ||
#include "binary_trees.h" | ||
|
||
|
||
/** | ||
* binary_tree_delete - deletes an entire binary tree | ||
* @tree: Pointer to the root node of the tree to delete | ||
*/ | ||
|
||
void binary_tree_delete(binary_tree_t *tree) | ||
{ | ||
if (tree == NULL) | ||
return; | ||
|
||
binary_tree_delete(tree->left); | ||
binary_tree_delete(tree->right); | ||
|
||
free(tree); | ||
} |
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,18 @@ | ||
#include "binary_trees.h" | ||
|
||
|
||
/** | ||
* binary_trees_delete - deletes an entire binary tree | ||
* @tree: Pointer to the root node of the tree to delete | ||
*/ | ||
|
||
void binary_tree_delete(binary_tree_t *tree) | ||
{ | ||
if (tree == NULL) | ||
return; | ||
|
||
binary_tree_delete(tree->left); | ||
binary_tree_delete(tree->right); | ||
|
||
free(tree); | ||
} |
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,22 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* main - Entry point | ||
* | ||
* Return: Always 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
binary_tree_t *root; | ||
|
||
root = binary_tree_node(NULL, 98); | ||
root->left = binary_tree_node(root, 12); | ||
root->right = binary_tree_node(root, 402); | ||
binary_tree_insert_right(root->left, 54); | ||
binary_tree_insert_right(root, 128); | ||
binary_tree_print(root); | ||
binary_tree_delete(root); | ||
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_is_leaf - checks if a node is a leaf | ||
* @node: A pointer to the node to check | ||
* Return: 1 if node is a leaf otherwise 0 | ||
*/ | ||
|
||
|
||
int binary_tree_is_leaf(const binary_tree_t *node) | ||
{ | ||
if (node == NULL) | ||
return (0); | ||
|
||
if (node->left == NULL && node->right == NULL) | ||
return (1); | ||
|
||
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,29 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* main - Entry point | ||
* | ||
* Return: Always 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
binary_tree_t *root; | ||
int ret; | ||
|
||
root = binary_tree_node(NULL, 98); | ||
root->left = binary_tree_node(root, 12); | ||
root->right = binary_tree_node(root, 402); | ||
binary_tree_insert_right(root->left, 54); | ||
binary_tree_insert_right(root, 128); | ||
binary_tree_print(root); | ||
|
||
ret = binary_tree_is_leaf(root); | ||
printf("Is %d a leaf: %d\n", root->n, ret); | ||
ret = binary_tree_is_leaf(root->right); | ||
printf("Is %d a leaf: %d\n", root->right->n, ret); | ||
ret = binary_tree_is_leaf(root->right->right); | ||
printf("Is %d a leaf: %d\n", root->right->right->n, ret); | ||
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_is_root - check if a given node is a root | ||
* @node: Pointer to the node to check | ||
* Return: 1 if node is root, otherwise 0 | ||
*/ | ||
|
||
|
||
int binary_tree_is_root(const binary_tree_t *node) | ||
{ | ||
if (node == NULL) | ||
return (0); | ||
|
||
if (node->parent == NULL) | ||
return (1); | ||
|
||
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" | ||
|
||
|
||
/** | ||
* bnary_tree_is_root - check if a given node is a root | ||
* @node: Pointer to the node to check | ||
* Return: 1 if node is root, otherwise 0 | ||
*/ | ||
|
||
|
||
int binary_tree_is_root(const binary_tree_t *node) | ||
{ | ||
if (node == NULL) | ||
return (0); | ||
|
||
if (node->parent == NULL) | ||
return (1); | ||
|
||
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,29 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "binary_trees.h" | ||
|
||
/** | ||
* main - Entry point | ||
* | ||
* Return: Always 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
binary_tree_t *root; | ||
int ret; | ||
|
||
root = binary_tree_node(NULL, 98); | ||
root->left = binary_tree_node(root, 12); | ||
root->right = binary_tree_node(root, 402); | ||
binary_tree_insert_right(root->left, 54); | ||
binary_tree_insert_right(root, 128); | ||
binary_tree_print(root); | ||
|
||
ret = binary_tree_is_root(root); | ||
printf("Is %d a root: %d\n", root->n, ret); | ||
ret = binary_tree_is_root(root->right); | ||
printf("Is %d a root: %d\n", root->right->n, ret); | ||
ret = binary_tree_is_root(root->right->right); | ||
printf("Is %d a root: %d\n", root->right->right->n, ret); | ||
return (0); | ||
} |
Oops, something went wrong.