Aleph-w
3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tree-node-common.H
Go to the documentation of this file.
1
2
3
/*
4
Aleph_w
5
6
Data structures & Algorithms
7
version 2.0.0b
8
https://github.com/lrleon/Aleph-w
9
10
This file is part of Aleph-w library
11
12
Copyright (c) 2002-2026 Leandro Rabindranath Leon
13
14
Permission is hereby granted, free of charge, to any person obtaining a copy
15
of this software and associated documentation files (the "Software"), to deal
16
in the Software without restriction, including without limitation the rights
17
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
copies of the Software, and to permit persons to whom the Software is
19
furnished to do so, subject to the following conditions:
20
21
The above copyright notice and this permission notice shall be included in all
22
copies or substantial portions of the Software.
23
24
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
SOFTWARE.
31
*/
32
33
34
# ifndef TREE_NODE_COMMON_H
35
# define TREE_NODE_COMMON_H
36
37
# include <gtest/gtest.h>
38
39
# include <
tpl_tree_node.H
>
40
41
using namespace
testing;
42
43
44
/* Build:
45
0
46
47
1 2 3 4 5
48
49
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
50
51
*/
52
Tree_Node<int>
*
sample_tree
(
size_t
num_nodes_by_subtree
,
int
key)
53
{
54
Tree_Node<int>
*
root
=
new
Tree_Node<int>
(key++);
55
for
(
size_t
i = 0; i <
num_nodes_by_subtree
; ++i, ++key)
56
root
->insert_rightmost_child(
new
Tree_Node<int>
(key));
57
58
for
(
Tree_Node<int>::Children_Iterator
it(
root
); it.
has_curr
(); it.next())
59
{
60
auto
ptr = it.get_curr();
61
for
(
size_t
i = 0; i <
num_nodes_by_subtree
; ++i, ++key)
62
ptr->insert_rightmost_child(
new
Tree_Node<int>
(key));
63
}
64
return
root
;
65
}
66
67
68
// preorder traversal of sample_tree(5, 0)
69
DynList<int>
l
= { 0, 1, 6, 7, 8, 9, 10, 2, 11, 12, 13, 14, 15, 3, 16, 17,
70
18, 19, 20, 4, 21, 22, 23, 24, 25, 5, 26, 27, 28, 29, 30 };
71
72
struct
Simple_Tree
:
public
Test
73
{
74
Tree_Node<int>
*
root
=
nullptr
;
75
Simple_Tree
() :
root
(
sample_tree
(5, 0)) {}
76
~Simple_Tree
() {
destroy_tree
(
root
); }
77
};
78
79
80
struct
Three_Trees
:
public
Test
81
{
82
Tree_Node<int>
*
root1
=
nullptr
;
83
Tree_Node<int>
*
root2
=
nullptr
;
84
Tree_Node<int>
*
root3
=
nullptr
;
85
86
const
DynList<int>
l1
= { 0, 1, 6, 7, 8, 9, 10, 2, 11, 12, 13, 14, 15, 3,
87
16, 17, 18, 19, 20, 4, 21, 22, 23, 24, 25, 5, 26,
88
27, 28, 29, 30 };
89
90
const
DynList<int>
l2
= { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43 };
91
92
const
DynList<int>
l3
= { 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
93
57, 58, 59, 60, 61, 62, 63, 64 };
94
95
Three_Trees
()
96
:
root1
(
sample_tree
(5, 0)),
root2
(
sample_tree
(3, 31)),
97
root3
(
sample_tree
(4, 44)) {}
98
~Three_Trees
()
99
{
100
destroy_tree
(
root1
);
101
destroy_tree
(
root2
);
102
destroy_tree
(
root3
);
103
}
104
};
105
106
107
# endif
Aleph::DynList
Dynamic singly linked list with functional programming support.
Definition
htlist.H:1423
Aleph::Tree_Node::Children_Iterator
Iterator over the children of this.
Definition
tpl_tree_node.H:610
Aleph::Tree_Node::Children_Iterator::has_curr
bool has_curr() const noexcept
Definition
tpl_tree_node.H:627
Aleph::Tree_Node
Generic m-ary trees.
Definition
tpl_tree_node.H:89
root
__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
Aleph::destroy_tree
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Definition
tpl_tree_node.H:1003
Aleph::maps
DynList< T > maps(const C &c, Op op)
Classic map operation.
Definition
ahFunctional.H:693
Simple_Tree
Definition
tree-node-common.H:73
Simple_Tree::~Simple_Tree
~Simple_Tree()
Definition
tree-node-common.H:76
Simple_Tree::root
Tree_Node< int > * root
Definition
tree-node-common.H:74
Simple_Tree::Simple_Tree
Simple_Tree()
Definition
tree-node-common.H:75
Three_Trees
Definition
tree-node-common.H:81
Three_Trees::root1
Tree_Node< int > * root1
Definition
tree-node-common.H:82
Three_Trees::Three_Trees
Three_Trees()
Definition
tree-node-common.H:95
Three_Trees::l2
const DynList< int > l2
Definition
tree-node-common.H:90
Three_Trees::~Three_Trees
~Three_Trees()
Definition
tree-node-common.H:98
Three_Trees::l3
const DynList< int > l3
Definition
tree-node-common.H:92
Three_Trees::l1
const DynList< int > l1
Definition
tree-node-common.H:86
Three_Trees::root2
Tree_Node< int > * root2
Definition
tree-node-common.H:83
Three_Trees::root3
Tree_Node< int > * root3
Definition
tree-node-common.H:84
tpl_tree_node.H
General tree (n-ary tree) node.
sample_tree
Tree_Node< int > * sample_tree(size_t num_nodes_by_subtree, int key)
Definition
tree-node-common.H:52
l
DynList< int > l
Definition
tree-node-common.H:69
Tests
tree-node-common.H
Generated by
1.9.8