Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
saynb committed Jan 31, 2024
1 parent 5a87af4 commit d7c70b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
24 changes: 12 additions & 12 deletions include/target-utils/rbt/rbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*!
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/rbt/rbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -393,35 +393,35 @@ 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;
}
/* 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;
}
/* 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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit d7c70b6

Please sign in to comment.