Posts

Showing posts from March, 2023

CODE FOR RED BLACK TREE

/** C++ implementation for Red-Black Tree Insertion This code is adopted from the code provided by Dinesh Khandelwal in comments **/ #include <bits/stdc++.h> using namespace std; enum Color {RED, BLACK}; struct Node {  int data;  bool color;  Node *left, *right, *parent;  // Constructor  Node(int data)  {  this->data = data;  left = right = parent = NULL;  this->color = RED;  } }; // Class to represent Red-Black Tree class RBTree { private:  Node *root; protected:  void rotateLeft(Node *&, Node *&);  void rotateRight(Node *&, Node *&);  void fixViolation(Node *&, Node *&); public:  // Constructor  RBTree() { root = NULL; }  void insert(const int &n);  void inorder();  void levelOrder(); }; // A recursive function to do inorder traversal void inorderHelper(Node *root) {  if (root == NULL)   return;  inorderHelper(root->left);  cout ...