Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bin-node.cc
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
33
38# include <gtest/gtest.h>
39
40# include <string>
41
42# include <htlist.H>
43# include <tpl_binNode.H>
44
45using namespace std;
46using namespace testing;
47using namespace Aleph;
48
49namespace
50{
51 struct SentinelControl
52 {
53 SentinelControl() = default;
54
55 explicit SentinelControl(SentinelCtor) noexcept
56 { /* empty */
57 }
58
59 static void reset() noexcept
60 { /* empty */
61 }
62
63 static SentinelControl & get_data()
64 {
65 throw std::domain_error("SentinelControl has not data");
66 }
67 };
68}
69
71
73{
74 BinNode<int> p(10);
75 ASSERT_EQ(p.getL(), nullptr);
76 ASSERT_EQ(p.getR(), nullptr);
77 ASSERT_EQ(p.get_key(), 10);
78
79 BinNode<int> q = p; // test copy
80 ASSERT_EQ(q.getL(), nullptr);
81 ASSERT_EQ(q.getR(), nullptr);
82 ASSERT_EQ(q.get_key(), 10);
83}
84
91
93{
94 BinNode<int> p(10);
95 BinNode<int> left(5);
96 BinNode<int> right(15);
97 p.getL() = &left;
98 p.getR() = &right;
99
102
103 p.reset();
106}
107
115
117{
119 BinNode<int> p(10);
120 BinNode<int> child(5);
121
122 EXPECT_EQ(Tr::null(), BinNode<int>::NullPtr);
123 EXPECT_EQ(Tr::left(&p), BinNode<int>::NullPtr);
124 Tr::left(&p) = &child;
125 EXPECT_EQ(Tr::left(&p), &child);
126 EXPECT_EQ(Tr::key(&p), 10);
127
128 const BinNode<int> & cp = p;
129 EXPECT_EQ(Tr::left(&cp), &child);
130 EXPECT_EQ(Tr::key(&cp), 10);
131}
132
141
143{
144 BinNode<int> p(7);
145 int & k = p.get_key();
146 auto * node = BinNode<int>::key_to_node(k);
147 EXPECT_EQ(node, &p);
148}
149
151{
152 using N = TestSentinelNode<int>;
153 ASSERT_NE(N::NullPtr, nullptr);
154
155 N p(10);
156 EXPECT_EQ(p.getL(), N::NullPtr);
157 EXPECT_EQ(p.getR(), N::NullPtr);
158
159 const N cp(10);
160 EXPECT_EQ(LLINK(&cp), N::NullPtr);
161 EXPECT_EQ(RLINK(&cp), N::NullPtr);
162}
163
165{
166 BinNode<int> p(10);
167 BinNode<int> q(5);
168
169 p.getL() = &q;
170
171 ASSERT_EQ(p.getL(), &q);
172 ASSERT_EQ(p.getR(), nullptr);
173 ASSERT_EQ(q.getL(), nullptr);
174 ASSERT_EQ(q.getR(), nullptr);
175 ASSERT_EQ(p.getL()->get_key(), q.get_key());
176 ASSERT_EQ(p.get_key(), 10);
177 ASSERT_EQ(q.get_key(), 5);
178}
179
181{
182 BinNode<int> p(10);
183 BinNode<int> q(15);
184
185 p.getR() = &q;
186
187 ASSERT_EQ(p.getR(), &q);
188 ASSERT_EQ(p.getL(), nullptr);
189 ASSERT_EQ(q.getL(), nullptr);
190 ASSERT_EQ(q.getR(), nullptr);
191 ASSERT_EQ(p.getR()->get_key(), q.get_key());
192 ASSERT_EQ(p.get_key(), 10);
193 ASSERT_EQ(q.get_key(), 15);
194}
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:81
@ KEY
Definition btreepic.C:169
Node for binary search tree.
Key & get_key() noexcept
void reset() noexcept
BinNode *& getR() noexcept
BinNode *& getL() noexcept
#define TEST(name)
#define N
Definition fib.C:294
#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.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Basic binary tree node definitions.