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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <ctime>
28# include <cstdlib>
29# include <cassert>
30
31# include <iostream>
32# include <aleph.H>
33# include <tpl_binNodeUtils.H>
34# include <tpl_binNodeXt.H>
35
36using namespace Aleph;
37using namespace std;
38
40
41void print_node(Node * p, int, int)
42{
43 // cout << "(" << p->get_key() << "," << p->getCount() << ")" ;
44 cout << p->get_key() << " " ;
45}
46
47
48int main(int argc, char *argv[])
49{
50 int n = argc > 1 ? atoi(argv[1]) : 10;
51
52 unsigned int t = std::time(0);
53
54 if (argc > 2)
55 t = atoi(argv[2]);
56
57 srand(t);
58
59 cout << argv[0] << " " << n << " " << t << endl;
60
61 int i, value;
62 Node * root1 = Node::NullPtr;
63
64 for (i = 0; i < n; i++)
65 {
66 while (1)
67 {
68 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
69 if (searchInBinTree(root1, value) == Node::NullPtr)
70 break;
71 }
72 Node * p = new Node (value);
73 root1 = insert_root(root1, p);
74 }
75
76 assert(check_bst(root1));
77
78 cout << endl << "T1" << endl;
79 preOrderRec(root1, &print_node); cout << endl << endl;
80
81 Node * root2 = Node::NullPtr;
82 for (i = 0; i < n; i++)
83 {
84 while (1)
85 {
86 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
87 if (searchInBinTree(root1, value) == Node::NullPtr and
88 searchInBinTree(root2, value) == Node::NullPtr)
89 break;
90 }
91 Node * p = new Node (value);
92 root2 = insert_root(root2, p);
93 }
94
95 assert(check_bst(root2));
96
97 cout << endl << "T2" << endl;
98 preOrderRec(root2, &print_node); cout << endl;
99
100 Node * r1 = copyRec(root1);
101 Node * r2 = copyRec(root2);
102
103 Node * dup = Node::NullPtr;
104
105 Node * root = join(root1, root2, dup);
107 cout << endl << "Join(T1, T2)" << endl;
108 preOrderRec(root, &print_node); cout << endl << endl;
110 destroyRec(dup);
111 dup = Node::NullPtr; // Reinitialize dup to avoid dangling pointer
112
113 Node * res = join(r2, r1, dup); // renamed r to res to avoid confusion with r1/r2
115 cout << endl << "Join(T2, T1)" << endl;
116 preOrderRec(res, &print_node); cout << endl << endl;
118 destroyRec(dup);
119
120 {
122
123 for (i = 0; i < n; ++i)
124 {
125 insert_in_bst(t1, new Node (rand()));
126 insert_in_bst(t2, new Node (rand()));
127 }
128
129 cout << "t1: ";
130 preOrderRec(t1, &print_node); cout << endl << endl;
131
132 cout << "t2: ";
133 preOrderRec(t2, &print_node); cout << endl << endl;
134
136 Node * j = join(t1, t2, dup_inner);
137
138 cout << "join(t1, t2, dup): ";
139 preOrderRec(j, &print_node); cout << endl << endl;
140
141 cout << "dup: ";
143
144 destroyRec(j);
146 }
147}
Core header for the Aleph-w library.
WeightedDigraph::Node Node
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:4060
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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:39
void print_node(Node *p, int, int)
Definition testJoin.C:41
Utility functions for binary tree operations.
Extended binary node with subtree count.