This is my repo for 0x1D. C - Binary trees
- Allowed editors:
vi
,vim
,emacs
- All your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89
- All your files should end with a new line
- A
README.md
file, at the root of the folder of the project, is mandatory - Your code should use the
Betty
style. It will be checked usingbetty-style.pl
andbetty-doc.pl
- You are not allowed to use global variables
- No more than 5 functions per file
- You are allowed to use the standard library
- In the following examples, the
main.c
files are shown as examples. You can use them to test your functions, but you don’t have to push them to your repo (if you do we won’t take them into account). We will use our ownmain.c
files at compilation. Ourmain.c
files might be different from the one shown in the examples - The prototypes of all your functions should be included in your header file called
binary_trees.h
- Don’t forget to push your header file
- All your header files should be include guarded
/**
* struct binary_tree_s - Binary tree node
*
* @n: Integer stored in the node
* @parent: Pointer to the parent node
* @left: Pointer to the left child node
* @right: Pointer to the right child node
*/
struct binary_tree_s
{
int n;
struct binary_tree_s *parent;
struct binary_tree_s *left;
struct binary_tree_s *right;
};
typedef struct binary_tree_s binary_tree_t;
typedef struct binary_tree_s bst_t;
typedef struct binary_tree_s avl_t;
typedef struct binary_tree_s heap_t;
- New node
Write a function that creates a binary tree node
- Prototype:
binary_tree_t *binary_tree_node(binary_tree_t *parent, int value);
- Where parent is a pointer to the parent node of the node to create And value is the value to put in the new node
- When created, a node does not have any child
- Your function must return a pointer to the new node, or NULL on failure
stevecmd/tmp/binary_trees$ cat 0-main.c
#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);
}
stevecmd/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 0-main.c 0-binary_tree_node.c -o 0-node
stevecmd/tmp/binary_trees$ ./0-node
.-------(098)-------.
.--(012)--. .--(402)--.
(006) (016) (256) (512)
File: 0-binary_tree_node.c
- Insert left
Write a function that inserts a node as the left-child of another node
- Prototype:
binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value);
- Where parent is a pointer to the node to insert the left-child in
- And value is the value to store in the new node
- Your function must return a pointer to the created node, or
NULL
on failure or if parent isNULL
- If parent already has a left-child, the new node must take its place, and the old left-child must be set as the left-child of the new node.
stevecmd/tmp/binary_trees$ cat 1-main.c
#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);
}
stevecmd/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 1-main.c 1-binary_tree_insert_left.c 0-binary_tree_node.c -o 1-left
stevecmd/tmp/binary_trees$ ./1-left
.--(098)--.
(012) (402)
.--(098)-------.
.--(054) .--(402)
(012) (128)
File: 1-binary_tree_insert_left.c
- Insert right
Write a function that inserts a node as the right-child of another node
- Prototype:
binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value);
- Where parent is a pointer to the node to insert the right-child in
- And value is the value to store in the new node
- Your function must return a pointer to the created node, or
NULL
on failure or if parent isNULL
- If parent already has a right-child, the new node must take its place, and the old right-child must be set as the right-child of the new node.
stevecmd/tmp/binary_trees$ cat 2-main.c
#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);
}
stevecmd/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 2-main.c 2-binary_tree_insert_right.c 0-binary_tree_node.c -o 2-right
stevecmd/tmp/binary_trees$ ./2-right
.--(098)--.
(012) (402)
.-------(098)--.
(012)--. (128)--.
(054) (402)
File: 2-binary_tree_insert_right.c
- Delete
Write a function that deletes an entire binary tree
- Prototype:
void binary_tree_delete(binary_tree_t *tree);
- Where tree is a pointer to the root node of the tree to delete
- If tree is
NULL
, do nothing
stevecmd/tmp/binary_trees$ cat 3-main.c
#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);
}
stevecmd/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 3-main.c 3-binary_tree_delete.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 3-del
stevecmd/tmp/binary_trees$ valgrind ./3-del
==13264== Memcheck, a memory error detector
==13264== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==13264== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==13264== Command: ./3-del
==13264==
.-------(098)--.
(012)--. (128)--.
(054) (402)
==13264==
==13264== HEAP SUMMARY:
==13264== in use at exit: 0 bytes in 0 blocks
==13264== total heap usage: 9 allocs, 9 frees, 949 bytes allocated
==13264==
==13264== All heap blocks were freed -- no leaks are possible
==13264==
==13264== For counts of detected and suppressed errors, rerun with: -v
==13264== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
File: 3-binary_tree_delete.c
- Is leaf
Write a function that checks if a node is a leaf
- Prototype:
int binary_tree_is_leaf(const binary_tree_t *node);
- Where node is a pointer to the node to check
- Your function must return 1 if node is a leaf, otherwise
0
- If node is
NULL
, return0
stevecmd/tmp/binary_trees$ cat 4-main.c
#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);
}
stevecmd/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 4-binary_tree_is_leaf.c 4-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 4-leaf
stevecmd/tmp/binary_trees$ ./4-leaf
.-------(098)--.
(012)--. (128)--.
(054) (402)
Is 98 a leaf: 0
Is 128 a leaf: 0
Is 402 a leaf: 1
File: 4-binary_tree_is_leaf.c
- Is root
Write a function that checks if a given node is a root
- Prototype:
int binary_tree_is_root(const binary_tree_t *node);
- Where node is a pointer to the node to check
- Your function must return 1 if node is a root, otherwise
0
- If node is
NULL
, return0
stevecmd/tmp/binary_trees$ cat 5-main.c
#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);
}
stevecmd/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 5-binary_tree_is_root.c 5-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 5-root
stevecmd/tmp/binary_trees$ ./5-root
.-------(098)--.
(012)--. (128)--.
(054) (402)
Is 98 a root: 1
Is 128 a root: 0
Is 402 a root: 0
File: 5-binary_tree_is_root.c