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 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 <cstdlib>
34# include <ctime>
35# include <iostream>
36# include <string>
37# include <fstream>
38# include <aleph.H>
39# include <tpl_rb_tree.H>
40# include <tpl_binNodeUtils.H>
41
42# include <cassert>
43using namespace std;
44# include <cassert>
45using namespace Aleph;
46
47
48ofstream output;
49
50
51ofstream file;
52
53static void print_key(const Rb_Tree<int>::Node *p, int, int)
54{
55 file << p->get_key() << " ";
56}
57
58
59static void print_color(const Rb_Tree<int>::Node *p, int, int pos)
60{
61 if (COLOR(p) == RED)
62 file << pos << " ";
63}
64
65
66static void print_key_ex(const Rb_Tree<int>::Node *p, int, int)
67{
68 output << p->get_key() << " ";
69}
70
71
72static void print_color_ex(const Rb_Tree<int>::Node *p, int, int pos)
73{
74 if (COLOR(p) == RED)
75 output << pos << " ";
76}
77
78
79int main(int argc, char *argv[])
80{
81 int n = 1000;
82 unsigned int t = std::time(0);
83 int value;
84
85 if (argc > 1)
86 {
87 try { n = std::stoi(argv[1]); }
88 catch (const std::exception & e)
89 {
90 cerr << "Warning: could not parse n from '" << argv[1]
91 << "': " << e.what() << ". Using default n=1000" << endl;
92 }
93 }
94
95 if (argc > 2)
96 {
97 try { t = std::stoi(argv[2]); }
98 catch (const std::exception & e)
99 {
100 cerr << "Warning: could not parse t from '" << argv[2]
101 << "': " << e.what() << ". Using default t=" << t << endl;
102 }
103 }
104
105 if (n <= 0)
106 {
107 cout << "n must be positive" << endl;
108 return 1;
109 }
110
111 std::srand(t);
112
113 output.open("rb-example-aux.Tree", ios::out);
114 if (not output.is_open())
115 {
116 cerr << "Error: could not open rb-example-aux.Tree for writing" << endl;
117 return 1;
118 }
119
120 file.open("rb-tree-aux.Tree", ios::out);
121 if (not file.is_open())
122 {
123 cerr << "Error: could not open rb-tree-aux.Tree for writing" << endl;
124 return 1;
125 }
126
127 cout << "writeRb " << n << " " << t << endl;
128
129 {
130 Rb_Tree<int> tree;
131 Rb_Tree<int>::Node *node;
132 int i;
133
134 for (i = 0; i < 30; i++)
135 {
136 do
137 {
138 value = (int) (100.0*std::rand()/(RAND_MAX+1.0));
139 node = tree.search(value);
140 } while (node not_eq NULL);
141 node = new Rb_Tree<int>::Node (value);
142 tree.insert(node);
143 }
144
145 assert(is_red_black(tree.getRoot()));
146
148 output << endl << "START-SHADOW ";
150 output << endl;
151
152 destroyRec(tree.getRoot());
153 }
154
155 {
156 Rb_Tree<int> tree;
157 Rb_Tree<int>::Node *node;
158 int i;
159 for (i = 0; i < n; i++)
160 {
161 do
162 {
163 value = (int) (n*10.0*std::rand()/(RAND_MAX+1.0));
164 node = tree.search(value);
165 } while (node not_eq NULL);
166 node = new Rb_Tree<int>::Node (value);
167 tree.insert(node);
168 }
169
170 assert(is_red_black(tree.getRoot()));
171
173 file << "START-SHADOW ";
175 file << endl;
176
177 destroyRec(tree.getRoot());
178 }
179}
180
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
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.
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
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.
#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:72
ofstream file
Definition writeRb.C:51
static void print_color(const Rb_Tree< int >::Node *p, int, int pos)
Definition writeRb.C:59
static void print_key(const Rb_Tree< int >::Node *p, int, int)
Definition writeRb.C:53
ofstream output
Definition writeRb.C:48
static void print_key_ex(const Rb_Tree< int >::Node *p, int, int)
Definition writeRb.C:66