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 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# include <ctime>
34# include <iostream>
35# include <string>
36# include <aleph.H>
37# include <tpl_binTree.H>
38# include <tpl_binNodeUtils.H>
39
40# include <cstdlib>
41# include <cassert>
42using namespace std;
43# include <cstdlib>
44# include <cassert>
45using namespace Aleph;
46
47
48static void printNode(BinNode<int>* node)
49{
50 cout << node->get_key() << " ";
51}
52
53
54int main(int argc, char *argv[])
55{
56 int n = 1000;
57 if (argc > 1)
58 {
59 try { n = stoi(argv[1]); } catch (...) { n = 1000; }
60 }
61
62 unsigned int t = std::time(0);
63 if (argc > 2)
64 {
65 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
66 }
67
68 srand(t);
69
70 cout << argv[0] << " " << n << " " << t << endl;
71
72 BinTree<int> tree;
74 int value;
75 int ins_count = 0;
76
77 // Guard against tiny inputs that would make splitting impossible
78 if (n <= 2)
79 {
80 cerr << "Error: n must be greater than 2 for meaningful tree splitting." << endl;
81 return 1;
82 }
83
84 for (int i = 0; i < n; i++)
85 {
86 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
87 p = new BinNode<int> (value);
88 while (tree.insert(p) == NULL)
89 KEY(p) = (int) (10.0*n*rand()/(RAND_MAX+1.0));
90 ins_count++;
91 }
92
93 assert(check_bst(tree.getRoot()));
94
95 cout << ins_count << " keys inserted" << endl;
97 cout << endl;
98
100
101 do
102 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
103 while (not split_key_rec(tree.getRoot(), value, t1, t2));
104
105 cout << "Partition key: " << value << endl << endl;
106
107 cout << "Left Tree" << endl;
109 cout << endl;
110
111 cout << "Right Tree" << endl;
113 cout << endl;
114
115 destroyRec(t1);
116 destroyRec(t2);
117}
Core header for the Aleph-w library.
int main()
@ KEY
Definition btreepic.C:169
size_t size_t int32_t value
Definition ca-c-api.h:116
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.
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
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
STL namespace.
Binary search tree with nodes without virtual destructors,.
static void printNode(BinNode< int > *node)
Definition testSplit.C:48
Utility functions for binary tree operations.
Generic unbalanced binary search tree.