Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testJoin.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 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# include <ctime>
33# include <cstdlib>
34# include <cassert>
35
36# include <iostream>
37# include <aleph.H>
38# include <tpl_binNodeUtils.H>
39# include <tpl_binNodeXt.H>
40
41using namespace Aleph;
42using namespace std;
43
45
46void print_node(Node * p, int, int)
47{
48 // cout << "(" << p->get_key() << "," << p->getCount() << ")" ;
49 cout << p->get_key() << " " ;
50}
51
52
53int main(int argc, char *argv[])
54{
55 int n = argc > 1 ? atoi(argv[1]) : 10;
56
57 unsigned int t = std::time(0);
58
59 if (argc > 2)
60 t = atoi(argv[2]);
61
62 srand(t);
63
64 cout << argv[0] << " " << n << " " << t << endl;
65
66 int i, value;
67 Node * root1 = Node::NullPtr;
68
69 for (i = 0; i < n; i++)
70 {
71 while (1)
72 {
73 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
74 if (searchInBinTree(root1, value) == Node::NullPtr)
75 break;
76 }
77 Node * p = new Node (value);
78 root1 = insert_root(root1, p);
79 }
80
81 assert(check_bst(root1));
82
83 cout << endl << "T1" << endl;
84 preOrderRec(root1, &print_node); cout << endl << endl;
85
86 Node * root2 = Node::NullPtr;
87 for (i = 0; i < n; i++)
88 {
89 while (1)
90 {
91 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
94 break;
95 }
96 Node * p = new Node (value);
97 root2 = insert_root(root2, p);
98 }
99
100 assert(check_bst(root2));
101
102 cout << endl << "T2" << endl;
103 preOrderRec(root2, &print_node); cout << endl;
104
105 Node * r1 = copyRec(root1);
106 Node * r2 = copyRec(root2);
107
108 Node * dup = Node::NullPtr;
109
110 Node * root = join(root1, root2, dup);
112 cout << endl << "Join(T1, T2)" << endl;
113 preOrderRec(root, &print_node); cout << endl << endl;
115 destroyRec(dup);
116 dup = Node::NullPtr; // Reinitialize dup to avoid dangling pointer
117
118 Node * res = join(r2, r1, dup); // renamed r to res to avoid confusion with r1/r2
120 cout << endl << "Join(T2, T1)" << endl;
121 preOrderRec(res, &print_node); cout << endl << endl;
123 destroyRec(dup);
124
125 {
127
128 for (i = 0; i < n; ++i)
129 {
130 insert_in_bst(t1, new Node (rand()));
131 insert_in_bst(t2, new Node (rand()));
132 }
133
134 cout << "t1: ";
135 preOrderRec(t1, &print_node); cout << endl << endl;
136
137 cout << "t2: ";
138 preOrderRec(t2, &print_node); cout << endl << endl;
139
141 Node * j = join(t1, t2, dup_inner);
142
143 cout << "join(t1, t2, dup): ";
144 preOrderRec(j, &print_node); cout << endl << endl;
145
146 cout << "dup: ";
148
149 destroyRec(j);
151 }
152}
Core header for the Aleph-w library.
WeightedDigraph::Node Node
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node for binary search tree.
Key & get_key() noexcept
static BinNode *const NullPtr
__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:4071
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
Node * copyRec(Node *root)
Copy recursively a tree.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Node * insert_in_bst(Node *&r, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node p in a binary search tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Search a key in a binary search tree.
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
STL namespace.
BinNode< int > Node
Definition testJoin.C:44
void print_node(Node *p, int, int)
Definition testJoin.C:46
Utility functions for binary tree operations.
Extended binary node with subtree count.