Aleph-w 3.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
71
72 Color & getColor() noexcept { return color; }
73
74 size_t & getCount() noexcept { return count; }
75
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 if (p == Node::NullPtr)
98 return true;
99
100 if (COLOR(p) == BLACK)
101 bh++;
102
103 if (LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr)
104 {
105 if (max == -1)
106 max = bh;
107
108 return bh == max;
109 }
110
113}
114
120template <class Node>
122{
123 if (node == Node::NullPtr)
124 return true;
125
126 if (not (COLOR(node) == RED or COLOR(node) == BLACK))
127 return false;
128
129 if (COLOR(node) == RED)
130 if (COLOR(LLINK(node)) == RED or COLOR(RLINK(node)) == RED)
131 return false;
132
133 int max = -1;
134
135 return test_black_condition_rk(node, max, 0);
136}
137
143template <class Node>
145{
146 if (node == Node::NullPtr)
147 return true;
148
149 // Check counter
150 if (node->getCount() != LLINK(node)->getCount() + RLINK(node)->getCount() + 1)
151 return false;
152
153 if (not is_red_black_rk(node))
154 return false;
155
158 return false;
159
160 return true;
161}
162
169template <class Node, class Compare>
170bool is_red_black_bst_rk(Node * node, Compare & cmp)
171{
172 if (not is_red_black_tree_rk(node))
173 return false;
174
175 return check_bst(node, cmp);
176}
177
178} // end namespace Aleph
179
180# endif // RBNODERK_H
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:81
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: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.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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:170
bool is_red_black_tree_rk(Node *node)
Verify if tree is a valid red-black tree with correct counters.
Definition rbNodeRk.H:144
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:121
DynList< T > maps(const C &c, Op op)
Classic map operation.
unsigned char Color
Definition rbNodeRk.H:50
#define COLOR(p)
Definition quadnode.H:58
#define BLACK
Black color constant.
Definition rbNode.H:76
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:73
Basic binary tree node definitions.