Aleph-w 7.10.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
113 template <class Node>
114int checked_avl_height(Node* p) noexcept
115{
116 if (p == Node::NullPtr)
117 return 0;
118
119 const int hL = checked_avl_height(LLINK(p));
120 if (hL < 0)
121 return -1;
122
123 const int hR = checked_avl_height(RLINK(p));
124 if (hR < 0)
125 return -1;
126
127 // Heights differ by at most 1, and the stored factor matches reality
128 const int diff = hR - hL;
129 if (diff > 1 or diff < -1 or static_cast<int>(DIFF(p)) != diff)
130 return -1;
131
132 return 1 + (hL > hR ? hL : hR);
133}
134
159 template <class Node>
160bool is_avl(Node* p)
161{
162 return checked_avl_height(p) >= 0;
163}
164
165# endif // AVLNODE_H
int checked_avl_height(Node *p) noexcept
Height of an AVL subtree, checking all its nodes.
Definition avlNode.H:114
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
Definition avlNode.H:160
#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
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
#define RLINK(i, n)
#define LLINK(i, n)
Utility functions for binary tree operations.