Skip to content

Commit

Permalink
Added tasks 1 to 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Kook16 committed Aug 30, 2023
1 parent 874385c commit e1d51f4
Show file tree
Hide file tree
Showing 35 changed files with 710 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 0-main.c
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);
}
31 changes: 31 additions & 0 deletions 1-binary_tree_insert_left.c
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);
}
Binary file added 1-left
Binary file not shown.
23 changes: 23 additions & 0 deletions 1-main.c
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);
}
27 changes: 27 additions & 0 deletions 10-binary_tree_depth.c
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);
}
Binary file added 10-depth
Binary file not shown.
29 changes: 29 additions & 0 deletions 10-main.c
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);
}
31 changes: 31 additions & 0 deletions 2-binary_tree_insert_right.c
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);
}
23 changes: 23 additions & 0 deletions 2-main.c
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);
}
Binary file added 2-right
Binary file not shown.
18 changes: 18 additions & 0 deletions 3-binary_tree_delete.c
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);
}
18 changes: 18 additions & 0 deletions 3-binary_tree_delete.c~
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);
}
Binary file added 3-del
Binary file not shown.
22 changes: 22 additions & 0 deletions 3-main.c
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);
}
20 changes: 20 additions & 0 deletions 4-binary_tree_is_leaf.c
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);
}
Binary file added 4-leaf
Binary file not shown.
29 changes: 29 additions & 0 deletions 4-main.c
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);
}
20 changes: 20 additions & 0 deletions 5-binary_tree_is_root.c
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);
}
20 changes: 20 additions & 0 deletions 5-binary_tree_is_root.c~
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);
}
29 changes: 29 additions & 0 deletions 5-main.c
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);
}
Binary file added 5-root
Binary file not shown.
Loading

0 comments on commit e1d51f4

Please sign in to comment.