Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
avlNodeRk.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 AVLNODERK_H
44# define AVLNODERK_H
45
46# include <tpl_binNodeUtils.H>
47
48namespace Aleph {
49
57{
58 signed char diff = 0; // balance factor: height(right) - height(left)
59 size_t count = 1; // number of nodes in subtree (including this node)
60
61public:
62
63 AvlNodeRk_Data() noexcept : diff(0), count(1) { /* empty */ }
64
65 AvlNodeRk_Data(SentinelCtor) noexcept : diff(0), count(0) { /* empty */ }
66
67 signed char & getDiff() noexcept { return diff; }
68
69 size_t & getCount() noexcept { return count; }
70
72 {
73 diff = 0;
74 count = 1;
75 }
76};
77
80
81# define DIFF(p) ((p)->getDiff())
82
88template <class Node>
90{
91 if (p == Node::NullPtr)
92 return true;
93
94 if (DIFF(p) < -1 or DIFF(p) > 1)
95 return false;
96
97 const int hL = computeHeightRec(LLINK(p));
98 const int hR = computeHeightRec(RLINK(p));
99
100 if (const int diff = hR - hL; diff > 1 or diff < -1)
101 return false;
102
103 if (static_cast<int>(DIFF(p)) != hR - hL)
104 return false;
105
106 // Verify counter
107 if (p->getCount() != LLINK(p)->getCount() + RLINK(p)->getCount() + 1)
108 return false;
109
110 if (not is_avl_rk(LLINK(p)))
111 return false;
112
113 return is_avl_rk(RLINK(p));
114}
115
116} // end namespace Aleph
117
118# endif // AVLNODERK_H
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:81
#define DIFF(p)
Access the balance factor of node p.
Definition avlNode.H:95
WeightedDigraph::Node Node
AVL node data with balance factor and subtree count.
Definition avlNodeRk.H:57
AvlNodeRk_Data(SentinelCtor) noexcept
Definition avlNodeRk.H:65
size_t & getCount() noexcept
Definition avlNodeRk.H:69
AvlNodeRk_Data() noexcept
Definition avlNodeRk.H:63
signed char & getDiff() noexcept
Definition avlNodeRk.H:67
void reset() noexcept
Definition avlNodeRk.H:71
Extended AVL node with subtree counter.
Definition avlNodeRk.H:79
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Specify tree node for a binary 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.
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool is_avl_rk(Node *p)
Verify if tree rooted at p is a valid AVL tree with correct counters.
Definition avlNodeRk.H:89
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Utility functions for binary tree operations.