TechTorch

Location:HOME > Technology > content

Technology

Implementing Red-Black Trees in C with STL Containers

June 16, 2025Technology1207
Implementing Red-Black Trees in C with STL Containers Red-Black Trees

Implementing Red-Black Trees in C with STL Containers

Red-Black Trees are a type of self-balancing Binary Search Tree (BST) that have a specific set of properties to maintain balance and ensure efficient operations. While Standard Template Library (STL) containers like std::set and std::map are not directly implemented as Red-Black Trees, they use these data structures under the hood for their operations. In this article, we will explore how to implement a Red-Black Tree in C using basic C constructs, and then demonstrate how to use STL containers for similar purposes.

Simplified Red-Black Tree Implementation in C

Implementing a Red-Black Tree in C can be achieved using basic structures and functions. While libraries like STL provide optimized implementations, understanding the underlying concepts remains valuable.

#include iostream enum Color { RED, BLACK }; struct Node { int data; Color color; struct Node *left, *right, *parent; Node (int data) : data(data), color(RED), left(nullptr), right(nullptr), parent(nullptr) {} }; class RedBlackTree { private: struct Node *root; void rotateLeft(struct Node *pt) { struct Node *pt_y pt->right; pt->right pt_y->left; if (pt_y->left ! nullptr) pt_y->left->parent pt; pt_y->parent pt->parent; if (pt->parent nullptr) root pt_y; else if (pt pt->parent->left) pt->parent->left pt_y; else pt->parent->right pt_y; pt_y->left pt; pt->parent pt_y; } void rotateRight(struct Node *pt) { struct Node *pt_y pt->left; pt->left pt_y->right; if (pt_y->right ! nullptr) pt_y->right->parent pt; pt_y->parent pt->parent; if (pt->parent nullptr) root pt_y; else if (pt pt->parent->left) pt->parent->left pt_y; else pt->parent->right pt_y; pt_y->right pt; pt->parent pt_y; } void fixViolation(struct Node *pt) { struct Node *parent_pt nullptr, *grandparent_pt nullptr; while (pt ! root pt->color RED pt->parent->color RED) { parent_pt pt->parent; grandparent_pt parent_pt->parent; // Case: A if (parent_pt grandparent_pt->left) { struct Node *uncle_pt grandparent_pt->right; // Case: 1 if (uncle_pt uncle_pt->color RED) { grandparent_pt->color RED; parent_pt->color BLACK; uncle_pt->color BLACK; pt grandparent_pt; } else { // Case: 2 if (pt parent_pt->right) { rotateLeft(parent_pt); pt parent_pt; parent_pt pt->parent; } // Case: 3 rotateRight(grandparent_pt); std::swap(parent_pt->color, grandparent_pt->color); pt parent_pt; } } else { struct Node *uncle_pt grandparent_pt->left; // Case: 1 if (uncle_pt uncle_pt->color RED) { grandparent_pt->color RED; parent_pt->color BLACK; uncle_pt->color BLACK; pt grandparent_pt; } else { // Case: 2 if (pt parent_pt->left) { rotateRight(parent_pt); pt parent_pt; parent_pt pt->parent; } // Case: 3 rotateLeft(grandparent_pt); std::swap(parent_pt->color, grandparent_pt->color); pt parent_pt; } } } root->color BLACK; } public: RedBlackTree() : root(nullptr) {} void insert(const int data) { struct Node *pt new Node(data); root BSTInsert(root, pt); fixViolation(pt); } struct Node *BSTInsert(struct Node *root, struct Node *pt) { if (root nullptr) return pt; if (pt->data data) { root->left BSTInsert(root->left, pt); root->left->parent root; } else if (pt->data > root->data) { root->right BSTInsert(root->right, pt); root->right->parent root; } return root; } void inorder() { inorderHelper(root); } void inorderHelper(struct Node *root) { if (root nullptr) return; inorderHelper(root->left); std::cout data right); } }; int main() { RedBlackTree tree; 7, 3, 18, 10, 22, 8, 11, 26; (); return 0; }

This implementation includes the node structure, rotation functions, fix-violation logic, and insertion method. The inorder traversal is also provided for demonstration.

Using STL Containers

If you opt for leverage the power of STL containers, std::set and std::map are already optimized for use with Red-Black Trees and offer similar functionality without requiring additional coding. Below is an example of using std::set to perform common operations:

#include iostream #include set int main() { std::set mySet; 7, 3, 18, 10; std::cout

With std::set, elements are automatically kept in sorted order, and it handles duplicates based on Red-Black Tree properties.

Conclusion

While implementing a Red-Black Tree in C provides a deeper understanding of the data structure, using STL containers is often more efficient and simplifies development. Whether you implement the tree yourself or use STL for operations, Red-Black Trees offer excellent performance for ordered data management.