Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
rbNode.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
65# ifndef TPL_RBNODE_H
66# define TPL_RBNODE_H
67
68# include <tpl_binNode.H>
69
71typedef unsigned char Color;
72
74# define COLOR(p) ((p)->getColor())
75
77# define RED (0)
78
80# define BLACK (1)
81
91{
92private:
93
95
96public:
97
99 RbNode_Data() noexcept : color(RED) {}
100
103
105 Color & getColor() { return color; }
106
108 Color getColor() const { return color; }
109
111 void reset() { color = RED; }
112
113};
114
117
130 template <class Node>
131bool test_black_condition(Node *p, int &max, int bh = 0)
132{
133 // A path ends at NullPtr, not at a leaf: a node with a single child
134 // also ends one, through its missing child.
135 if (p == Node::NullPtr)
136 {
137 if (max == -1) // first path end reached
138 max = bh;
139
140 return bh == max;
141 }
142
143 if (COLOR(p) == BLACK)
144 bh++; // count this black node
145
146 return test_black_condition(LLINK(p), max, bh) and
148}
149
163 template <class Node>
164int red_black_height(Node *p) noexcept
165{
166 if (p == Node::NullPtr)
167 return 0;
168
169 if (not (COLOR(p) == RED or COLOR(p) == BLACK))
170 return -1;
171
172 if (COLOR(p) == RED)
173 {
174 if (LLINK(p) != Node::NullPtr and COLOR(LLINK(p)) == RED)
175 return -1;
176 if (RLINK(p) != Node::NullPtr and COLOR(RLINK(p)) == RED)
177 return -1;
178 }
179
180 const int left_height = red_black_height(LLINK(p));
181 if (left_height < 0)
182 return -1;
183
184 if (red_black_height(RLINK(p)) != left_height)
185 return -1;
186
187 return left_height + (COLOR(p) == BLACK ? 1 : 0);
188}
189
202 template <class Node>
203bool is_red_black(Node * node)
204{
205 if (node == Node::NullPtr)
206 return true;
207
208 if (not (COLOR(node) == RED or COLOR(node) == BLACK))
209 return false;
210
211 // Red nodes cannot have red children
212 if (COLOR(node) == RED)
213 if (COLOR(LLINK(node)) == RED or COLOR(RLINK(node)) == RED)
214 return false;
215
216 int max = -1;
217
218 return test_black_condition(node, max, 0);
219}
220
233 template <class Node>
235{
236 return red_black_height(node) >= 0;
237}
238
250 template <class Node, class Compare>
251bool is_red_black_bst(Node * node, Compare & cmp)
252{
253 if (not is_red_black_tree(node))
254 return false;
255
256 return check_bst(node, cmp);
257}
258# endif // TPL_RBNODE_H
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:83
WeightedDigraph::Node Node
Data portion of a Red-Black tree node.
Definition rbNode.H:91
Color & getColor()
Get reference to node color.
Definition rbNode.H:105
Color color
Node color: RED or BLACK.
Definition rbNode.H:94
void reset()
Reset node to red (for reinsertion)
Definition rbNode.H:111
RbNode_Data() noexcept
Default constructor creates a red node.
Definition rbNode.H:99
Color getColor() const
Get node color.
Definition rbNode.H:108
RbNode_Data(SentinelCtor) noexcept
Sentinel constructor creates a black node.
Definition rbNode.H:102
Declare RbNode type with 128-byte pool allocation.
Definition rbNode.H:116
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Specify tree node for a binary tree.
#define COLOR(p)
Access the color of node p.
Definition rbNode.H:74
int red_black_height(Node *p) noexcept
Black height of a red-black subtree, checking all its nodes.
Definition rbNode.H:164
bool is_red_black_bst(Node *node, Compare &cmp)
Verify that tree is both a valid RB tree and valid BST.
Definition rbNode.H:251
bool test_black_condition(Node *p, int &max, int bh=0)
Test the black-height property of an RB tree.
Definition rbNode.H:131
#define BLACK
Black color constant.
Definition rbNode.H:80
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:77
bool is_red_black_tree(Node *node)
Test RB properties for entire subtree.
Definition rbNode.H:234
unsigned char Color
Color type for RB nodes.
Definition rbNode.H:71
bool is_red_black(Node *node)
Test all Red-Black tree properties for a node.
Definition rbNode.H:203
#define RLINK(i, n)
#define LLINK(i, n)
Basic binary tree node definitions.
#define BLACK
Definition tpl_rbNode.H:54
#define RED
Definition tpl_rbNode.H:53