diff options
author | Christian C <cc@localhost> | 2025-03-23 15:34:48 -0700 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-03-23 15:34:48 -0700 |
commit | 22c32ae8649e8540198942b33d4bab72c4ea7238 (patch) | |
tree | 946e173c4ab362ec88f21854e8613a21c330a6e1 /lib/algo | |
parent | 981e8bfd12f79cb469bb54a915230eda6dafab41 (diff) |
Userspace types
Diffstat (limited to 'lib/algo')
-rw-r--r-- | lib/algo/avl_tree.c | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/lib/algo/avl_tree.c b/lib/algo/avl_tree.c index 9ac5d47..7b86bab 100644 --- a/lib/algo/avl_tree.c +++ b/lib/algo/avl_tree.c @@ -5,7 +5,7 @@ #include <stdio.h> // Get the height of an AVL node -AvlHeight_t get_height(struct AVLNode* node) +AvlHeight_t get_height(AVLNode* node) { if (node == NULL) { return 0; @@ -20,7 +20,7 @@ AvlHeight_t max_height(AvlHeight_t a, AvlHeight_t b) } // Get the balance factor of a node -ssize_t get_balance_factor(struct AVLNode* node) +ssize_t get_balance_factor(AVLNode* node) { if (node == NULL) { return 0; @@ -29,10 +29,10 @@ ssize_t get_balance_factor(struct AVLNode* node) } // Rotate an AVL node right -struct AVLNode* right_rotate(struct AVLNode* parent) +AVLNode* right_rotate(AVLNode* parent) { - struct AVLNode* child1 = parent->left; - struct AVLNode* child2 = child1->right; + AVLNode* child1 = parent->left; + AVLNode* child2 = child1->right; child1->right = parent; parent->left = child2; @@ -43,10 +43,10 @@ struct AVLNode* right_rotate(struct AVLNode* parent) } // Rotate an AVL node left -struct AVLNode* left_rotate(struct AVLNode* parent) +AVLNode* left_rotate(AVLNode* parent) { - struct AVLNode* child1 = parent->right; - struct AVLNode* child2 = child1->left; + AVLNode* child1 = parent->right; + AVLNode* child2 = child1->left; child1->left = parent; parent->right = child2; @@ -57,9 +57,9 @@ struct AVLNode* left_rotate(struct AVLNode* parent) } // Create AVL node -struct AVLNode* create_avl_node(void* data, AvlComparator compare) +AVLNode* create_avl_node(void* data, AvlComparator compare) { - struct AVLNode* node = (struct AVLNode*)malloc(sizeof(struct AVLNode)); + AVLNode* node = (AVLNode*)malloc(sizeof(AVLNode)); if (node == NULL) { return NULL; } @@ -72,12 +72,12 @@ struct AVLNode* create_avl_node(void* data, AvlComparator compare) } // Insert data into AVL tree -struct Result avl_insert(struct AVLNode* node, void* data, AvlComparator compare) +Result avl_insert(AVLNode* node, void* data, AvlComparator compare) { - struct Result result; + Result result; // 1. Standard BST insertion if (node == NULL) { - return (struct Result) {create_avl_node(data, compare), TRUE}; + return (Result) {create_avl_node(data, compare), TRUE}; } if (node->compare(data, node->data)) { @@ -86,16 +86,16 @@ struct Result avl_insert(struct AVLNode* node, void* data, AvlComparator compare fprintf(stderr, "Failed to insert!"); return result; } - node->left = (struct AVLNode*)result.data; + node->left = (AVLNode*)result.data; } else if (node->compare(node->data, data)) { result = avl_insert(node->right, data, compare); if (!result.success) { fprintf(stderr, "Failed to insert!"); return result; } - node->right = (struct AVLNode*)result.data; + node->right = (AVLNode*)result.data; } else { - return (struct Result) {node, FALSE}; + return (Result) {node, FALSE}; } // 2. Update height of the ancestor node @@ -107,25 +107,25 @@ struct Result avl_insert(struct AVLNode* node, void* data, AvlComparator compare // LeftLeft if ((balance > 1) && node->compare(data, node->left->data)) { - return (struct Result) {right_rotate(node), TRUE}; + return (Result) {right_rotate(node), TRUE}; } // RightRight if ((balance < -1) && node->compare(node->right->data, data)) { - return (struct Result) {left_rotate(node), TRUE}; + return (Result) {left_rotate(node), TRUE}; } // LeftRight if ((balance > 1) && node->compare(node->left->data, data)) { - return (struct Result) {right_rotate(node), TRUE}; + return (Result) {right_rotate(node), TRUE}; } // RightLeft if ((balance < -1) && node->compare(data,node->right->data)) { - return (struct Result) {left_rotate(node), TRUE}; + return (Result) {left_rotate(node), TRUE}; } - return (struct Result) {node, TRUE}; + return (Result) {node, TRUE}; } // In-order traversal print pointer -void print_in_order(struct AVLNode* root) +void print_in_order(AVLNode* root) { if (root != NULL) { print_in_order(root->left); @@ -135,7 +135,7 @@ void print_in_order(struct AVLNode* root) } // Free avl tree nodes starting at root -void free_avl_tree(struct AVLNode* root) +void free_avl_tree(AVLNode* root) { if (root != NULL) { free_avl_tree(root->left); @@ -145,7 +145,7 @@ void free_avl_tree(struct AVLNode* root) } // Free avl tree and their data starting at root -void free_avl_tree_nodes(struct AVLNode* root) +void free_avl_tree_nodes(AVLNode* root) { if (root != NULL) { free_avl_tree_nodes(root->left); |