Skip to content

Commit

Permalink
Merge branch 'branches/dev' into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
nakira974 committed Mar 15, 2024
2 parents 1d3febf + ac5a87a commit 337136e
Show file tree
Hide file tree
Showing 26 changed files with 966 additions and 430 deletions.
23 changes: 20 additions & 3 deletions headers/array.h → headers/arrays_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file array.h
* @brief This file contains the API for arrays
* @file arrays_utils.h
* @brief This file contains the API for arrays utility methods
* @author Maxime Loukhal
* @date 09/03/2024
*/
Expand All @@ -26,6 +26,7 @@ extern "C" {
*/
void array_split(void ***out, int *out_size, void *in, int start_index, int stop_index);


/**
* @brief Convert the given array into a list
* @param array Array to be converted to list
Expand All @@ -52,8 +53,24 @@ CLinkedList *array_toCList(void **array);
* @param array Array to be converted to set
* @return Converted array to set
*/
struct Set* array_toSet(void **array, bool(*equals) (const void* value1, const void * value2));
struct Set *array_toSet(void **array, bool(*equals)(const void *value1, const void *value2));

#ifdef __cpluscplus
/**
* @brief Inline function that evaluates the size of an array
* @param array Array to determine the size
*/
static inline size_t array_length(void** array){
return sizeof(array) / sizeof(array[0]);
}
#else
/**
* @brief Macro that evaluates the size of an array
* @param array Array to determine the size
*/
#define array_length(array) sizeof(array) / sizeof(array[0])

#endif
#ifdef __cplusplus
}
#endif
Expand Down
156 changes: 91 additions & 65 deletions headers/bitree.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,75 +16,79 @@ extern "C" {
#include <cstdlib>
#include <cstdbool>
#else

#include <stdlib.h>
#include <stdbool.h>

#endif

/**
* @brief Data structure definition for a binary tree node
*/
typedef struct BinaryTreeNode{
typedef struct BinaryTreeNode {
/**
* @brief Node's value
*/
void *value;
/**
* @brief Node's left child
*/
struct BinaryTreeNode* left;
struct BinaryTreeNode *left;
/**
* @brief Node's right child
*/
struct BinaryTreeNode* right;
struct BinaryTreeNode *right;
} BinaryTreeNode;

/**
* @brief Data structure definition for a binary tree
*/
typedef struct BinaryTree{
/**
* @brief Binary tree current size
*/
int size;
/**
* @brief User equals handle for stored values
* @param value1 Value 1 to be compared
* @param value2 Value 2 to be compared
* @return true if values are equal, false otherwise
*/
bool (*equals) (const void* value1, const void *value2);
/**
* @brief User destroy method to clean node's value
* @param value Value to be removed from the tree
*/
void (*destroy)(void* value);
/**
* @brief Binary tree current root node
*/
BinaryTreeNode* root;
}BinaryTree;

/**
* @brief Creates a given binary tree with default values
* @param tree Tree to be created
* @param destroy Destroy user handle
*/
void bitree_create(BinaryTree *tree, void(*destroy)(void* value));

/**
* @brief Destroys and clean memory of a given binary tree
* @param tree Binary tree to be destroyed
*/
void bitree_destroy(BinaryTree *tree);

/**
* @brief Try to create a new left child node of the given node in the given tree
* @param tree Tree to add a left node
* @param node Node to add a left child
* @param value Value to be added
* @return true if the left node was created, false otherwise
*/
bool bitree_addLeft(BinaryTree *tree, BinaryTreeNode *node, const void *value);
typedef struct BinaryTree {
/**
* @brief Binary tree current size
*/
int size;
/**
* @brief User equals handle for stored values
* @param value1 Value 1 to be compared
* @param value2 Value 2 to be compared
* @return true if values are equal, false otherwise
*/
bool (*equals)(const void *value1, const void *value2);

/**
* @brief User destroy method to clean node's value
* @param value Value to be removed from the tree
*/
void (*destroy)(void *value);

/**
* @brief Binary tree current root node
*/
BinaryTreeNode *root;
} BinaryTree;

/**
* @brief Creates a given binary tree with default values
* @param tree Tree to be created
* @param destroy Destroy user handle
*/
void bitree_create(BinaryTree *tree, void(*destroy)(void *value));

/**
* @brief Destroys and clean memory of a given binary tree
* @param tree Binary tree to be destroyed
*/
void bitree_destroy(BinaryTree *tree);

/**
* @brief Try to create a new left child node of the given node in the given tree
* @param tree Tree to add a left node
* @param node Node to add a left child
* @param value Value to be added
* @return true if the left node was created, false otherwise
*/
bool bitree_addLeft(BinaryTree *tree, BinaryTreeNode *node, const void *value);

/**
* @brief Try to create a new right child node of the given node in the given tree
Expand Down Expand Up @@ -123,14 +127,14 @@ bool bitree_merge(BinaryTree *out, BinaryTree *left, BinaryTree *right, const vo
* @param tree Binary tree to get the maximum depth
* @return The maximum depth of the given binary tree
*/
int bitree_maxDepth(BinaryTree * tree);
int bitree_maxDepth(BinaryTree *tree);

/**
* @brief Returns the current maximum depth of the given binary tree
* @param tree Binary tree to get the maximum depth
* @return The maximum depth of the given binary tree
*/
int bitree_maxDepthBranch(BinaryTreeNode * branchRoot);
int bitree_maxDepthBranch(BinaryTreeNode *branchRoot);

/**
* @brief Determines if two nodes are in the same binary tree or not
Expand All @@ -139,29 +143,31 @@ int bitree_maxDepthBranch(BinaryTreeNode * branchRoot);
* @param right Right node to be compared
* @return true if two nodes are in the same tree, false otherwise
*/
bool bitree_isSameTree(bool (*equals)(const void *value1, const void* value2), BinaryTreeNode* left, BinaryTreeNode* right);
bool
bitree_isSameTree(bool (*equals)(const void *value1, const void *value2), BinaryTreeNode *left, BinaryTreeNode *right);

/**
* @brief Invert the given binary tree
* @param out Inverted binary tree
* @param tree Binary tree to invert
*/
bool bitree_invert(BinaryTree *out, BinaryTree * tree);
bool bitree_invert(BinaryTree *out, BinaryTree *tree);

/**
* @brief Invert the current branch starting from the given node
* @param branchRoot Relative branchRoot where to start to invert the binary tree
* @return The reversed branch
*/
BinaryTreeNode * bitree_invertBranch(BinaryTreeNode* branchRoot);
BinaryTreeNode *bitree_invertBranch(BinaryTreeNode *branchRoot);
/**
* @brief Determine if two nodes are symmetric or not
* @param equals Nodes value compare function, usefully used to add some other nodes comparer
* @param left Left node to be compared
* @param right Right node to be compared
* @return true if given nodes are symmetric, false otherwise
*/
bool bitree_isMirror(bool (*equals)(const void *value1, const void* value2), BinaryTreeNode* left, BinaryTreeNode* right);
bool
bitree_isMirror(bool (*equals)(const void *value1, const void *value2), BinaryTreeNode *left, BinaryTreeNode *right);

/**
* @brief Returns a binary tree by level
Expand All @@ -183,7 +189,9 @@ void **bitree_levelOrder(BinaryTree *tree, int *returnSize, int **returnColumnSi
*
* @return A pointer to the root node of the constructed binary tree.
*/
BinaryTreeNode * bitree_build_from_preorder_inorder_branch(void** preorder, int preorder_size, void** inorder, int inorder_size, bool (*equals)(const void *value1, const void* value2));
BinaryTreeNode *
bitree_build_from_preorder_inorder_branch(void **preorder, int preorder_size, void **inorder, int inorder_size,
bool (*equals)(const void *value1, const void *value2));

/**
* @brief Construct a binary tree from inorder and postorder traversal.
Expand All @@ -195,8 +203,10 @@ BinaryTreeNode * bitree_build_from_preorder_inorder_branch(void** preorder, int
* @param equals Node value equals function
* @return A pointer to the root node of the constructed binary tree.
*/
BinaryTreeNode* bitree_build_from_inorder_postorder_branch(void** inorder, int inorderSize, int** postorder,
int postorderSize, bool (*equals)(const void *value1, const void* value2));
BinaryTreeNode *bitree_build_from_inorder_postorder_branch(void **inorder, int inorderSize, int **postorder,
int postorderSize,
bool (*equals)(const void *value1, const void *value2));

/**
* @brief Construct a binary tree from preorder and inorder traversal.
*
Expand All @@ -209,7 +219,9 @@ BinaryTreeNode* bitree_build_from_inorder_postorder_branch(void** inorder, int i
*
* @return A pointer to the constructed binary tree.
*/
BinaryTree * bitree_build_from_preorder_inorder(void** preorder, int preorder_size, void** inorder, int inorder_size, void(*destroy)(void* value), bool (*equals)(const void *value1, const void* value2));
BinaryTree *bitree_build_from_preorder_inorder(void **preorder, int preorder_size, void **inorder, int inorder_size,
void(*destroy)(void *value),
bool (*equals)(const void *value1, const void *value2));

/**
* @brief Construct a binary tree from inorder and postorder traversal.
Expand All @@ -222,27 +234,41 @@ BinaryTree * bitree_build_from_preorder_inorder(void** preorder, int preorder_si
* @param equals Node value equals function
* @return A pointer to the root node of the constructed binary tree.
*/
BinaryTree* bitree_build_from_inorder_postorder(void** inorder,
int inorderSize,
int** postorder,
int postorderSize,
void(*destroy)(void* value),
bool (*equals)(const void *value1, const void* value2));
BinaryTree *bitree_build_from_inorder_postorder(void **inorder,
int inorderSize,
int **postorder,
int postorderSize,
void(*destroy)(void *value),
bool (*equals)(const void *value1, const void *value2));

/**
* @brief Returns the longest path length in the given branch
* @param root Root node of the given branch to find longest path
* @param diameter Pointer to the returned longest path length
* @return
*/
int bitree_height(BinaryTreeNode * root, int* diameter);
int bitree_height(BinaryTreeNode *root, int *diameter);

/**
* @brief Returns the longest path length in the given binary tree
* @param tree Tree to get the longest path on
* @return The longest path length of the given binary tree
*/
int bitree_diameter(BinaryTree * tree);
int bitree_diameter(BinaryTree *tree);

/**
* @brief Count the number of nodes in the given binary tree
* @param tree Binary tree to count nodes
* @return Node count of the given binary tree
*/
int bitree_nodeCount(BinaryTree *tree);

/**
* @brief Count the number of nodes in the given branch
* @param node Binary tree to count nodes
* @return Node count of the given branch
*/
int bitree_branchNodeCount(BinaryTreeNode *node);

#ifdef __cplusplus
/**
Expand Down
Loading

0 comments on commit 337136e

Please sign in to comment.