Refactored static functions in BinaryTree.c
This commit is contained in:
parent
60c3f81884
commit
88e4385c5f
1 changed files with 32 additions and 32 deletions
|
@ -13,19 +13,19 @@ typedef struct BinaryTreeWalker_s {
|
|||
enum BinaryTreeNodePosition position;
|
||||
} BinaryTreeWalker;
|
||||
|
||||
static size_t _TreeNodeSize(BinaryTree* tree)
|
||||
static size_t _BinaryTree_TreeNodeSize(BinaryTree* tree)
|
||||
{
|
||||
return sizeof(BinaryTreeNode) + tree->value_size;
|
||||
}
|
||||
|
||||
static inline size_t _DepthOf(BinaryTreeNode* node)
|
||||
static inline size_t _BinaryTree_DepthOf(BinaryTreeNode* node)
|
||||
{
|
||||
return node == NULL ? 0 : node->depth;
|
||||
}
|
||||
|
||||
static BinaryTreeNode* _CreateNode(BinaryTree* tree, void* value)
|
||||
static BinaryTreeNode* _BinaryTree_CreateNode(BinaryTree* tree, void* value)
|
||||
{
|
||||
BinaryTreeNode* node = Allocator_Allocate(tree->allocator, _TreeNodeSize(tree));
|
||||
BinaryTreeNode* node = Allocator_Allocate(tree->allocator, _BinaryTree_TreeNodeSize(tree));
|
||||
if (node != NULL) {
|
||||
memcpy(node->value, value, tree->value_size);
|
||||
node->depth = 1;
|
||||
|
@ -50,7 +50,7 @@ int BinaryTree_Create(BinaryTree* target, BinaryTreeComparator compare, void* xd
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static void _UpdateDepth(BinaryTreeNode* node)
|
||||
static void _BinaryTreeNode_UpdateDepth(BinaryTreeNode* node)
|
||||
{
|
||||
size_t depth_left = 0, depth_right = 0;
|
||||
|
||||
|
@ -70,11 +70,11 @@ static void _UpdateDepth(BinaryTreeNode* node)
|
|||
node->depth += 1;
|
||||
}
|
||||
|
||||
static void _UpdateDepths(DynamicArray* parents)
|
||||
static void _BinaryTree_UpdateDepths(DynamicArray* parents)
|
||||
{
|
||||
for (size_t i = parents->reserved - 1; i != SIZE_MAX; i--) {
|
||||
BinaryTreeWalker* walker = DynamicArray_GetPointer(parents, i);
|
||||
_UpdateDepth(walker->node);
|
||||
_BinaryTreeNode_UpdateDepth(walker->node);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,8 +137,8 @@ static int _BinaryTree_Insert(BinaryTree* tree, BinaryTreeNode* node)
|
|||
}
|
||||
}
|
||||
|
||||
_UpdateDepths(&tree->walker_stack);
|
||||
_UpdateDepth(node);
|
||||
_BinaryTree_UpdateDepths(&tree->walker_stack);
|
||||
_BinaryTreeNode_UpdateDepth(node);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -166,7 +166,7 @@ static BinaryTreeNode** _BinaryTreeNode_SmallestNodeLocation(BinaryTreeNode* nod
|
|||
|
||||
static int _BinaryTreeNode_UnlinkNode(BinaryTreeNode** nodep)
|
||||
{
|
||||
size_t node_depth = _DepthOf(*nodep);
|
||||
size_t node_depth = _BinaryTree_DepthOf(*nodep);
|
||||
if (node_depth == 1) {
|
||||
// leaf node without children
|
||||
*nodep = NULL;
|
||||
|
@ -231,9 +231,9 @@ static int _BinaryTree_Remove(BinaryTree* self, void* value)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int _GrowParentStack(BinaryTree* tree)
|
||||
int _BinaryTree_GrowWalkerStack(BinaryTree* tree)
|
||||
{
|
||||
size_t depth = _DepthOf(tree->root);
|
||||
size_t depth = _BinaryTree_DepthOf(tree->root);
|
||||
if (tree->walker_stack.capacity < depth + 1) {
|
||||
return DynamicArray_Resize(&tree->walker_stack, tree->walker_stack.capacity * 2);
|
||||
}
|
||||
|
@ -242,7 +242,7 @@ int _GrowParentStack(BinaryTree* tree)
|
|||
}
|
||||
|
||||
// right is heavier, shift it to the left
|
||||
static void _RotateLeft(BinaryTreeNode** node)
|
||||
static void _BinaryTreeNode_RotateLeft(BinaryTreeNode** node)
|
||||
{
|
||||
BinaryTreeNode* right = (*node)->right;
|
||||
|
||||
|
@ -252,7 +252,7 @@ static void _RotateLeft(BinaryTreeNode** node)
|
|||
}
|
||||
|
||||
// left is heavier, shift it to the right
|
||||
static void _RotateRight(BinaryTreeNode** node)
|
||||
static void _BinaryTreeNode_RotateRight(BinaryTreeNode** node)
|
||||
{
|
||||
BinaryTreeNode* left = (*node)->left;
|
||||
|
||||
|
@ -261,28 +261,28 @@ static void _RotateRight(BinaryTreeNode** node)
|
|||
*node = left;
|
||||
}
|
||||
|
||||
static void _RebalanceNode(BinaryTreeNode** node)
|
||||
static void _BinaryTreeNode_Rebalance(BinaryTreeNode** node)
|
||||
{
|
||||
size_t left_depth = _DepthOf(node[0]->left);
|
||||
size_t right_depth = _DepthOf(node[0]->right);
|
||||
size_t left_depth = _BinaryTree_DepthOf(node[0]->left);
|
||||
size_t right_depth = _BinaryTree_DepthOf(node[0]->right);
|
||||
|
||||
if (left_depth < right_depth && right_depth - left_depth > 1) {
|
||||
_RotateLeft(node);
|
||||
_UpdateDepth((*node)->left); // old node
|
||||
_UpdateDepth(*node); // rotated node
|
||||
_BinaryTreeNode_RotateLeft(node);
|
||||
_BinaryTreeNode_UpdateDepth((*node)->left); // old node
|
||||
_BinaryTreeNode_UpdateDepth(*node); // rotated node
|
||||
} else if (right_depth < left_depth && left_depth - right_depth > 1) {
|
||||
_RotateRight(node);
|
||||
_UpdateDepth((*node)->right); // old node
|
||||
_UpdateDepth(*node); // rotated node
|
||||
_BinaryTreeNode_RotateRight(node);
|
||||
_BinaryTreeNode_UpdateDepth((*node)->right); // old node
|
||||
_BinaryTreeNode_UpdateDepth(*node); // rotated node
|
||||
}
|
||||
}
|
||||
|
||||
static void _RebalanceRoot(BinaryTree* tree)
|
||||
static void _BinaryTree_RebalanceRoot(BinaryTree* tree)
|
||||
{
|
||||
_RebalanceNode(&tree->root);
|
||||
_BinaryTreeNode_Rebalance(&tree->root);
|
||||
}
|
||||
|
||||
static void _Rebalance(BinaryTree* tree)
|
||||
static void _BinaryTree_Rebalance(BinaryTree* tree)
|
||||
{
|
||||
for (size_t i = tree->walker_stack.reserved - 1; i + 1 != 0; i--) {
|
||||
BinaryTreeWalker* walker = DynamicArray_GetPointer(
|
||||
|
@ -318,11 +318,11 @@ static void _Rebalance(BinaryTree* tree)
|
|||
|
||||
}
|
||||
|
||||
_RebalanceNode(nodep);
|
||||
_BinaryTreeNode_Rebalance(nodep);
|
||||
}
|
||||
|
||||
|
||||
_RebalanceRoot(tree);
|
||||
_BinaryTree_RebalanceRoot(tree);
|
||||
}
|
||||
|
||||
int BinaryTree_Insert(BinaryTree* tree, void* value)
|
||||
|
@ -330,18 +330,18 @@ int BinaryTree_Insert(BinaryTree* tree, void* value)
|
|||
if (tree == NULL) return EDESTADDRREQ;
|
||||
if (value == NULL) return EINVAL;
|
||||
|
||||
if (_GrowParentStack(tree)) {
|
||||
if (_BinaryTree_GrowWalkerStack(tree)) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
BinaryTreeNode* new_node = _CreateNode(tree, value);
|
||||
BinaryTreeNode* new_node = _BinaryTree_CreateNode(tree, value);
|
||||
if (new_node == NULL) return ENOMEM;
|
||||
|
||||
if (_BinaryTree_Insert(tree, new_node)) {
|
||||
_BinaryTree_DestroyNode(tree, new_node);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
_Rebalance(tree);
|
||||
_BinaryTree_Rebalance(tree);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -356,7 +356,7 @@ int BinaryTree_Remove(BinaryTree* self, void* value)
|
|||
return remove_code;
|
||||
}
|
||||
|
||||
_Rebalance(self);
|
||||
_BinaryTree_Rebalance(self);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue