-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.cpp
234 lines (182 loc) · 4.62 KB
/
examples.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Generic Splay Trees
*
* Example Implementation
* This test file provides examples to use all methods of the splay tree library.
*
* To better visualize the splay tree, you can try using this tool, https://www.cs.usfca.edu/~galles/visualization/SplayTree.html
*/
#include<iostream>
#include "splay.hpp"
using namespace std;
// Sample User Defined Class
class Year
{
//This class only compares the year. Example taken for simplicity.
public:
Year():yyyy_(0) {}
Year(int yyyy) : yyyy_(yyyy) { }
bool operator<(const Year& rhs)
{
return yyyy_<rhs.yyyy_;
}
bool operator>(const Year& rhs)
{
return yyyy_>rhs.yyyy_;
}
bool operator==(const Year& rhs)
{
return yyyy_==rhs.yyyy_;
}
bool operator!=(const Year& rhs)
{
return !(*this==rhs);
}
friend ostream& operator<<(ostream& os, const Year& y);
private:
int yyyy_;
};
ostream& operator<<(ostream& os, const Year& y)
{
os << y.yyyy_;
return os;
}
int main()
{
//Example 1
//Insertion and Iterator Traversal
#if 1
splay<int> s;
s.insert(10);
s.insert(30);
s.insert(22);
s.insert(18);
s.insert(16);
s.insert(17);
cout<<"Inorder forward traversal: ";
for(auto i = s.begin(); i!=s.end(); ++i)
{
cout<<*i<<" ";
}
cout<<"\n";
cout<<"Inorder reverse traversal: ";
for(auto i = s.rbegin(); i!=s.rend(); ++i)
{
cout<<*i<<" ";
}
cout<<"\n";
cout<<"Preorder traversal: ";
for(auto i = s.begin_pre(); i!=s.end_pre(); ++i)
{
cout<<*i<<" ";
}
cout<<"\n";
cout<<"Postorder traversal: ";
for(auto i = s.begin_post(); i!=s.end_post(); ++i)
{
cout<<*i<<" ";
}
cout<<"\n";
#endif
//Example 2
//Search
#if 0
splay<int> s;
s.insert(10);
s.insert(30);
s.insert(22);
s.insert(18);
s.insert(16);
s.insert(17);
cout<<"Inorder traversal before searching :";
for(auto i = s.begin(); i!=s.end(); ++i)
{
cout<<*i<<" ";
}
cout<<"\n";
if(s.find(22)!=s.end())
{
cout<<"Find Result: 22 is present in the splay tree.\n";
}
else
{
cout<<"Find Result: 22 is not present in the splay tree.\n";
}
cout<<"Inorder traversal after searching for 22: ";
for(auto i = s.begin(); i!=s.end(); ++i)
{
cout<<*i<<" ";
}
cout<<"\n";
#endif
//Example 3
//Copy Constructor, Equality Check and Deletion
#if 0
splay<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(22);
s1.insert(18);
s1.insert(16);
s1.insert(17);
splay<int> s2(s1);
cout<<"Inorder traversal of s1 before deletion :";
for(auto i = s1.begin(); i!=s1.end(); ++i) cout<<*i<<" "; cout<<"\n";
cout<<"Inorder traversal of s2 before deletion :";
for(auto i = s2.begin(); i!=s2.end(); ++i) cout<<*i<<" "; cout<<"\n";
s1.erase(16);
cout<<"Deleted 16 from s1. "<<"\n";
cout<<"Inorder traversal of s1 after deletion from s1:";
for(auto i = s1.begin(); i!=s1.end(); ++i) cout<<*i<<" "; cout<<"\n";
cout<<"Inorder traversal of s2 after deletion from s1:";
for(auto i = s2.begin(); i!=s2.end(); ++i) cout<<*i<<" "; cout<<"\n";
#endif
//Example 4
//Using other inbuilt data types and remaining methods
#if 0
splay<char> s;
s.insert('a');
s.insert('f');
s.insert('e');
s.insert('b');
s.insert('d');
s.insert('c');
cout<<"Inorder traversal of s: ";
for(auto i = s.begin(); i!=s.end(); ++i) cout<<*i<<" "; cout<<"\n";
cout<<boolalpha;
cout<<"Is s empty? "<<s.empty()<<"\n";
cout<<"Size of s: "<<s.size()<<"\n";
s.clear();
cout<<"s is cleared."<<"\n";
cout<<"Is s empty? "<<s.empty()<<"\n";
cout<<"Size of s: "<<s.size()<<"\n";
#endif
//Example 5
//Using user defined type as splay value class
//User defined types has to support comparision (<, ==, >)
#if 0
splay<Year> y;
Year y1(2004);
Year y2(2000);
Year y3(2012);
Year y4(2008);
Year y5(2016);
Year y6(2020);
y.insert(y1);
y.insert(y2);
y.insert(y3);
y.insert(y4);
y.insert(y5);
y.insert(y6);
cout<<"Inorder traversal of s: ";
for(auto i = y.begin(); i!=y.end(); ++i) cout<<*i<<" "; cout<<"\n";
cout<<boolalpha;
cout<<"Is s empty? "<<y.empty()<<"\n";
cout<<"Size of s: "<<y.size()<<"\n";
y.clear();
cout<<"s is cleared."<<"\n";
cout<<"Is s empty? "<<y.empty()<<"\n";
cout<<"Size of s: "<<y.size()<<"\n";
#endif
return 0;
}