Aleph-w 3.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
61# ifndef TPL_RBNODE_H
62# define TPL_RBNODE_H
63
64# include <tpl_binNode.H>
65
67typedef unsigned char Color;
68
70# define COLOR(p) ((p)->getColor())
71
73# define RED (0)
74
76# define BLACK (1)
77
87{
88private:
89
91
92public:
93
96
99
101 Color & getColor() { return color; }
102
104 void reset() { color = RED; }
105
106};
107
110
123 template <class Node>
124bool test_black_condition(Node *p, int &max, int bh = 0)
125{
126 if (p == Node::NullPtr)
127 return true;
128
129 if (COLOR(p) == BLACK)
130 bh++; // count this black node
131
132 if (LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr)
133 {
134 if (max == -1) // first leaf reached
135 max = bh;
136
137 return bh == max;
138 }
139
140 return test_black_condition(LLINK(p), max, bh) and
141 test_black_condition(RLINK(p), max, bh);
142}
143
156 template <class Node>
157bool is_red_black(Node * node)
158{
159 if (node == Node::NullPtr)
160 return true;
161
162 if (not (COLOR(node) == RED or COLOR(node) == BLACK))
163 return false;
164
165 // Red nodes cannot have red children
166 if (COLOR(node) == RED)
167 if (COLOR(LLINK(node)) == RED or COLOR(RLINK(node)) == RED)
168 return false;
169
170 int max = -1;
171
172 return test_black_condition(node, max, 0);
173}
174
184 template <class Node>
186{
187 if (not is_red_black(node))
188 return false;
189
190 if (not is_red_black(LLINK(node)) or not is_red_black(RLINK(node)))
191 return false;
192
193 return true;
194}
195
207 template <class Node, class Compare>
208bool is_red_black_bst(Node * node, Compare & cmp)
209{
210 if (not is_red_black_tree(node))
211 return false;
212
213 return check_bst(node, cmp);
214}
215# endif // TPL_RBNODE_H
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:81
WeightedDigraph::Node Node
Data portion of a Red-Black tree node.
Definition rbNode.H:87
Color & getColor()
Get reference to node color.
Definition rbNode.H:101
Color color
Node color: RED or BLACK.
Definition rbNode.H:90
RbNode_Data(SentinelCtor)
Sentinel constructor creates a black node.
Definition rbNode.H:98
void reset()
Reset node to red (for reinsertion)
Definition rbNode.H:104
RbNode_Data()
Default constructor creates a red node.
Definition rbNode.H:95
Declare RbNode type with 128-byte pool allocation.
Definition rbNode.H:109
__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:4110
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
#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:70
bool is_red_black_bst(Node *node, Compare &cmp)
Verify that tree is both a valid RB tree and valid BST.
Definition rbNode.H:208
bool test_black_condition(Node *p, int &max, int bh=0)
Test the black-height property of an RB tree.
Definition rbNode.H:124
#define BLACK
Black color constant.
Definition rbNode.H:76
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:73
bool is_red_black_tree(Node *node)
Test RB properties for entire subtree.
Definition rbNode.H:185
unsigned char Color
Color type for RB nodes.
Definition rbNode.H:67
bool is_red_black(Node *node)
Test all Red-Black tree properties for a node.
Definition rbNode.H:157
Basic binary tree node definitions.
#define BLACK
Definition tpl_rbNode.H:54
#define RED
Definition tpl_rbNode.H:53