-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedBlackTree.h
58 lines (52 loc) · 1.69 KB
/
RedBlackTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef RBT_H
#define RBT_H
#define COLOR_BLACK 0
#define COLOR_RED 1
#define COLOR_DOUBLE_BLACK 2
#include <string>
using namespace std;
struct RBTNode
{
int data;
unsigned short int color;
RBTNode* left = nullptr;
RBTNode* right = nullptr;
RBTNode* parent = nullptr;
bool isNullNode = false;
};
class RedBlackTree {
public:
~RedBlackTree();
RedBlackTree();
RedBlackTree(const RedBlackTree& other);
void Insert(int val);
void Remove(int val);
bool Contains(int val);
int GetMin();
int GetMax();
unsigned long long int Size();
string ToInfixString() const {return ToInfixString(root);};
string ToPrefixString() const {return ToPrefixString(root);};
string ToPostfixString() const {return ToPostfixString(root);};
private:
RBTNode* root;
unsigned long long int numItems;
string ToInfixString(RBTNode* node) const;
string ToPrefixString(RBTNode* node) const;
string ToPostfixString(RBTNode* node) const;
RBTNode* GetParentSibling(RBTNode* node);
RBTNode* GetGrandparent(RBTNode* node);
void Recolor(RBTNode* node);
void RotateLeft(RBTNode* node);
void RotateRight(RBTNode* node);
void SwapColor(RBTNode* node1, RBTNode* node2);
void Recheck(RBTNode* node);
RBTNode* GetSuccessor(RBTNode* node);
void DeleteNode(RBTNode* node);
void FixDoubleBlack(RBTNode* node);
void ReplaceAndDeleteNode(RBTNode* deleteNode, RBTNode* replaceNode);
RBTNode* GetSibling(RBTNode* node);
RBTNode* GetReplaceNode(RBTNode* node);
void DeleteTree(RBTNode* node);
};
#endif