Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fmmd.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
39# include<tpl_tree.H>
40# include<tpl_binNode.H>
41# include<tpl_dlist.H>
42# include<fstream>
43
44
45 template <class Key>
46BinNode<Key> * forest_to_bin(Tree_Node<Key> * root)
47{
48 if (root == nullptr)
49 return nullptr;
50
51 BinNode<Key> * result = new BinNode<Key> (root->get_data());
52
53 result->getL() = forest_to_bin(root->get_left_child());
54 result->getR() = forest_to_bin(root->get_right_sibling());
55
56 return result;
57}
58
59
60 template <class Key>
61void insert_child(BinNode<Key> * bin_node, Tree_Node<Key> *& tree)
62{
63 if (bin_node == nullptr)
64 return;
65
66 Tree_Node<Key> * child = new Tree_Node<Key>(KEY(bin_node));
67
68 tree->insert_leftmost_child(child);
69}
70
71
72 template <class Key>
73void insert_sibling(BinNode<Key> * bin_node, Tree_Node<Key> *& tree)
74{
75 if (bin_node == nullptr)
76 return;
77
78 Tree_Node<Key> * sibling = new Tree_Node<Key>(KEY(bin_node));
79
80 tree->insert_right_sibling(sibling);
81}
82
83
84 template <class Key>
85void bin_to_tree(BinNode<Key> * bn, Tree_Node<Key> *& tree)
86{
87 if (bn == nullptr)
88 return;
89
90 insert_child(LLINK(bn), tree);
91 Tree_Node<Key> * left_child = tree->get_left_child();
92 bin_to_tree(LLINK(bn), left_child);
93
94 insert_sibling(RLINK(bn), tree);
95 Tree_Node<Key> * right_sibling = tree->get_right_sibling();
96 bin_to_tree(RLINK(bn), right_sibling);
97}
98
99
100 template <class Key> Tree_Node<Key> *
102{
103 if (bn == nullptr)
104 return nullptr;
105
106 Tree_Node<Key> * tree = new Tree_Node<Key> (KEY(bn));
107
108 bin_to_tree(bn, tree);
109
110 return tree;
111}
112
114{
115 unsigned int n1 : 4;
116 unsigned int n2 : 4;
117};
118
119char nibble_to_char(unsigned char c)
120{
121 return c < 10 ? c+ '0' : 'A' + c -10;
122}
123
124char * stringficate(void *src_data, size_t src_size,char * result)
125{
126 Two_Nibbles two_nibbles;
127
128 char * ret_val = result;
129 char * src_str= static_cast<char *> (src_data);
130
131 for(int i=0;i<src_size; i++)
132 {
133 two_nibbles= * reinterpret_cast<Two_Nibbles*>(src_str);
134 src_str++;
135 *result++ = nibble_to_char(two_nibbles.n2);
136 *result++ = nibble_to_char(two_nibbles.n1);
137 }
138 *result = '\0';
139 return ret_val;
140}
141
142int char_to_nibble(unsigned char c)
143{
144 return c < 58 ? c - '0' : c -'A' + 10;
145}
146
147
148void destringficate(void *result, size_t src_size, char *src_data){
149
150 Two_Nibbles two_nibbles;
151 char * result_str = static_cast<char*> (result);
152
153 for(int i=0;i<src_size; i++)
154 {
155 two_nibbles.n2 = char_to_nibble(*src_data);
156 src_data++;
157 two_nibbles.n1 = char_to_nibble(*src_data);
158 src_data++;
159 char *character = reinterpret_cast<char*>(&two_nibbles);
160 *result_str++ = *character;
161 }
162}
163
164 template <class T>
165struct pre_in{
168};
169
170 template <class Node,class T> inline static
171void get_pre_in_list(Node* node,Dlist<T> & l,int &pos)
172{
173
174 if (node == Node::NullPtr)
175 return;
176
177 T pi;
178 pi.data=KEY(node);
179 Dnode<T> * lnodo = new Dnode<T>(pi);
180 l.append(lnodo);
181
182 get_pre_in_list(LLINK(node),l,pos);
183
184 lnodo->get_data().inorder_pos=pos;
185 pos++;
186
187 get_pre_in_list(RLINK(node),l,pos);
188}
189
190 template <class Key> void
191write_to_stream(BinNode<Key> * root, ofstream & output_stream)
192{
193 if( root == nullptr)
194 return;
195 Dlist<pre_in<Key> > l;
196 int cardinality=0;
197 char buffer[1024];
198 size_t size= sizeof(KEY(root));
199
200 get_pre_in_list(root,l,cardinality);
201 output_stream<<cardinality<<endl;
202 output_stream<<size<<endl;
203
204 Dlist<pre_in<Key> >::Iterator a(l);
205 for(a.reset_first(); a.has_curr(); a.next_ne())
206 output_stream<<
207 stringficate(reinterpret_cast<void *>(&(a.get_curr()->get_data().data)),size,buffer)
208 <<endl;
209
210 for(a.reset_first();a.has_curr();a.next_ne())
211 output_stream<<a.get_curr()->get_data().inorder_pos+1<<endl;
212
213
214}
215
216 template <class Key>
217BinNode<Key>* build_tree(DynArray<Key>& preorder, int inf_p, int sup_p,
218 DynArray<Key>& inorder, int inf_i, int sup_i)
219{
220
221 if (inf_p > sup_p)
222 {
223 assert(inf_i > sup_i);
225 }
226
227 assert(sup_p - inf_p == sup_i - inf_i);
228
229 BinNode<Key> * root = new BinNode<Key> (preorder[inf_p]);
230
231 int i;
232 for (int j = inf_i; j <= sup_i; j++)
233 if (inorder[j] == preorder[inf_p])
234 {
235 i = j - inf_i;
236 break;
237 }
238
239 assert(i <= sup_i);
240
241 LLINK(root) = build_tree(preorder, inf_p + 1, inf_p + i,
242 inorder, inf_i, inf_i + (i - 1));
243
244 RLINK(root) = build_tree(preorder, inf_p + i + 1, sup_p,
245 inorder, inf_i + i + 1, sup_i);
246
247 return root;
248}
249
250 template <class Key>
251BinNode<Key> * read_from_stream(ifstream& input_stream)
252{
253 int cardinality;
254 size_t size;
255 int pos;
256
257 input_stream>>cardinality;
258 input_stream>>size;
259
260 char buffer[size*2+1];
261 char str_data[size*2+1];
262
263 DynArray<Key> preorder(cardinality);
264 DynArray<Key> inorder(cardinality);
265
266 for(int i = 0; i< cardinality ; i++){
267 input_stream>>buffer;
268 destringficate(reinterpret_cast<void *>(str_data),size,buffer);
269 preorder[i] = *reinterpret_cast<Key *>(str_data);
270 }
271
272 for(int i = 0; i < cardinality; i++){
273 input_stream>>pos;
274 inorder[pos-1]=preorder[i];
275 }
276
277 return build_tree<Key>(preorder,0,cardinality-1,inorder,0,cardinality-1);
278}
279
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
EepicNode< long > * build_tree()
Definition btreepic.C:1435
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
Basic binary node with no extra control data.
BinNode *& getL()
BinNode *& getR()
BinNode< Key > * forest_to_bin(Tree_Node< Key > *root)
Definition fmmd.H:46
void destringficate(void *result, size_t src_size, char *src_data)
Definition fmmd.H:148
char nibble_to_char(unsigned char c)
Definition fmmd.H:119
Tree_Node< Key > * bin_to_forest(BinNode< Key > *bn)
Definition fmmd.H:101
void insert_child(BinNode< Key > *bin_node, Tree_Node< Key > *&tree)
Definition fmmd.H:61
char * stringficate(void *src_data, size_t src_size, char *result)
Definition fmmd.H:124
BinNode< Key > * read_from_stream(ifstream &input_stream)
Definition fmmd.H:251
void write_to_stream(BinNode< Key > *root, ofstream &output_stream)
Definition fmmd.H:191
int char_to_nibble(unsigned char c)
Definition fmmd.H:142
void insert_sibling(BinNode< Key > *bin_node, Tree_Node< Key > *&tree)
Definition fmmd.H:73
void bin_to_tree(BinNode< Key > *bn, Tree_Node< Key > *&tree)
Definition fmmd.H:85
static void get_pre_in_list(Node *node, Dlist< T > &l, int &pos)
Definition fmmd.H:171
__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
unsigned int n1
Definition fmmd.H:115
unsigned int n2
Definition fmmd.H:116
Definition fmmd.H:165
int inorder_pos
Definition fmmd.H:167
T data
Definition fmmd.H:166
Basic binary tree node definitions.
DynList< int > l
void preorder(int v[], int n, int i)
Write preorder traversal of heap.
Definition writeHeap.C:222
void inorder(int v[], int n, int i)
Write inorder traversal of heap.
Definition writeHeap.C:242