Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_tree_node.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 <iostream>
29# include <memory>
30# include <string>
31# include <stdexcept>
32# include <ah-errors.H>
33# include <tpl_tree_node.H>
34# include <tpl_dynArray.H>
35
36# include <cassert>
37using namespace std;
38
39void print_node(Tree_Node<int> * node, int level, int index)
40{
41 cout << node->get_data() << " level = " << level << " index = "
42 << index << endl;
43}
44
45
46bool is_string_an_int(const string& str)
47{
48 string::size_type len = str.size();
49
50 for (string::size_type i = 0; i < len; i++)
51 if (not std::isdigit(static_cast<unsigned char>(str[i])))
52 return false;
53
54 return true;
55}
56
57
58int * string_to_deway(const string& str, size_t & size)
59{
61
62 const string delimiters(".");
63 string::size_type begin_index, end_index, number_str_len;
64
65 begin_index = str.find_first_not_of(delimiters);
66 while (begin_index not_eq string::npos)
67 {
68 end_index = str.find_first_of(delimiters, begin_index);
69
71
72 const string number_str =
73 str.substr(begin_index, number_str_len).c_str();
74
76 << "character is not a digit";
77
78 a[a.size()] = atoi(number_str.c_str());
79
80 begin_index = str.find_first_not_of(delimiters, end_index);
81 }
82
83 int * ret_val = new int [a.size() + 1];
84
85 for (int i = 0; i < a.size(); i++)
86 ret_val[i] = a[i];
87 ret_val[a.size()] = -1;
88
89 size = a.size();
90
91 return ret_val;
92}
93
94
95int main()
96{
97 int number;
98 cout << "Enter the root value: ";
99 if (not (cin >> number))
100 {
101 cout << "Error: Invalid input for root value." << endl;
102 return 1;
103 }
104
105 Tree_Node<int> * root = new Tree_Node<int> (number);
106
107 cout << "Entering tree nodes (deway then number) (any letter to finish)"
108 << endl;
109
110 while (true)
111 {
112 try
113 {
114 cout << " Parent Deway number = ";
115 string deway_string;
116 size_t deway_size;
117 if (not (cin >> deway_string))
118 break;
119
121
123
124 if (node == NULL)
125 {
126 cout << "Node " << deway_string << " does not exist" << endl;
127 continue;
128 }
129
130 cout << " Inserting rightmost child in " << deway_string
131 << " - " << node->get_data() << endl
132 << " New node key = ";
133
134 int key;
135 if (not (cin >> key))
136 break;
137
138 Tree_Node<int> * p = new Tree_Node<int> (key);
139 node->insert_rightmost_child(p);
140
141 // assert(check_tree(root));
142
143 cout << endl;
144 }
145 catch (invalid_argument & e)
146 {
147 cout << e.what() << endl
148 << "Finishing " << endl;
149 break;
150 }
151 catch (bad_alloc)
152 {
153 cout << "Out of memory";
154 }
155 }
156
157 cout << " preorder" << endl;
159
160 cout << " postorder" << endl;
162
163 auto aux = clone_tree(root);
164
166
168 destroy_tree(aux);
169}
Exception handling system with formatted messages for Aleph-w.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
size_t size() const noexcept
Return the current dimension of array.
Generic m-ary trees.
T & get_data() noexcept
Returns a modifiable reference to the node contents.
void insert_rightmost_child(Tree_Node *p) noexcept
Inserts p as the rightmost child of this.
void deway(Tree_Node< int > *p, int prefix[], const int &len, const size_t &dim)
Recursively compute and print Deway numbering for a tree node.
Definition deway.C:121
__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
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
void tree_postorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Postorder traversal of a tree.
bool are_tree_equal(Node *t1, Node *t2, Eq &eq)
Returns true if t1 is equal to t2.
void tree_preorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Preorder traversal of a tree.
Node * deway_search(Node *root, int path[], const size_t &size)
Returns a node of a forest given its Dewey number.
size_t size(Node *root) noexcept
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.
static void clone_tree(Node *src, Node *tgt)
STL namespace.
void print_node(Tree_Node< int > *node, int level, int index)
int * string_to_deway(const string &str, size_t &size)
bool is_string_an_int(const string &str)
int main()
Lazy and scalable dynamic array implementation.
General tree (n-ary tree) node.