Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeRb.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 <cstdlib>
29# include <ctime>
30# include <iostream>
31# include <string>
32# include <fstream>
33# include <aleph.H>
34# include <tpl_rb_tree.H>
35# include <tpl_binNodeUtils.H>
36
37# include <cassert>
38using namespace std;
39# include <cassert>
40using namespace Aleph;
41
42
43ofstream output;
44
45
46ofstream file;
47
48static void print_key(const Rb_Tree<int>::Node *p, int, int)
49{
50 file << p->get_key() << " ";
51}
52
53
54static void print_color(const Rb_Tree<int>::Node *p, int, int pos)
55{
56 if (COLOR(p) == RED)
57 file << pos << " ";
58}
59
60
61static void print_key_ex(const Rb_Tree<int>::Node *p, int, int)
62{
63 output << p->get_key() << " ";
64}
65
66
67static void print_color_ex(const Rb_Tree<int>::Node *p, int, int pos)
68{
69 if (COLOR(p) == RED)
70 output << pos << " ";
71}
72
73
74int main(int argc, char *argv[])
75{
76 int n = 1000;
77 unsigned int t = std::time(0);
78 int value;
79
80 if (argc > 1)
81 {
82 try { n = std::stoi(argv[1]); }
83 catch (const std::exception & e)
84 {
85 cerr << "Warning: could not parse n from '" << argv[1]
86 << "': " << e.what() << ". Using default n=1000" << endl;
87 }
88 }
89
90 if (argc > 2)
91 {
92 try { t = std::stoi(argv[2]); }
93 catch (const std::exception & e)
94 {
95 cerr << "Warning: could not parse t from '" << argv[2]
96 << "': " << e.what() << ". Using default t=" << t << endl;
97 }
98 }
99
100 if (n <= 0)
101 {
102 cout << "n must be positive" << endl;
103 return 1;
104 }
105
106 std::srand(t);
107
108 output.open("rb-example-aux.Tree", ios::out);
109 if (not output.is_open())
110 {
111 cerr << "Error: could not open rb-example-aux.Tree for writing" << endl;
112 return 1;
113 }
114
115 file.open("rb-tree-aux.Tree", ios::out);
116 if (not file.is_open())
117 {
118 cerr << "Error: could not open rb-tree-aux.Tree for writing" << endl;
119 return 1;
120 }
121
122 cout << "writeRb " << n << " " << t << endl;
123
124 {
125 Rb_Tree<int> tree;
126 Rb_Tree<int>::Node *node;
127 int i;
128
129 for (i = 0; i < 30; i++)
130 {
131 do
132 {
133 value = (int) (100.0*std::rand()/(RAND_MAX+1.0));
134 node = tree.search(value);
135 } while (node not_eq NULL);
136 node = new Rb_Tree<int>::Node (value);
137 tree.insert(node);
138 }
139
140 assert(is_red_black(tree.getRoot()));
141
143 output << endl << "START-SHADOW ";
145 output << endl;
146
147 destroyRec(tree.getRoot());
148 }
149
150 {
151 Rb_Tree<int> tree;
152 Rb_Tree<int>::Node *node;
153 int i;
154 for (i = 0; i < n; i++)
155 {
156 do
157 {
158 value = (int) (n*10.0*std::rand()/(RAND_MAX+1.0));
159 node = tree.search(value);
160 } while (node not_eq NULL);
161 node = new Rb_Tree<int>::Node (value);
162 tree.insert(node);
163 }
164
165 assert(is_red_black(tree.getRoot()));
166
168 file << "START-SHADOW ";
170 file << endl;
171
172 destroyRec(tree.getRoot());
173 }
174}
175
Core header for the Aleph-w library.
Node * insert(Node *p) noexcept
Insert a node into the tree.
Node * search(const Key &key) const noexcept
Search for a key in the tree.
Node *& getRoot() noexcept
Get reference to root pointer.
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.
#define COLOR(p)
Definition quadnode.H:58
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:73
bool is_red_black(Node *node)
Test all Red-Black tree properties for a node.
Definition rbNode.H:160
Red-black tree with nodes without virtual destructor.
Utility functions for binary tree operations.
Red-Black tree implementation (bottom-up balancing).
static void print_color_ex(const Rb_Tree< int >::Node *p, int, int pos)
Definition writeRb.C:67
ofstream file
Definition writeRb.C:46
static void print_color(const Rb_Tree< int >::Node *p, int, int pos)
Definition writeRb.C:54
static void print_key(const Rb_Tree< int >::Node *p, int, int)
Definition writeRb.C:48
ofstream output
Definition writeRb.C:43
static void print_key_ex(const Rb_Tree< int >::Node *p, int, int)
Definition writeRb.C:61