From d7c70b6d0dae52d40ff453ad664d7ab59c7aa786 Mon Sep 17 00:00:00 2001 From: "Bandyopadhyay, Sayan" Date: Wed, 31 Jan 2024 12:41:07 -0500 Subject: [PATCH] Review comments * remove number from BF_RBT_ERR * convert rbt_node_direction to bf_rbt_node_direction * convert enum vals to uppercase * Change APIs to return bf_rbt_node_direction_t instead of int * few whitespace changes --- include/target-utils/rbt/rbt.h | 24 ++++++++++++------------ src/rbt/rbt.c | 22 +++++++++++----------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/include/target-utils/rbt/rbt.h b/include/target-utils/rbt/rbt.h index 22a4ae9..56c8489 100644 --- a/include/target-utils/rbt/rbt.h +++ b/include/target-utils/rbt/rbt.h @@ -19,24 +19,24 @@ #define BLACK 0 #define RED 1 -typedef enum bf_rbt_sts_t { +typedef enum bf_rbt_sts { BF_RBT_OK, - BF_RBT_ERR = 3, + BF_RBT_ERR, BF_RBT_NO_KEY, BF_RBT_KEY_EXISTS } bf_rbt_sts_t; -enum rbt_node_direction { - left_node, - right_node, - invalid -}; +typedef enum bf_rbt_node_direction { + BF_RBT_LEFT_NODE, + BF_RBT_RIGHT_NODE, + BF_RBT_ROOT_NODE +} bf_rbt_node_direction_t; typedef struct bf_rbt_node_t { - uint32_t priority; - bool color; - struct bf_rbt_node_t *left, *right, *parent; - void *data; + uint32_t priority; + bool color; + struct bf_rbt_node_t *left, *right, *parent; + void *data; } bf_rbt_node_t; /*! @@ -108,7 +108,7 @@ bf_rbt_node_t *bf_get_neighbor_rbt_node(bf_rbt_node_t *root); * @param root child node for which it is necessary to determine its direction. * @return direction left/right child */ -int bf_get_rbt_node_direction(bf_rbt_node_t *root); +bf_rbt_node_direction_t bf_get_rbt_node_direction(bf_rbt_node_t *root); /*! * Retrieve inorder successor node for given node diff --git a/src/rbt/rbt.c b/src/rbt/rbt.c index cff1ef4..d898d37 100644 --- a/src/rbt/rbt.c +++ b/src/rbt/rbt.c @@ -209,13 +209,13 @@ bf_rbt_node_t *bf_get_lowest_priority_node(bf_rbt_node_t *rbt_head) { return current; } -int bf_get_rbt_node_direction(bf_rbt_node_t *root) { +bf_rbt_node_direction_t bf_get_rbt_node_direction(bf_rbt_node_t *root) { if (root->parent == NULL) return BF_RBT_ERR; if (root->parent->priority < root->priority || (root->parent->right != NULL && root->parent->right->priority == root->priority)) - return right_node; - return left_node; + return BF_RBT_RIGHT_NODE; + return BF_RBT_LEFT_NODE; } bool bf_get_rbt_neigh_color(bf_rbt_node_t *root) { @@ -366,7 +366,7 @@ bf_rbt_node_t *bf_bst_node_deletion(uint32_t key, bf_rbt_node_t *rbt_head, int * void bf_balance_rbt_post_deletion(bf_rbt_node_t *node, bf_rbt_node_t **rbt_head) { bf_rbt_node_t *neigh_node; bool imbalance_tree = true; - int node_dir; + bf_rbt_node_direction_t node_dir; while (imbalance_tree != false && node != NULL) { if (node->parent == NULL) { imbalance_tree = false; @@ -393,7 +393,7 @@ void bf_balance_rbt_post_deletion(bf_rbt_node_t *node, bf_rbt_node_t **rbt_head) /* Check if far child from current node is RED which means * right child of neighbor is RED if current node is in the left */ - else if (node_dir == left_node && neigh_node->right != NULL && neigh_node->right->color == RED) { + else if (node_dir == BF_RBT_LEFT_NODE && neigh_node->right != NULL && neigh_node->right->color == RED) { bf_left_rotate_rbt_node(node->parent, rbt_head); neigh_node->right->color = BLACK; imbalance_tree = false; @@ -401,7 +401,7 @@ void bf_balance_rbt_post_deletion(bf_rbt_node_t *node, bf_rbt_node_t **rbt_head) /* Mirror copy of above case * left child of neighbor is RED if current node is in the right */ - else if (node_dir == right_node && neigh_node->left != NULL && neigh_node->left->color == RED) { + else if (node_dir == BF_RBT_RIGHT_NODE && neigh_node->left != NULL && neigh_node->left->color == RED) { bf_right_rotate_rbt_node(node->parent, rbt_head); neigh_node->left->color = BLACK; imbalance_tree = false; @@ -409,19 +409,19 @@ void bf_balance_rbt_post_deletion(bf_rbt_node_t *node, bf_rbt_node_t **rbt_head) /* Check if far child from current node is BLACK and near child is RED which means * right child of neighbor is BLACK, left child of neighbor is RED if current node is in the left */ - else if (node_dir == left_node && (neigh_node->right == NULL || neigh_node->right->color == BLACK) && + else if (node_dir == BF_RBT_LEFT_NODE && (neigh_node->right == NULL || neigh_node->right->color == BLACK) && (neigh_node->left != NULL && neigh_node->left->color == RED)) { bf_right_rotate_rbt_node(neigh_node, rbt_head); } /* Mirror copy of above case * right child of neighbor is RED, left child of neighbor is BLACK if current node is in the right */ - else if (node_dir == right_node && (neigh_node->left == NULL || neigh_node->left->color == BLACK) && + else if (node_dir == BF_RBT_RIGHT_NODE && (neigh_node->left == NULL || neigh_node->left->color == BLACK) && (neigh_node->right != NULL && neigh_node->right->color == RED)) { bf_left_rotate_rbt_node(neigh_node, rbt_head); } } else { - if (node_dir == right_node) + if (node_dir == BF_RBT_RIGHT_NODE) bf_right_rotate_rbt_node(node->parent, rbt_head); else bf_left_rotate_rbt_node(node->parent, rbt_head); @@ -433,7 +433,7 @@ void bf_balance_rbt_post_deletion(bf_rbt_node_t *node, bf_rbt_node_t **rbt_head) int bf_remove_rbt_entry(uint32_t key, bf_rbt_node_t **rbt_head) { bf_rbt_node_t *res_node, *parent; - int child_dir; + bf_rbt_node_direction_t child_dir; int color; //Apply traditional BST deletion and get the node to be deleted res_node = bf_bst_node_deletion(key, *rbt_head, &color); @@ -450,7 +450,7 @@ int bf_remove_rbt_entry(uint32_t key, bf_rbt_node_t **rbt_head) { } else { // Update the parent of node to be deleted child_dir = bf_get_rbt_node_direction(res_node); - if (child_dir == left_node) { + if (child_dir == BF_RBT_LEFT_NODE) { parent->left = NULL; } else { parent->right = NULL;