forked from sp-mishra/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treemain.cpp
118 lines (99 loc) · 2.22 KB
/
treemain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "containers/tree/NTree.hpp"
#include <iostream>
typedef blib::container::tree::Node<int> Node;
typedef blib::container::tree::NTree<Node> Tree;
void nodeOperationsTest( ) {
std::cout << "Node operations start" << std::endl;
Node r;
Tree t;
t.root( r );
auto& n = t.root( );
if ( !n ) {
std::cout << "No data in root node" << std::endl;
}
n.data( 0 );
if ( n ) {
std::cout << "Node data populated to = " << n.data( ) << std::endl;
}
for ( int i = 1; i < 20; ++i ) {
n.addChild( i );
}
for ( auto c : n ) {
if ( c ) {
std::cout << c.data( ) << " ";
}
}
std::cout << std::endl;
for ( auto it = n.begin( ); it != n.end( ); ++it ) {
if ( *it ) {
std::cout << it->data( ) << " ";
}
}
std::cout << std::endl;
auto it = n.begin( );
n.removeChild( it );
for ( auto c : n ) {
if ( c ) {
std::cout << c.data( ) << " ";
}
}
std::cout << std::endl;
for ( auto rit = n.child_node_rtol_begin( ); rit != n.child_node_rtol_end( ); ++rit ) {
if ( *rit ) {
std::cout << rit->data( ) << " ";
}
}
std::cout << "\nNode operations end" << std::endl;
}
void createTree( Tree& t ) {
Node l11, l12, l21, l22, l31, l32;
l11.data( 11 );
l12.data( 12 );
l21.data( 21 );
l22.data( 22 );
l31.data( 31 );
l32.data( 32 );
Node l1, l2, l3;
l1.data( 1 );
l2.data( 2 );
l3.data( 3 );
l1.addChild( l11 );
l1.addChild( l12 );
l2.addChild( l21 );
l2.addChild( l22 );
l3.addChild( l31 );
l3.addChild( l32 );
Node r;
r.data( 0 );
r.addChild( l1 );
r.addChild( l2 );
r.addChild( l3 );
t.root( r );
}
void preOrderTest( ) {
std::cout << "preOrderTest start" << std::endl;
Tree t;
createTree( t );
for ( auto it = t.pre_order_begin( ); it != t.pre_order_end( ); ++it ) {
auto n = *it;
if ( n ) {
std::cout << n.data( ) << " ";
}
}
std::cout << "\npreOrderTest end" << std::endl;
}
int main( int/* argc*/, char ** /*argv[]*/ ) {
try {
nodeOperationsTest( );
preOrderTest( );
}
catch ( std::exception& e ) {
std::cout << "exception = " << e.what( ) << std::endl;
}
catch ( ... ) {
std::cout << "unknown exception" << std::endl;
}
int i;
std::cin >> i;
return 1;
}