aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian C <cc@localhost>2025-03-26 16:27:05 -0700
committerChristian C <cc@localhost>2025-03-26 16:27:05 -0700
commit18d3621cb789e443843350e5d9fbbe7bc434339c (patch)
tree1390fca60a5f5330bd8d508d12b8cdc3332cc750
parent1b05c26a8ede821f84c042e25438040c36d4aee0 (diff)
Reuse library code
-rw-r--r--lib/seg/mask_data.c61
1 files changed, 2 insertions, 59 deletions
diff --git a/lib/seg/mask_data.c b/lib/seg/mask_data.c
index 6033c3b..d1713e2 100644
--- a/lib/seg/mask_data.c
+++ b/lib/seg/mask_data.c
@@ -22,70 +22,13 @@ bool_t compare_labels(MaskData* left, MaskData* right)
// Create AVL Mask node
AVLNode* create_avl_mask_node(MaskData* data)
{
- AVLNode* node = (AVLNode*)malloc(sizeof(AVLNode));
- if (node == NULL) {
- return NULL;
- }
- node->data = data;
- node->compare = (bool_t (*)(void*,void*))&compare_labels;
- node->left = NULL;
- node->right = NULL;
- node->height = 1; // Leaf initially
- return node;
+ return create_avl_node((void*)data, (AvlComparator)compare_labels);
}
// Insert MaskData into the AVL Tree
Result insert_mask(AVLNode* node, MaskData* data)
{
- Result result;
- // 1. Standard BST insertion
- if (node == NULL) {
- return (Result) {create_avl_mask_node(data), TRUE};
- }
-
- MaskData *node_data = (MaskData*)node->data;
- if (node->compare(data, node_data)) {
- result = insert_mask(node->left, data);
- if (!result.success) {
- fprintf(stderr, "Failed to insert!");
- return result;
- }
- node->left = (AVLNode*)result.data;
- } else if (node->compare(node->data, data)) {
- result = insert_mask(node->right, data);
- if (!result.success) {
- fprintf(stderr, "Failed to insert!");
- return result;
- }
- node->right = (AVLNode*)result.data;
- } else {
- return (Result) {node, FALSE};
- }
-
- // 2. Update height of the ancestor node
- node->height = 1 + max_height(get_height(node->left), get_height(node->right));
-
- ssize_t balance = get_balance_factor(node);
-
- // 4. If the node becomes unbalanced
-
- // LeftLeft
- if ((balance > 1) && node->compare(data, node->left->data)) {
- return (Result) {right_rotate(node), TRUE};
- }
- // RightRight
- if ((balance < -1) && node->compare(node->right->data, data)) {
- return (Result) {left_rotate(node), TRUE};
- }
- // LeftRight
- if ((balance > 1) && node->compare(node->left->data, data)) {
- return (Result) {right_rotate(node), TRUE};
- }
- // RightLeft
- if ((balance < -1) && node->compare(data,node->right->data)) {
- return (Result) {left_rotate(node), TRUE};
- }
- return (Result) {node, TRUE};
+ return avl_insert(node, (void*)data, (AvlComparator)compare_labels);
}
// Allocate a label's Mask data in a tree