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/* Aleph-w
2
3 / \ | | ___ _ __ | |__ __ __
4 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
5 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
6 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
7 |_|
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2018 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#include <iostream>
33#include <string>
34#include <stdexcept>
35#include <ah-errors.H>
36#include <tpl_tree_node.H>
37#include <tpl_dynArray.H>
38
39#include <cassert>
40using namespace std;
41using namespace Aleph;
42
44
45static void print_node(Node *node, int level, int index)
46{
47 cout << " [" << index << "] " << string(2 * level, ' ') << node->get_data() << '\n';
48}
49
50static Node *append_child(Node *parent, const string &name)
51{
52 auto *child = new Node(name);
53 parent->insert_rightmost_child(child);
54 return child;
55}
56
57static void draw_children(Node *node, const string &prefix)
58{
59 for (Node *child = node->get_left_child(); child != nullptr;
60 child = child->get_right_sibling())
61 {
62 const bool is_last = child->get_right_sibling() == nullptr;
63 cout << prefix << (is_last ? "`-- " : "|-- ") << child->get_data() << '\n';
64 draw_children(child, prefix + (is_last ? " " : "| "));
65 }
66}
67
68static void draw_tree(Node *root)
69{
70 cout << root->get_data() << '\n';
72}
73
80static void print_dewey_view(Node *node, const string &dewey, size_t level)
81{
82 cout << string(2 * level, ' ') << dewey << " " << node->get_data() << '\n';
83
84 size_t child_index = 0;
85 for (Node *child = node->get_left_child(); child != nullptr;
86 child = child->get_right_sibling(), ++child_index)
87 print_dewey_view(child, dewey + "." + to_string(child_index), level + 1);
88}
89
90int main()
91{
92 auto *root = new Node("Aleph-w");
93
94 Node *containers = append_child(root, "Containers");
95 Node *trees = append_child(root, "Trees");
96 Node *graphs = append_child(root, "Graphs");
97
98 append_child(containers, "Array");
99 append_child(containers, "DynList");
100 append_child(containers, "Hash tables");
101
102 Node *binary_trees = append_child(trees, "Binary trees");
103 append_child(trees, "General trees");
105 append_child(binary_trees, "Red-black");
106 append_child(binary_trees, "Treap");
107
108 append_child(graphs, "Traversal");
109 append_child(graphs, "Shortest paths");
110
111 // assert(check_tree(root));
112
113 cout << "Aleph-w data-structure tree\n"
114 << "===========================\n";
116
117 cout << "\nDewey-numbered view\n"
118 << "--------------------\n";
119 print_dewey_view(root, "0", 0);
120
121 cout << "\nPreorder traversal\n";
123
124 cout << "\nPostorder traversal\n";
126
127 int treap_path[] = {0, 1, 0, 2, -1};
129 assert(treap != nullptr);
130 cout << "\nDewey path 0.1.0.2 points to: " << treap->get_data() << '\n';
131
134 cout << "Clone verification: passed\n";
135
138}
Exception handling system with formatted messages for Aleph-w.
WeightedDigraph::Node Node
Forward declaration used by CRTP helpers before the full node definition.
T & get_data() noexcept
Returns a modifiable reference to the node contents.
__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:4071
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 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.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
static void prefix(Node *root, DynList< Node * > &acc)
static void clone_tree(Node *src, Node *tgt)
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
STL namespace.
static void print_dewey_view(Node *node, const string &dewey, size_t level)
Print every node with its Dewey number and depth indentation.
Tree_Node< string > Node
static Node * append_child(Node *parent, const string &name)
static void draw_children(Node *node, const string &prefix)
int main()
static void print_node(Node *node, int level, int index)
static void draw_tree(Node *root)
Lazy and scalable dynamic array implementation.
General tree (n-ary tree) node.