-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbtree.h
132 lines (98 loc) · 3.38 KB
/
rbtree.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************
* DynFMI - Dynamic FM-Index for a Collection of Texts *
* Copyright (C) 2006 Wolfgang Gerlach *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
// This is a red-black tree implementation by Wolfgang Gerlach based on the algorithm provided by
// Cormen et al.: Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001
using namespace std;
#ifndef GUARD_RBTree
#define GUARD_RBTree
#include <stdlib.h>
#include "types.h"
namespace rbtree {
enum RBNodecolor{BLACK,RED};
// generic Red-Black Tree Node:
class RBNode
{
public:
RBNode* parent;
RBNode* left;
RBNode* right; // 3*4 bytes
enum RBNodecolor color; // due to structure alignment: 4 bytes !!!
RBNode(){};
RBNode(RBNode *n)
: parent(n), left(n), right(n){
color=RED;
}
virtual ~RBNode(){} //adds 4 bytes vtable
RBNode* getParent(){
return parent;
}
RBNode* getLeft(){
return left;
}
RBNode* getRight(){
return right;
}
void setParent(RBNode* n){
parent=n;
}
void setLeft(RBNode* n){
left=n;
}
void setRight(RBNode* n){
right=n;
}
};
class RBTree{
public:
RBNode *root;
RBNode *nil;
virtual ~RBTree();
void checkTree();
ulong countBlack(RBNode *n){
if (n == nil)
return 0;
return ((n->color == BLACK) ? 1: 0)
+ countBlack(n->left) + countBlack(n->right);
}
void rbInsertFixup(RBNode* z, void (*updateNode)(RBNode* n, RBTree *T));
void rbDeleteFixup(RBNode *x, void (*updateNode)(RBNode* n, RBTree *T));
void rbDelete(RBNode *z, void (*updateNode)(RBNode* n, RBTree *T));
RBNode* findRightSiblingLeaf(RBNode *n);
RBNode* findLeftSiblingLeaf(RBNode *n);
RBNode* treeSuccessor(RBNode *x);
RBNode* treePredecessor(RBNode *x);
RBNode* treeMinimum(RBNode *x);
RBNode* treeMaximum(RBNode *x);
bool isLeftChild(RBNode *n);
bool isRightChild(RBNode *n);
int getNodeMaxDepth(RBNode *n);
int getNodeMinDepth(RBNode *n);
void printSubTree(RBNode *n);
void checkSubTree(RBNode *n);
void checkNode(RBNode *x);
void deleteNode(RBNode* x){
if (x->left!=nil) deleteNode(x->left);
if (x->right!=nil) deleteNode(x->right);
delete x;
}
private:
void leftRotate(RBNode* x, void (*updateNode)(RBNode* n, RBTree *T));
void rightRotate(RBNode* x, void (*updateNode)(RBNode* n, RBTree *T));
};
} // namespace
#endif