Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
rbNodeRk.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
43# ifndef RBNODERK_H
44# define RBNODERK_H
45
46# include <tpl_binNode.H>
47
48namespace Aleph {
49
50typedef unsigned char Color;
51# define COLOR(p) ((p)->getColor())
52# define RED (0)
53# define BLACK (1)
54
62{
63 Color color; // RED or BLACK
64 size_t count; // number of nodes in subtree (including this node)
65
66public:
67
68 RbNodeRk_Data() noexcept : color(RED), count(1) { /* empty */ }
69
70 RbNodeRk_Data(SentinelCtor) noexcept : color(BLACK), count(0) { /* empty */ }
71
72 Color & getColor() noexcept { return color; }
73
74 size_t & getCount() noexcept { return count; }
75
76 void reset() noexcept
77 {
78 color = RED;
79 count = 1;
80 }
81};
82
85
86
94template <class Node>
95bool test_black_condition_rk(Node *p, int &max, int bh = 0)
96{
97 // A path ends at NullPtr, not at a leaf: a node with a single child
98 // also ends one, through its missing child.
99 if (p == Node::NullPtr)
100 {
101 if (max == -1)
102 max = bh;
103
104 return bh == max;
105 }
106
107 if (COLOR(p) == BLACK)
108 bh++;
109
110 return test_black_condition_rk(LLINK(p), max, bh) and
112}
113
126template <class Node>
127int red_black_height_rk(Node *p) noexcept
128{
129 if (p == Node::NullPtr)
130 return 0;
131
132 if (p->getCount() != LLINK(p)->getCount() + RLINK(p)->getCount() + 1)
133 return -1;
134
135 if (not (COLOR(p) == RED or COLOR(p) == BLACK))
136 return -1;
137
138 if (COLOR(p) == RED)
139 {
140 if (LLINK(p) != Node::NullPtr and COLOR(LLINK(p)) == RED)
141 return -1;
142 if (RLINK(p) != Node::NullPtr and COLOR(RLINK(p)) == RED)
143 return -1;
144 }
145
146 const int left_height = red_black_height_rk(LLINK(p));
147 if (left_height < 0)
148 return -1;
149
150 if (red_black_height_rk(RLINK(p)) != left_height)
151 return -1;
152
153 return left_height + (COLOR(p) == BLACK ? 1 : 0);
154}
155
161template <class Node>
163{
164 if (node == Node::NullPtr)
165 return true;
166
167 if (not (COLOR(node) == RED or COLOR(node) == BLACK))
168 return false;
169
170 if (COLOR(node) == RED)
171 if (COLOR(LLINK(node)) == RED or COLOR(RLINK(node)) == RED)
172 return false;
173
174 int max = -1;
175
176 return test_black_condition_rk(node, max, 0);
177}
178
188template <class Node>
190{
191 return red_black_height_rk(node) >= 0;
192}
193
200template <class Node, class Compare>
201bool is_red_black_bst_rk(Node * node, Compare & cmp)
202{
203 if (not is_red_black_tree_rk(node))
204 return false;
205
206 return check_bst(node, cmp);
207}
208
209} // end namespace Aleph
210
211# endif // RBNODERK_H
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:83
WeightedDigraph::Node Node
Red-Black node data with color and subtree count.
Definition rbNodeRk.H:62
void reset() noexcept
Definition rbNodeRk.H:76
RbNodeRk_Data(SentinelCtor) noexcept
Definition rbNodeRk.H:70
size_t & getCount() noexcept
Definition rbNodeRk.H:74
Color & getColor() noexcept
Definition rbNodeRk.H:72
RbNodeRk_Data() noexcept
Definition rbNodeRk.H:68
Extended Red-Black node with subtree counter.
Definition rbNodeRk.H:84
__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.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Main namespace for Aleph-w library functions.
bool is_red_black_bst_rk(Node *node, Compare &cmp)
Verify that tree rooted at node is a valid red-black BST with counters.
Definition rbNodeRk.H:201
bool is_red_black_tree_rk(Node *node)
Verify if tree is a valid red-black tree with correct counters.
Definition rbNodeRk.H:189
int red_black_height_rk(Node *p) noexcept
Black height of a ranked red-black subtree, checking all its nodes.
Definition rbNodeRk.H:127
and
Check uniqueness with explicit hash + equality functors.
bool test_black_condition_rk(Node *p, int &max, int bh=0)
Test the black height condition for a red-black tree.
Definition rbNodeRk.H:95
bool is_red_black_rk(Node *node)
Verify if tree rooted at node is a valid red-black tree.
Definition rbNodeRk.H:162
unsigned char Color
Definition rbNodeRk.H:50
#define COLOR(p)
Definition quadnode.H:59
#define BLACK
Black color constant.
Definition rbNode.H:80
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:77
#define RLINK(i, n)
#define LLINK(i, n)
Basic binary tree node definitions.