Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
avlNode.H File Reference

AVL tree node with balance factor. More...

#include <tpl_binNodeUtils.H>
Include dependency graph for avlNode.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  AvlNode_Data
 Data portion of an AVL tree node. More...
 
class  AvlNode< Key >
 Declare AvlNode type with 40-byte pool allocation. More...
 
class  AvlNodeVtl< Key >
 

Macros

#define DIFF(p)   ((p)->getDiff())
 Access the balance factor of node p.
 

Functions

template<class Node >
int checked_avl_height (Node *p) noexcept
 Height of an AVL subtree, checking all its nodes.
 
template<class Node >
bool is_avl (Node *p)
 Validate that a tree satisfies AVL properties.
 

Detailed Description

AVL tree node with balance factor.

This file defines the AvlNode structure for AVL trees, including the balance factor (height difference) attribute and validation functions.

AVL Tree Properties

An AVL tree maintains the invariant that for every node: |height(left) - height(right)| ≤ 1

The balance factor (diff) stores: height(right) - height(left) Valid values are -1, 0, or +1.

Balance Factor Meaning

diff Meaning
-1 Left subtree is 1 taller
0 Both subtrees have equal height
+1 Right subtree is 1 taller
See also
tpl_avl.H AVL tree implementation
rbNode.H Red-Black tree node (alternative balancing)
Author
Leandro Rabindranath León

Definition in file avlNode.H.

Macro Definition Documentation

◆ DIFF

#define DIFF (   p)    ((p)->getDiff())

Access the balance factor of node p.

Definition at line 95 of file avlNode.H.

Function Documentation

◆ checked_avl_height()

template<class Node >
int checked_avl_height ( Node *  p)
noexcept

Height of an AVL subtree, checking all its nodes.

In one traversal, checks at every node of the subtree rooted at p that the heights of its two subtrees differ by at most one and that its stored balance factor is that difference.

Template Parameters
NodeAVL node type
Parameters
pRoot of subtree to validate
Returns
the height of the subtree, or -1 if a node violates the AVL properties
Note
O(n).

Definition at line 114 of file avlNode.H.

References checked_avl_height(), Aleph::diff(), DIFF, LLINK, and RLINK.

Referenced by checked_avl_height(), and is_avl().

◆ is_avl()

template<class Node >
bool is_avl ( Node *  p)

Validate that a tree satisfies AVL properties.

Checks that:

  • The stored balance factor matches actual height difference
  • The balance factor is in range [-1, +1]
  • All subtrees also satisfy AVL properties

It does not look at the keys; Gen_Avl_Tree::verify() also checks their order.

Template Parameters
NodeAVL node type
Parameters
pRoot of subtree to validate
Returns
true if tree is a valid AVL tree
Note
O(n).
Example
Avl_Tree<int> tree;
// ... insertions ...
assert(is_avl(tree.getRoot()));
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
Definition avlNode.H:160

Definition at line 160 of file avlNode.H.

References checked_avl_height().

Referenced by main(), TEST(), TEST(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::verify().