-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
111 lines (89 loc) · 3.38 KB
/
Copy pathtest.cpp
File metadata and controls
111 lines (89 loc) · 3.38 KB
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
#include "bst.hpp"
#include <string>
int main() {
// test insert()
bst<int,char> mybst{}; // calls the implicit default constructor
auto p = std::pair<int,char>{5,'c'}; // calls the constructor of std::pair
auto output = mybst.insert(p); // l-value insert
std::cout << "Value inserted? " << (output.second ? "Yes\n" : "No\n") ;
mybst.insert(std::pair<const int,char>{4,'b'}); // r-value insert
mybst.insert(std::pair<const int,char>{7,'e'});
mybst.insert(std::pair<const int,char>{9,'g'});
mybst.insert(std::pair<const int,char>{1,'a'});
output = mybst.insert(std::pair<const int,char>{7,'k'});
std::cout << "Value inserted? " << (output.second ? "Yes\n" : "No\n") ;
// test begin()
auto mybegin = mybst.begin();
std::cout << "first key: " << mybegin->first << " first value: " << mybegin->second << "\n";
auto cit = mybst.cbegin();
++cit;
std::cout << "second key: " << cit->first << " first value: " << cit->second << "\n";
// test put-to operator
std::cout << "Print tree:\n" << mybst << std::endl;
// test find()
std::cout << "search 1... ";
auto r = mybst.find(1);
std::cout << "found " << r->first << std::endl;
std::cout << "search 47... ";
r = mybst.find(47);
if(r==mybst.end())
std::cout << "value not found" << std::endl;
// test subscript operator
auto value = mybst[7];
std::cout<< "mybst[7] = " << value << std::endl;
value = mybst[56];
std::cout<< "mybst[56] = " << value << std::endl;
// test clear()
std::cout << "before clearing: " << mybst;
mybst.clear();
std::cout << "\nafter clearing: ";
std::cout << mybst << std::endl;
std::cout << "insert 56 and 65\n";
mybst.insert(std::pair<const int,char>{56,'b'});
mybst.insert(std::pair<const int,char>{65,'e'});
std::cout << "print regenerated tree:";
std::cout << mybst << std::endl;
// test emplace()
mybst.emplace(8, 'a');
mybst.emplace(6, 'a');
mybst.emplace(3, 'a');
mybst.emplace(2, 'a');
mybst.emplace(10, 'a');
mybst.emplace(10, 'a');
// test erase()
std::cout << "before erasing: " << mybst;
std::cout << "\nerase 3 and 9\n";
mybst.erase(3);
mybst.erase(9);
std::cout << "after erasing: ";
std::cout << mybst;
// test copy ctor
std::cout << "\ncall copy ctor: new bst is ";
bst<int, char> mylist {mybst};
std::cout << mylist << "\n";
mybst.erase(6);
std::cout << "erased 6 from mybst - check if really deep copy: \n";
std::cout << "mybst: " << mybst << "\n";
std::cout << "mylist: " << mylist << "\n";
std::cout << "call move ctor from mylist to mydata\n";
bst<int, char> mydata {std::move(mylist)};
std::cout << "mylist: " << mylist << "\n";
std::cout << "mydata: " << mydata << "\n";
std::cout << "copy assign mydata to mylist\n";
mylist = mydata;
std::cout << "mylist: " << mylist << "\n";
std::cout << "mydata: " << mydata << "\n";
std::cout << "erased 6 from mydata - check if really deep copy: \n";
mydata.erase(6);
std::cout << "mylist: " << mylist << "\n";
std::cout << "mydata: " << mydata << "\n";
std::cout << "move assign mydata to mylist\n";
mylist = std::move(mydata);
std::cout << "mylist: " << mylist << "\n";
std::cout << "mydata: " << mydata << "\n";
// test balance
mybst.balance();
std::cout << "mybst has been balanced \n" << mybst << std::endl;
// note: we have checked with private member function print_tree() that the balancing occurs properly
return 0;
}