本文整理汇总了C++中BST::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ BST::clear方法的具体用法?C++ BST::clear怎么用?C++ BST::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BST
的用法示例。
在下文中一共展示了BST::clear方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
int tmp;
while (scanf("%d", &n) != EOF)
{
bst.clear();
for (int i = 0; i < n; i++)
{
scanf("%d", &tmp);
bst.insert(tmp);
}
bst.print();
}
}
示例2: main
//.........这里部分代码省略.........
}
++it;
}
cout << "" << endl;
cout << "OK, order is correct" << endl;
cout << "" << endl;
/* Testing == and !=*/
cout << "Testing equals and not equals:" << endl;
cout << "" << endl;
BST<int>::iterator test1 = b.begin();
BST<int>::iterator test2 = b.begin();
if(*test1 != *test2) {
cout << *test1 << " and " << *test2 << ": Should be equal." << endl;
return -1;
}
cout << "Correct! " << *test1 << " and " << *test2 << ": Are equal." << endl;
cout << "" << endl;
++test1;
if(*test1 == *test2) {
cout << *test1 << " and " << *test2 << ": Should NOT be equal." << endl;
return -1;
}
cout << "Correct! " <<*test1 << " and " << *test2 << ": Are NOT equal." << endl;
cout << "" << endl;
cout << "== and != tests passed!" << endl;
cout << "" << endl;
cout << "Testing find...Printing all with find" << endl;
cout << "" << endl;
BST<int>::iterator test100 = b.find(3);
cout << *test100 << endl;
test100 = b.find(4);
cout << *test100 << endl;
test100 = b.find(1);
cout << *test100 << endl;
test100 = b.find(33);
cout << *test100 << endl;
test100 = b.find(66);
cout << *test100 << endl;
test100 = b.find(144);
cout << *test100 << endl;
test100 = b.find(1984);
cout << *test100 << endl;
test100 = b.find(-92393);
cout << *test100 << endl;
test100 = b.find(46);
cout << *test100 << endl;
test100 = b.find(22);
cout << *test100 << endl;
test100 = b.find(0);
cout << *test100 << endl;
test100 = b.find(123445);
cout << *test100 << endl;
test100 = b.find(100);
cout << *test100 << endl;
test100 = b.find(-33);
cout << *test100 << endl;
test100 = b.find(666);
BST<int>::iterator test101 = b.end();
BST<int>::iterator test102 = b.begin();
if(test100 == test101) {
cout << "666 returns null" << endl;
cout << "" << endl;
}
if(test100 != test102) {
cout << "Passes another check for !=" << endl;
cout << "" << endl;
}
cout << "Testing Clear..." << endl;
cout << "" << endl;
b.clear();
cout << "Clear Ran, Now Testing Size." << endl;
cout << "" << endl;
/* Test size. */
cout << "Size is: " << b.size() << endl;
cout << "" << endl;
if(b.size() != 0) {
cout << "BST WAS NOT CLEARED!!!!" << endl;
return -1;
}
cout << "Correct! YAY! Super Great!" << endl;
cout << "" << endl;
/* Test BST iterator; should iterate inorder */
cout << "This should cause a seg fault if BST is empty:" << endl;
cout << "" << endl;
BST<int>::iterator it2 = b.begin();
cout << *it2 << endl;
}
示例3: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2013
* Author: P. Kube (c) 2013
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(100);
v.push_back(-33);
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
cout << "vit = " << *vit << endl;
if(! pr.second ) {
cout << "Incorrect bool return value when inserting " << *vit << endl;
return -1;
}
if(*(pr.first) != *vit) {
cout << "Incorrect iterator return value when inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "Size is: " << b.size() << endl;
if(b.size() != v.size()) {
cout<< "bsize is" << b.size() << endl;
cout<< "vsize is" << v.size() << endl;
cout << "... which is incorrect." << endl;
return -1;
}
cout<<"simple test" << endl;
for(BST<int>::iterator it = b.begin(); it != b.end(); ++it){
cout<<*it<< endl;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; vit != ven; ++vit) {
if(! (it != en) ) {
cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
return -1;
}
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++it;
}
cout << "OK." << endl;
/* Testing clear and checking clear size afterwards*/
BST<int> c;
vit = v.begin();
ven = v.end();
for(vit = v.begin(); vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = c.insert(*vit);
}
c.clear();
cout<< "clear size is" << c.size() << endl;
if(c.size() != 0){
cout << "fail" << endl;
return -1;
}
/* Testing BST destructor */
BST<int>* d = new BST<int>();
d->insert(3);
//.........这里部分代码省略.........
示例4: main
// Warning: This test driver has horrible memory leaks.
// Everytime the program does a remove to "it", the next time "it"
// gets used, that previous Int object gets clobbered.
int main()
{
BST<int, Int*> tree;
Int* it;
cout << "Size: " << tree.size() << "\n";
tree.insert(100, new Int(100));
tree.print();
cout << "Size: " << tree.size() << "\n";
it = tree.remove(10);
tree.print();
cout << "Size: " << tree.size() << "\n";
tree.clear();
cout << "Cleared.\n";
cout << "Size: " << tree.size() << "\n";
tree.insert(15, new Int(15));
cout << "Size: " << tree.size() << "\n";
if ((it = tree.find(20)) != NULL)
cout << "Found " << it << endl;
else cout << "No key 20\n";
if ((it = tree.find(15)) != NULL)
cout << "Found " << it << endl;
else cout << "No key 15\n";
tree.print();
if ((it = tree.remove(20)) != NULL)
cout << "Removed " << it << endl;
else cout << "No key 20\n";
cout << "Now, insert 20\n";
tree.insert(20, new Int(20));
tree.print();
if ((it = tree.remove(20)) != NULL)
cout << "Removed " << it << endl;
else cout << "No key 20\n";
tree.print();
tree.insert(700, new Int(700));
cout << "Size: " << tree.size() << "\n";
tree.insert(350, new Int(350));
tree.insert(201, new Int(201));
tree.insert(170, new Int(170));
tree.insert(151, new Int(151));
tree.insert(190, new Int(190));
tree.insert(1000, new Int(1000));
tree.insert(900, new Int(900));
tree.insert(950, new Int(950));
tree.insert(10, new Int(10));
tree.print();
if ((it = tree.find(1000)) != NULL)
cout << "Found " << it << endl;
else cout << "No key 1000\n";
if ((it = tree.find(999)) != NULL)
cout << "Found " << it << endl;
else cout << "No key 999\n";
if ((it = tree.find(20)) != NULL)
cout << "Found " << it << endl;
else cout << "No key 20\n";
cout << "Now do some delete tests.\n";
tree.print();
if ((it = tree.remove(15)) != NULL)
cout << "Removed " << it << endl;
else cout << "No key 15\n";
tree.print();
if ((it = tree.remove(151)) != NULL)
cout << "Removed " << it << endl;
else cout << "No key 151\n";
tree.print();
if ((it = tree.remove(151)) != NULL)
cout << "Removed " << it << endl;
else cout << "No key 151\n";
if ((it = tree.remove(700)) != NULL)
cout << "Removed " << it << endl;
else cout << "No key 700\n";
tree.print();
tree.clear();
tree.print();
cout << "Size: " << tree.size() << "\n";
cout << "Finally, test iterator\n";
tree.insert(3500, new Int(3500));
tree.insert(2010, new Int(2010));
tree.insert(1700, new Int(1700));
tree.insert(1510, new Int(1510));
tree.insert(1900, new Int(1900));
tree.insert(10000, new Int(10000));
tree.insert(9000, new Int(9000));
tree.insert(9500, new Int(9500));
tree.insert(100, new Int(100));
tree.print();
cout << "Start:\n";
Int* temp;
while (tree.size() > 0) {
temp = tree.removeAny();
cout << temp << " ";
}
cout << "\n";
/*return 0;*/ //YaoC
system("pause"); //YaoC
//.........这里部分代码省略.........
示例5: main
/**
* A simple test driver for the BST class template.
* P1 CSE 100 2013
* Author: P. Kube (c) 2013
*/
int main() {
/* Create an STL vector of some ints */
vector<int> v;
/*
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(100);
v.push_back(-33);
v.push_back(-20);
v.push_back(6);
v.push_back(0);
v.push_back(-1);
v.push_back(-2);
v.push_back(-3);
v.push_back(-4);
*/
/* Create an instance of BST holding int */
BST<int> b;
/* Insert a few data items. */
vector<int>::iterator vit = v.begin();
vector<int>::iterator ven = v.end();
for(; vit != ven; ++vit) {
// all these inserts are unique, so should return a std::pair
// with second part true
std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
if(! pr.second ) {
cout << "Incorrect bool return value when inserting " << *vit << endl;
return -1;
}
if(*(pr.first) != *vit) {
cout << "Incorrect iterator return value when inserting " << *vit << endl;
return -1;
}
}
/* Test size. */
cout << "Size is: " << b.size() << endl;
if(b.size() != v.size()) {
cout << "... which is incorrect." << endl;
return -1;
}
/* Test find return value. */
vit = v.begin();
for(; vit != ven; ++vit) {
if(*(b.find(*vit)) != *vit) {
cout << "Incorrect return value when finding " << *vit << endl;
return -1;
}
}
/* Sort the vector, to compare with inorder iteration on the BST */
sort(v.begin(),v.end());
/* Test BST iterator; should iterate inorder */
cout << "traversal using iterator:" << endl;
vit = v.begin();
BST<int>::iterator en = b.end();
BST<int>::iterator it = b.begin();
for(; vit < ven; ++vit) {
if(! (it != en) ) {
cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
return -1;
}
cout << *it << endl;
if(*it != *vit) {
cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
return -1;
}
++it;
}
/** Added a basic clear tests that checks for correct size after delete */
cout << "OK." << endl;
b.clear();
cout << ((b.size() == 0)
? "PASS: clear() size of tree: "
: "FAIL: clear() Size of tree: ") << b.size() << endl;
/** ================================ Added test Cases =====================*/
/* Test fixtures */
vector<int> v2;
srand(unsigned(time(NULL)));
BST<int> b2;
BST<int>* treeDestructorTest = new BST<int>();
unsigned int numInspected = 0;
unsigned int sizeOfInput = 0;
for(int i = 1000; i >= -1000; i--){
v2.push_back(i);
//.........这里部分代码省略.........
示例6: main
int main(int argc, char * argv[]){
/* Variable Declarations */
double stdev = 0;
double avgTotal =0;
int maxSize = 0; //max size of tree
int index = 1;
int numRuns = 0; //Number of benchmark runs to preform
int N = 1;
int power = 1;
bool shuffle = false;
bool bst = false;
bool rst = false;
bool set1 = false;
//Switch statement to parse user input
while (argv[index] != NULL ){
//get argument
//string word ( argv[index]);
if( !strcmp(argv[index],"bst") )
{
//Code to generate BST
bst = true;
}
else if ( !strcmp (argv[index], "rst") )
{
//Code to generate RST
rst = true;
}
else if ( !strcmp(argv[index], "set" ) )
{
//Code to generate c++ set
set1 = true;
}
else if ( !strcmp(argv[index], "shuffled" ) )
{
//Code to shuffle input
shuffle = true;
}
index++;
}
//Get the max size as 3rd user argument
maxSize = atoi( argv[3] );
//Get the number of benchmark runs to preform
numRuns = atoi( argv[4] ) - 1;
if (bst){
BST<countint> s;
//cout messages to user
cout << "# Benchmarking average number of comparisons for successful find" << endl;
cout << "# Data Structure: " << argv[1] << endl;
cout << "# Data: " << argv[2] << endl;
cout << "# N is powers of 2, minus 1, from 1 to " << argv[3] << endl;
cout << "# Averaging over 5 runs for each N" << endl;
cout << "#" << endl;
cout << "# N avgcomps stdev"<< endl;
while ( N < maxSize ){
vector<countint> v;
v.clear();
for(int i = N; 0 < i ; --i) {
v.push_back(i);
}
vector<countint>::iterator vit = v.begin();
vector<countint>::iterator ven = v.end();
double totalSquared = 0;
//For the number of runs passed in
for ( int run = 0; run < numRuns; run++ ){
//if user wanted elements shuffeled
if (shuffle){
std::random_shuffle( v.begin(),v.end() );
}
//Insert into the vector
vit = v.begin();
s.clear();
for(int i = 0; i < N ; i++ ){
//cerr << "variable being inserted: " << *vit << endl;
s.insert(*vit);
vit++;
}
//create avgComps variable
double avgcomps = 0;
//.........这里部分代码省略.........
示例7: main
int main(int argc, char** argv) {
if(argc != 5)
return 1;
string structure = argv[1]; //BST, RST, or std::set
string order = argv[2]; //sorted or shuffled
int maxSize = atoi(argv[3]); //Max size of tree
int numRuns = atoi(argv[4]); //Number of runs
cout << "# Benchmarking average number of comparisons for successful "
<< "find" << endl;
cout << "# Data structure: " << structure << endl;
cout << "# Data: " << order << endl;
cout << "# N is powers of 2, minus 1, from 1 to 32768" << endl;
cout << "# Averaging over " << numRuns << " runs for each N" << endl;
cout << "# " << endl;
cout << "# N\t avgComps\t stddev" << endl;
if (structure == "bst") {
for (int j = 1, N = 1; N <= maxSize; N = pow(2,++j)-1) {
double avgComps = 0;
double totalAvgNumRuns = 0;
double standDev = 0;
std::vector<countint> v;
/* Create an empty instance of BST holding countint */
BST<countint>* b = new BST<countint>();
v.clear();
for (int i = 0; i < N ; ++i) {
v.push_back(i);
}
if (order == "shuffled") {
std::random_shuffle(v.begin(), v.end());
}
std::vector<countint>::iterator vit = v.begin();
std::vector<countint>::iterator ven = v.end();
for (int f = 1; f <= numRuns; f++)
{
for(vit = v.begin(); vit != ven; ++vit) {
std::pair<BST<countint>::iterator,bool> pr = b->insert(*vit);
if(! pr.second ) {
cout << "Incorrect bool return value when inserting "
<< *vit << endl;
return -1;
}
}
countint::clearcount();
for(vit = v.begin(); vit != ven; ++vit) {
b->find(*vit);
}
//Compute Stuffs
avgComps += countint::getcount()/(double)N;
totalAvgNumRuns += pow(countint::getcount()/(double)N,2.0);
countint::clearcount();
b->clear();
v.clear();
}
//Compute More Stuffs
avgComps = avgComps/(double)numRuns;
totalAvgNumRuns = totalAvgNumRuns/(double)numRuns;
standDev = sqrt(abs(totalAvgNumRuns - pow(avgComps,2)));
//printing output
cout << setw(6) << right << N << "\t" << setw(12) << right << avgComps
<< setw(16) << right << setiosflags(ios::fixed) << setprecision(8)
<< standDev << endl;
}
}
else if (structure == "rst") {
for (int j = 1, N = 1; N <= maxSize; N = pow(2,++j)-1) {
double avgComps = 0;
double totalAvgNumRuns = 0;
double standDev = 0;
for (int f = 0; f < numRuns; f++) {
std::vector<countint> v;
v.clear();
for (int i = 0; i < N ; ++i) {
v.push_back(i);
}
if (order == "shuffled") {
std::random_shuffle(v.begin(), v.end());
}
std::vector<countint>::iterator vit = v.begin();
std::vector<countint>::iterator ven = v.end();
//.........这里部分代码省略.........
示例8: main
int main () {
vector<string> temp;
BST<string> myBst;
string usr_input, input = "q,w,e,r,t,a,s,d,f,g,z,x,c,v";
int tSize = tokenSize(input, del);
cout << "Create default BinarySearchTree (BST): " << endl;
temp.clear();
parse(input, temp, del);
cout << "Display the binary search tree as it is being built:\n ";
for (int i = 0; i < tSize; i++) {
myBst.insert(temp[i]);
cout << temp[i] << ( ( i < (tSize-1) ) ? "," : "\n" );
}
myBst.print();
menu();
while (true) {
cout << "\nTRACE(" << ( (trace==0) ? "OFF" : "ON" ) << "): ";
getline(cin,usr_input);
if (usr_input.length() > 1) cout << "\nSingle Character ONLY! " << endl;
else {
char in = tolower(usr_input[0]);
if (in == 'p') {
if (myBst.empty()) cout << "This BST is EMPTY!\n";
else myBst.print();
}
else if (in == 'i') {
cout << "Enter token: ";
getline(cin, input);
myBst.insert(input);
if (trace > 0) {
myBst.print();
cout << "____________________________________\n";
}
cout << endl << input << " is inserted to the binary search tree.\n";
}
else if (in == 'r') {
cout << "Enter token: ";
getline(cin, input);
myBst.erase(input);
}
else if (in == 'b') {
myBst.clear();
temp.clear();
string input;
cout << "---- Enter a (\'" << del << "\' separated) string -> ";
getline(cin, input);
parse(input, temp, del);
tSize = tokenSize(input,del);
for (int i = 0; i < tSize; i++) {
myBst.insert(temp[i]);
if (trace > 1) {
myBst.print();
cout << "____________________________________\n";
}
}
}
else if (in == 'e') cout << "Binary search tree is: " << ((myBst.empty()) ? "EMPTY\n": "NOT EMPTY\n");
else if (in == 'c') myBst.clear();
else if (in == 't') trace = (trace + 1) % 2;
else if (in == 'm') menu();
else if (in == 'q') exit(0);
else cout << "Invalid Input ! " << endl;
}
}
}