Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
avlNode.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
63# ifndef AVLNODE_H
64# define AVLNODE_H
65
66# include <tpl_binNodeUtils.H>
67
77{
78
79 signed char diff = 0;
80
81public:
82
84 AvlNode_Data() noexcept : diff(0) { /* EMPTY */ }
85
87 signed char & getDiff() noexcept { return diff; }
88
90 void reset() noexcept { diff = 0; }
91
92};
93
95# define DIFF(p) ((p)->getDiff())
96
99
122 template <class Node>
123bool is_avl(Node* p)
124{
125 if (p == Node::NullPtr)
126 return true;
127
128 // Balance factor must be in valid range
129 if (DIFF(p) < -1 or DIFF(p) > 1)
130 return false;
131
132 // Compute actual heights
133 const int hL = computeHeightRec(LLINK(p));
134 const int hR = computeHeightRec(RLINK(p));
135
136 // Verify AVL property: heights differ by at most 1
137 if (const int diff = hR - hL; diff > 1 or diff < -1)
138 return false;
139
140 // Verify stored balance factor matches reality
141 if (static_cast<int>(DIFF(p)) != hR - hL)
142 return false;
143
144 // Recursively check subtrees
145 if (not is_avl(LLINK(p)))
146 return false;
147
148 return is_avl(RLINK(p));
149}
150
151# endif // AVLNODE_H
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
Definition avlNode.H:123
#define DIFF(p)
Access the balance factor of node p.
Definition avlNode.H:95
WeightedDigraph::Node Node
Data portion of an AVL tree node.
Definition avlNode.H:77
signed char & getDiff() noexcept
Get reference to balance factor.
Definition avlNode.H:87
void reset() noexcept
Reset balance factor to 0 (for reuse)
Definition avlNode.H:90
signed char diff
Balance factor: height(right) - height(left)
Definition avlNode.H:79
AvlNode_Data() noexcept
Default constructor initializes balance to 0 (equal heights)
Definition avlNode.H:84
Declare AvlNode type with 40-byte pool allocation.
Definition avlNode.H:98
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
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.