4MYKIMUNWPTZBZB4YLGVGWJW6WVSWGU7B3AI2BGGD3MQ6MIJRN4QC
int main() {
// TODO(student): write at least 1000 lines of test
int main() {
// red = 0, black = 1
RedBlackTree<int> tree;
tree.insert(2);
tree.insert(1);
tree.insert(3);
tree.insert(4);
tree.insert(5);
std::cout << tree.contains(4) << "\n";
std::cout << tree.find_min() << "\n";
std::cout << tree.find_max() << "\n";
tree.print_tree();
auto root = tree.get_root();
std::cout << tree.color(root) << "\n";
Node* copy(Node* node) const {
if (node == nullptr)
return nullptr;
else
return new Node(node->data, node->color, copy(node->left), copy(node->right), node->parent);
}
void display(Node* root, std::ostream& out, unsigned int counter = 0) const {
if (m_root == nullptr) {
out << "<empty>\n";
} else if (root == nullptr) {
out << "";
} else {
display(root->right, out, counter + 1);
for (unsigned int i = 0; i < counter; i++) out << " ";
char c = root->color ? 'b' : 'r';
out << c << root->data << "\n";
display(root->left, out, counter + 1);
}
}
void left_rotate(Node* x) {
Node* y = x->right;
x->right = y->left;
if (y->left != nullptr) {
y->left->parent = x;
}
y->parent = x->parent;
if (x->parent == nullptr) {
m_root = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
}
y->left = x;
x->parent = y;
}
void right_rotate(Node* x) {
Node* y = x->left;
x->left = y->right;
if (y->right != nullptr) {
y->right->parent = x;
}
y->parent = x->parent;
if (x->parent == nullptr) {
m_root = y;
} else if (x == x->parent->right) {
x->parent->right = y;
} else {
x->parent->left = y;
}
y->right = x;
x->parent = y;
}
RedBlackTree(const RedBlackTree& tree);
~RedBlackTree();
RedBlackTree& operator=(const RedBlackTree& rhs);
RedBlackTree(const RedBlackTree& tree) : m_root(nullptr) {
m_root = copy(tree.m_root);
}
~RedBlackTree() {
make_empty(m_root);
}
RedBlackTree& operator=(const RedBlackTree& rhs) {
if (this == &rhs)
return *this;
make_empty(m_root);
m_root = copy(rhs.m_root);
return *this;
}
void insert(const T& item) {
if (contains(item)) {
return;
} else {
Node* new_node = new Node(item);
if (m_root == nullptr) {
m_root = new_node;
} else {
Node* temp = m_root;
Node* running_parent;
// the default node color is black but newly inserted nodes are red so we must update here
new_node->color = RED;
while (temp != nullptr) {
running_parent = temp;
if (item < temp->data)
temp = temp->left;
else if (item > temp->data)
temp = temp->right;
}
void insert(const T& item);
new_node->parent = running_parent;
if (item > running_parent->data)
running_parent->right = new_node;
else
running_parent->left = new_node;
if (new_node->parent->parent == nullptr)
return;
insert_fix(new_node);
}
}
}
void insert_fix(Node* node) {
Node* uncle;
while (node->parent->color == RED) {
if (node->parent == node->parent->parent->right) {
uncle = node->parent->parent->left;
if (uncle->color == RED) {
uncle->color = BLACK;
node->parent->color = BLACK;
node->parent->parent->color = RED;
node = node->parent->parent;
} else {
if (node == node->parent->left) {
node = node->parent;
right_rotate(node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
left_rotate(node->parent->parent);
}
} else {
uncle = node->parent->parent->right;
if (uncle->color == RED) {
uncle->color = BLACK;
node->parent->color = BLACK;
node->parent->parent->color = RED;
node = node->parent->parent;
} else {
if (node == node->parent->right) {
node = node->parent;
left_rotate(node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
right_rotate(node->parent->parent);
}
}
if (node == m_root)
break;
}
m_root->color = BLACK;
}