Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
Binary Tree API

Binary tree node utilities. More...

Functions

t_btree_nodebtree_new (void *data)
 Creates a new binary tree node.
 
void btree_set_left (t_btree_node *parent, t_btree_node *child)
 Sets the left child of a parent node.
 
void btree_set_right (t_btree_node *parent, t_btree_node *child)
 Sets the right child of a parent node.
 
t_btree_nodebtree_detach_left (t_btree_node *parent)
 Detaches and returns the left child of a parent node.
 
t_btree_nodebtree_detach_right (t_btree_node *parent)
 Detaches and returns the right child of a parent node.
 
void btree_free (t_btree_node **node, void(*data_free)(void *data))
 Recursively frees a node and its descendants.
 

Detailed Description

Binary tree node utilities.

Functions to create, link, detach and recursively free binary tree nodes.

Function Documentation

◆ btree_detach_left()

t_btree_node * btree_detach_left ( t_btree_node parent)

Detaches and returns the left child of a parent node.

Updates both parent->left and the detached child's parent pointer to NULL.

Warning
parent and parent->left must not be NULL.
Parameters
parentParent node to detach from (borrowed).
Returns
Detached left child node (owned by caller), or NULL if none.

◆ btree_detach_right()

t_btree_node * btree_detach_right ( t_btree_node parent)

Detaches and returns the right child of a parent node.

Updates both parent->right and the detached child's parent pointer to NULL.

Warning
parent and parent->right must not be NULL.
Parameters
parentParent node to detach from (borrowed).
Returns
Detached right child node (owned by caller), or NULL if none.

◆ btree_free()

void btree_free ( t_btree_node **  node,
void(*)(void *data)  data_free 
)

Recursively frees a node and its descendants.

Performs a post-order traversal and frees each node. If data_free is provided, it is called for each non-NULL node payload before node free.

Parameters
nodePointer to the root node pointer (set to NULL after freeing).
data_freeOptional payload destructor (can be NULL).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ btree_new()

t_btree_node * btree_new ( void *  data)

Creates a new binary tree node.

Note
Ownership of data is transferred to the new node on success. On failure, caller retains ownership of data.
Parameters
dataPayload for the new node (ownership transferred on success).
Returns
Pointer to the new node (owned), or NULL on allocation failure.

◆ btree_set_left()

void btree_set_left ( t_btree_node parent,
t_btree_node child 
)

Sets the left child of a parent node.

Links child under parent and updates child->parent accordingly.

Warning
parent and child must not be NULL.
Parameters
parentParent node to update (borrowed).
childChild node to attach as left child (borrowed).

◆ btree_set_right()

void btree_set_right ( t_btree_node parent,
t_btree_node child 
)

Sets the right child of a parent node.

Links child under parent and updates child->parent accordingly.

Warning
parent and child must not be NULL.
Parameters
parentParent node to update (borrowed).
childChild node to attach as right child (borrowed).