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 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 <aleph.H>
31# include <tpl_dynArray.H>
32# include <tpl_binNode.H>
33# include <tpl_binNodeUtils.H>
34# include <string>
35
36# include <cstdlib>
37# include <cassert>
38using namespace std;
39# include <cstdlib>
40# include <cassert>
41using namespace Aleph;
42
43
44static void printNode(BinNode<int>* node)
45{
46 cout << node->get_key() << " ";
47}
48
49
50int main(int argc, char * argv[])
51{
52 int i;
53 int n = 1000;
54 if (argc > 1)
55 {
56 try { n = stoi(argv[1]); } catch (...) { n = 1000; }
57 }
58
59 if (n <= 0)
60 {
61 cerr << "n must be positive" << endl;
62 return 1;
63 }
64
65 unsigned int t = std::time(0);
66
67 if (argc > 2)
68 {
69 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
70 }
71
72 srand(t);
73
74 DynArray<int> array;
75
76 cout << argv[0] << " " << n << " " << t << endl;
77
78 BinNode<int> *root = new BinNode<int> ((int) (10.0*n*rand()/(RAND_MAX+1.0)));
79 cout << root->get_key() << " ";
80
81 array[0] = root->get_key();
82
83 BinNode<int> *p;
84 int value;
85 int ins_count = 1;
86
87 for (i = 1; i < n; i++)
88 {
89 do
90 {
91 value = (int) (10.0*n*rand()/(RAND_MAX+1.0));
92 p = searchInBinTree(root, value);
93 }
94 while (p != NULL);
95
96 p = new BinNode<int> (value);
97 cout << value << " ";
98 root = insert_root(root, p);
99
100 array[i] = value;
101
102 ins_count++;
103 }
104
105 cout << endl << ins_count << " items inserted" << endl;
107
108 cout << "prefix: ";
110 cout << endl;
111
112 int removals = (n < 10) ? n : 10;
113 for (i = 0; i < removals; i++)
114 {
115 int idx;
116 do
117 {
118 idx = (int) (1.0*n*rand()/(RAND_MAX+1.0));
119
120 value = array[idx];
121
122 p = searchInBinTree(root, value);
123
124 }
125 while (p == NULL);
126
127 cout << value << " ";
128
129 p = remove_from_bst(root, value);
130
131 assert(p != NULL);
132 delete p; // Free the detached node to prevent memory leak
133 }
134
135 cout << endl;
137 cout << endl;
139}
Core header for the Aleph-w library.
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:4060
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
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.
static void printNode(BinNode< int > *node)
Utility functions for binary tree operations.
Basic binary tree node definitions.
Lazy and scalable dynamic array implementation.