Gator Library
An implementation of the Red-Black Tree and Priority queue data structure in C++.
Loading...
Searching...
No Matches
redblack_tree.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <type_traits>
15#include "book.hpp"
16
18#define rbtree RBTree
19
21#define rbtnode RbtNode
22
23using namespace std;
24
32enum Color { RED, BLACK };
33
42class RbtNode {
43public:
44 int key;
51public:
59
65 RbtNode* sibling() const;
66};
67
75class RBTree
76{
77private:
81 void rotate_left(rbtnode *&, rbtnode *&);
82 void rotate_right(rbtnode *&, rbtnode *&);
85 rbtnode* insert_node(rbtnode* root_node, rbtnode* node);
87 void remove(rbtnode* node);
91 void fix_double_black(rbtnode* node);
93public:
97 RBTree();
98
104 int ColorFlipCount();
105
112
119
125 void add(book book_info);
126
132 void remove(int key);
133
140 rbtnode* find(int key) const;
141
148 std::vector<int> findclosest(int targetId) const;
149
156 rbtnode* operator[](int key) const;
157
163 bool empty() const;
164};
Header file for the RBTree class and RbtNode class.
Definition redblack_tree.hpp:76
void fix_double_black(rbtnode *node)
Definition redblack_tree.cpp:289
rbtnode * BSTreplace(rbtnode *x)
Definition redblack_tree.cpp:272
RBTree()
Constructs a new RBTree object.
Definition redblack_tree.cpp:21
rbtnode * end()
Returns a pointer to the end of the red-black tree.
rbtnode * insert_node(rbtnode *root_node, rbtnode *node)
Definition redblack_tree.cpp:80
void add(book book_info)
Adds a new book to the red-black tree.
Definition redblack_tree.cpp:193
rbtnode * find(int key) const
Finds a book in the red-black tree based on the given key.
Definition redblack_tree.cpp:202
rbtnode * begin()
Returns a pointer to the first node in the red-black tree.
bool empty() const
Checks if the red-black tree is empty.
Definition redblack_tree.cpp:482
rbtnode * root
Definition redblack_tree.hpp:78
std::vector< int > findclosest(int targetId) const
Finds the closest book IDs to the given target ID in the red-black tree.
Definition redblack_tree.cpp:223
int color_flip_count
Definition redblack_tree.hpp:79
void rotate_right(rbtnode *&, rbtnode *&)
Definition redblack_tree.cpp:56
int ColorFlipCount()
Returns the number of color flips performed during tree operations.
Definition redblack_tree.cpp:27
rbtnode * successor(rbtnode *x)
Definition redblack_tree.cpp:262
rbtnode * operator[](int key) const
Overloads the subscript operator to find a book in the red-black tree based on the given key.
Definition redblack_tree.cpp:476
void remove(rbtnode *node)
Definition redblack_tree.cpp:388
void fix_colorviolation(rbtnode *&, rbtnode *&)
Definition redblack_tree.cpp:99
void rotate_left(rbtnode *&, rbtnode *&)
Definition redblack_tree.cpp:32
Represents a node in the red-black tree.
Definition redblack_tree.hpp:42
RbtNode * left
Definition redblack_tree.hpp:46
RbtNode(int k, const book &v)
Constructs a new RbtNode object.
Definition redblack_tree.hpp:58
RbtNode * right
Definition redblack_tree.hpp:47
RbtNode * sibling() const
Returns the sibling of the node.
Definition redblack_tree.cpp:5
int key
Definition redblack_tree.hpp:44
RbtNode * parent
Definition redblack_tree.hpp:48
Color color
Definition redblack_tree.hpp:49
book value
Definition redblack_tree.hpp:45
Represents a book in the library.
Definition book.hpp:24
Color
Represents the color of a node in the red-black tree.
Definition redblack_tree.hpp:32
@ BLACK
Definition redblack_tree.hpp:32
@ RED
Definition redblack_tree.hpp:32
#define rbtnode
Red-black tree node type.
Definition redblack_tree.hpp:21