-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavl_test.cpp
208 lines (183 loc) · 5.26 KB
/
avl_test.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "../src/avl.h"
/* Custom data-type for testing purposes */
struct customNode
{
/* An integer */
int val;
/* A character */
char c;
/* Provide definition of < to help compare 2 'CustomNode's */
bool operator<( const struct customNode& other )
{
if( val < other.val )
{
return true;
}
if( val == other.val )
{
return c < other.c;
}
return false;
}
/* Provide definition of == to help compare 2 'CustomNode' for equality */
bool operator==( const struct customNode& other )
{
return ( val == other.val && c == other.c );
}
/* Provide definition of > to help compare 2 'CustomNode's */
bool operator>( const struct customNode& other )
{
return !( *this < other ) && !( *this == other );
}
/* A friendly function to output CustomNode easily */
friend std::ostream& operator<<( std::ostream& out, struct customNode& obj )
{
out << obj.val << " " << obj.c;
return out;
}
};
/* A handy alias for struct customNode */
typedef struct customNode CustomNode;
int main( void )
{
/* Code for testing each public API */
/* First, lets' create an AVL tree of integers from 11-20 */
AVL::Tree< int > t1;
for( int i = 11; i <= 20; ++i )
{
assert( t1.insert( i ) );
}
/* Size of t1 should be 10 */
assert( t1.size() == 10 );
/* 15 should be present and 25 should not be */
assert( t1.search( 15 ) );
assert( !t1.search( 25 ) );
/* Let's do a successful delete of 15 and an unsuccessful delete of 30 */
assert( t1.findAndDeleteByCopying( 15 ) );
assert( !t1.findAndDeleteByCopying( 30 ) );
/* Let's do a forward inorder iteration */
AVL::Iterator< int > fwd( t1.begin() );
/* Iterate only if 'ptr' is not null
'ptr' points to the 'Node< int >' object and 'el' is the required
value */
while( fwd.ptr )
{
/* Print element value */
std::cout << fwd.ptr->el << "\n";
/* Fetch next */
t1.nextNode( fwd );
}
std::cout << "\n";
/* Let's do a backward inorder iteration */
AVL::Iterator< int > bwd( t1.rbegin() );
/* Iterate only if 'ptr' is not null */
while( bwd.ptr )
{
/* Print element value */
std::cout << bwd.ptr->el << "\n";
/* Fetch previous */
t1.prevNode( bwd );
}
std::cout << "\n";
/* Let's use getIndexGivenValue */
/* 17 is the 6th element */
assert( t1.getIndexGivenValue( 17 ) == 6 );
/* 25 doesn't exist */
assert( t1.getIndexGivenValue( 25 ) == 0 );
/* Let's use getValueGivenIndex */
/* 16 is at index 5 */
assert( t1.getValueGivenIndex( 5 )->el == 16 );
/* t1.size() + 1 index is invalid */
assert( t1.getValueGivenIndex( t1.size() + 1 ) == 0 );
/* Let's use lowerbound */
/* lowerbound of 14 is 14 */
assert( t1.lowerbound( 14 )->el == 14 );
/* lowerbound of 15 is 16 */
assert( t1.lowerbound( 15 )->el == 16 );
/* lowerbound of 9 is 11 */
assert( t1.lowerbound( 9 )->el == 11 );
/* lowerbound of 21 doesn't exist in t1 */
assert( t1.lowerbound( 21 ) == 0 );
/* Create t2 and t3 using copy constructor and assignment operator
respectively */
AVL::Tree< int > t2( t1 ), t3 = t2;
/* Insert 50 in t2 and output it */
t2.insert( 50 );
/* Use previously defined iterator */
fwd.set( t2.begin() );
while( fwd.ptr )
{
/* Print element value */
std::cout << fwd.ptr->el << "\n";
/* Fetch next */
t2.nextNode( fwd );
}
std::cout << "\n";
/* Clear t3 and output it */
t3.clear();
fwd.set( t3.begin() );
while( fwd.ptr )
{
/* Print element value */
std::cout << fwd.ptr->el << "\n";
t3.nextNode( fwd );
}
std::cout << "\n";
/* Let's try using some custom data-type */
AVL::Tree< CustomNode > t4;
for( int i = 10; i <= 20; ++i )
{
assert( t4.insert( { i, 'a' } ) );
}
for( int i = 21; i <= 30; ++i )
{
assert( t4.insert( { i, 'b' } ) );
}
/* Size of t4 should be 21 */
assert( t4.size() == 21 );
/* Let's output it */
/* Forward iteration */
AVL::Iterator< CustomNode > it( t4.begin() );
while( it.ptr )
{
/* Print element value */
std::cout << it.ptr->el << "\n";
t4.nextNode( it );
}
std::cout << "\n";
/* Backward iteration */
it.set( t4.rbegin() );
while( it.ptr )
{
/* Print element value */
std::cout << it.ptr->el << "\n";
t4.prevNode( it );
}
/* Let's do a heavy testing using 1 million integers */
AVL::Tree< int > t5;
for( int i = 1; i <= 1e6; ++i )
{
assert( t5.insert( i ) );
}
/* Iterate over t5 and test whether the ith value is equal to i */
int i = 1;
fwd.set( t5.begin() );
while( fwd.ptr )
{
assert( fwd.ptr->el == i );
++i;
t5.nextNode( fwd );
}
/* Similarly, iterate backward and test whether the ith value is
equal to i */
--i;
bwd.set( t5.rbegin() );
while( bwd.ptr )
{
assert( bwd.ptr->el == i );
--i;
t5.prevNode( bwd );
}
/* Testing complete */
return 0;
}