Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testInsertRoot.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 <aleph.H>
36# include <tpl_dynArray.H>
37# include <tpl_binNode.H>
38# include <tpl_binNodeUtils.H>
39# include <string>
40
41# include <cstdlib>
42# include <cassert>
43using namespace std;
44# include <cstdlib>
45# include <cassert>
46using namespace Aleph;
47
48
49static void printNode(BinNode<int>* node)
50{
51 cout << node->get_key() << " ";
52}
53
54
55int main(int argc, char * argv[])
56{
57 int i;
58 int n = 1000;
59 if (argc > 1)
60 {
61 try { n = stoi(argv[1]); } catch (...) { n = 1000; }
62 }
63
64 if (n <= 0)
65 {
66 cerr << "n must be positive" << endl;
67 return 1;
68 }
69
70 unsigned int t = std::time(0);
71
72 if (argc > 2)
73 {
74 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
75 }
76
77 srand(t);
78
79 DynArray<int> array;
80
81 cout << argv[0] << " " << n << " " << t << endl;
82
83 BinNode<int> *root = new BinNode<int> ((int) (10.0*n*rand()/(RAND_MAX+1.0)));
84 cout << root->get_key() << " ";
85
86 array[0] = root->get_key();
87
88 BinNode<int> *p;
89 int value;
90 int ins_count = 1;
91
92 for (i = 1; i < n; i++)
93 {
94 do
95 {
96 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
98 }
99 while (p != NULL);
100
101 p = new BinNode<int> (value);
102 cout << value << " ";
103 root = insert_root(root, p);
104
105 array[i] = value;
106
107 ins_count++;
108 }
109
110 cout << endl << ins_count << " items inserted" << endl;
112
113 cout << "prefix: ";
115 cout << endl;
116
117 int removals = (n < 10) ? n : 10;
118 for (i = 0; i < removals; i++)
119 {
120 int idx;
121 do
122 {
123 idx = (int) (1.0*n*rand()/(RAND_MAX+1.0));
124
125 value = array[idx];
126
128
129 }
130 while (p == NULL);
131
132 cout << value << " ";
133
135
136 assert(p != NULL);
137 delete p; // Free the detached node to prevent memory leak
138 }
139
140 cout << endl;
142 cout << endl;
144}
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node for binary search tree.
Key & get_key() noexcept
__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
Node * remove_from_bst(Node *&root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Remove a key from a binary search tree.
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
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
STL namespace.
static void printNode(BinNode< int > *node)
Utility functions for binary tree operations.
Basic binary tree node definitions.
Lazy and scalable dynamic array implementation.