Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testSplit.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
28# include <ctime>
29# include <iostream>
30# include <string>
31# include <aleph.H>
32# include <tpl_binTree.H>
33# include <tpl_binNodeUtils.H>
34
35# include <cstdlib>
36# include <cassert>
37using namespace std;
38# include <cstdlib>
39# include <cassert>
40using namespace Aleph;
41
42
43static void printNode(BinNode<int>* node)
44{
45 cout << node->get_key() << " ";
46}
47
48
49int main(int argc, char *argv[])
50{
51 int n = 1000;
52 if (argc > 1)
53 {
54 try { n = stoi(argv[1]); } catch (...) { n = 1000; }
55 }
56
57 unsigned int t = std::time(0);
58 if (argc > 2)
59 {
60 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
61 }
62
63 srand(t);
64
65 cout << argv[0] << " " << n << " " << t << endl;
66
67 BinTree<int> tree;
69 int value;
70 int ins_count = 0;
71
72 // Guard against tiny inputs that would make splitting impossible
73 if (n <= 2)
74 {
75 cerr << "Error: n must be greater than 2 for meaningful tree splitting." << endl;
76 return 1;
77 }
78
79 for (int i = 0; i < n; i++)
80 {
81 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
82 p = new BinNode<int> (value);
83 while (tree.insert(p) == NULL)
84 KEY(p) = (int) (10.0*n*rand()/(RAND_MAX+1.0));
85 ins_count++;
86 }
87
88 assert(check_bst(tree.getRoot()));
89
90 cout << ins_count << " keys inserted" << endl;
92 cout << endl;
93
95
96 do
97 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
98 while (not split_key_rec(tree.getRoot(), value, t1, t2));
99
100 cout << "Partition key: " << value << endl << endl;
101
102 cout << "Left Tree" << endl;
104 cout << endl;
105
106 cout << "Right Tree" << endl;
108 cout << endl;
109
110 destroyRec(t1);
111 destroyRec(t2);
112}
Core header for the Aleph-w library.
@ KEY
Definition btreepic.C:169
Node for binary search tree.
Key & get_key() noexcept
Node *& getRoot() noexcept
Return the root of tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept
Split recursively according to a key.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
void preOrderThreaded(Node *node, void(*visitFct)(Node *))
Traverse preorder a binary tree without recursion and without stack.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
STL namespace.
Binary search tree with nodes without virtual destructors,.
static void printNode(BinNode< int > *node)
Definition testSplit.C:43
Utility functions for binary tree operations.
Generic unbalanced binary search tree.