-
Notifications
You must be signed in to change notification settings - Fork 0
/
redblacktree.h
111 lines (71 loc) · 1.65 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by sasuke on 1/26/21.
//
#ifndef REDBLACKTREE_REDBLACKTREE_H
#define REDBLACKTREE_REDBLACKTREE_H
#include <algorithm>
#include <iostream>
typedef int Value;
typedef int Leaf;
typedef int Key;
enum Color {
BLACK = 0, RED
};
enum STatus {
EQUAL = 0, LESS = -1, BIGGER = 1
};
int isEqualTo(Key k1, Key k2);
class Node;
/**
* @brief basic node. implement rotate, flip and so on.
*/
class Node {
friend Node *rotateLeft(Node *h);
friend Node *rotateRight(Node *h);
friend void flipColors(Node *h);
friend bool isRed(Node *root);
public:
explicit Node(Key k, Value val, Color col);
~Node() = default;
Value getVal() const { return this->value; }
void setVal(Value val) { this->value = val; }
void setLchild(Node *child) {
this->lchild = child;
}
void setRchild(Node *child) {
this->rchild = child;
}
Key getKey() const { return this->key; }
Node *getLchild() const { return this->lchild; }
Node *getRchild() const { return this->rchild; }
void setColor(Color col) { this->color = col; }
Color getColor() const { return this->color; }
void setKey(Key k) { this->key = k; }
private:
Value value;
Color color;
Key key;
Node *lchild, *rchild;
};
/**
* @brief
*/
class RedBlackTree {
public:
explicit RedBlackTree(Node *t);
~RedBlackTree() = default;
Node *put(Key k, Value val);
void deletemin();
void deletemax();
bool find(Key key, Value &val);
void del(Key k);
Key getminKey(Node *node);
private:
Node *root;
Node *deletemin(Node *h);
Node *deletemax(Node *h);
Node *find(Node *h, Key k);
Node *put(Node *node, Key k, Value val);
Node *del(Node *h, Key k);
};
#endif //REDBLACKTREE_REDBLACKTREE_H