Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
treapNode.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
64# ifndef TREAPNODE_H
65# define TREAPNODE_H
66
67# include <limits.h>
68# include <tpl_binNode.H>
69
70using namespace Aleph;
71
72namespace Aleph {
73
75const long Max_Priority = ULONG_MAX;
76
78const long Min_Priority = 0;
79
89{
90 unsigned long priority;
91
92public:
93
96
99
101 unsigned long & getPriority() noexcept { return priority; }
102
104 void reset() noexcept { /* empty */ }
105};
106
109
119template <class Node>
120unsigned long & PRIO(Node * p) noexcept { return p->getPriority(); }
121
143 template <class Node>
144bool is_treap(Node * root) noexcept
145{
146 if (root == Node::NullPtr)
147 return true;
148
149 // Parent priority must be ≤ children's priorities
150 if (PRIO(root) > PRIO(LLINK(root)) or PRIO(root) > PRIO(RLINK(root)))
151 return false; // heap property violation
152
154}
155
156} // end namespace Aleph
157
158# endif // TREAPNODE_H
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:81
WeightedDigraph::Node Node
Data portion of a treap node.
Definition treapNode.H:89
unsigned long priority
Random priority for heap property.
Definition treapNode.H:90
void reset() noexcept
Reset (no-op for treap nodes)
Definition treapNode.H:104
TreapNode_Data() noexcept
Default constructor sets maximum priority.
Definition treapNode.H:95
TreapNode_Data(SentinelCtor) noexcept
Sentinel constructor also uses maximum priority.
Definition treapNode.H:98
unsigned long & getPriority() noexcept
Get reference to priority value.
Definition treapNode.H:101
Declare TreapNode type with 80-byte pool allocation.
Definition treapNode.H:108
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
#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.
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
Definition treapNode.H:120
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_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
const long Max_Priority
Maximum priority value (used for sentinel/leaves)
Definition treapNode.H:75
const long Min_Priority
Minimum priority value.
Definition treapNode.H:78
DynList< T > maps(const C &c, Op op)
Classic map operation.
Basic binary tree node definitions.