本文整理汇总了C++中BinaryTree::add方法的典型用法代码示例。如果您正苦于以下问题:C++ BinaryTree::add方法的具体用法?C++ BinaryTree::add怎么用?C++ BinaryTree::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinaryTree
的用法示例。
在下文中一共展示了BinaryTree::add方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[]) {
std::string input;
std::istringstream input_stream;
int number;
bool quit = false;
BinaryTree<int> tree;
do {
std::cout << "Please Enter Number[s] or 'q' to quit: " << std::endl;
std::getline(std::cin, input);
input_stream.str(input);
while( input_stream >> number) {
std::cout << "Adding Number: " << number << std::endl;
tree.add(number);
}
if(input_stream.eof()) {
std::cout << "Current storage:" << std::endl;
tree.display();
} else if ( (input_stream.rdstate() & std::istringstream::failbit ) != 0 ) {
//handle
std::cout <<"Quit" << std::endl;
quit = true;
}
input_stream.clear();
} while (!quit);
tree.display();
return 0;
}
示例2: get_names_for_pattern
// Iterate over names in index, fetch those
// that match the pattern.
void get_names_for_pattern(Index &index,
stdVector<stdString> &names,
const stdString &pattern)
{
if (verbose)
printf("Expanding pattern '%s'\n", pattern.c_str());
try
{
AutoPtr<RegularExpression> regex;
if (pattern.length() > 0)
regex.assign(new RegularExpression(pattern.c_str()));
Index::NameIterator name_iter;
if (!index.getFirstChannel(name_iter))
return; // No names
// Put all names in binary tree
BinaryTree<stdString> channels;
do
{
if (regex && !regex->doesMatch(name_iter.getName()))
continue; // skip what doesn't match regex
channels.add(name_iter.getName());
}
while (index.getNextChannel(name_iter));
// Sorted dump of names
channels.traverse(add_name2vector, (void *)&names);
}
catch (GenericException &e)
{
throw GenericException(__FILE__, __LINE__,
"Error expanding name pattern '%s':\n%s\n",
pattern.c_str(), e.what());
}
}
示例3: testBinaryTree_get_lower_nearest
void testBinaryTree_get_lower_nearest()
{
BinaryTree<int> tree;
tree.add(3, 3);
tree.add(5, 5);
tree.add(1, 1);
tree.add(9, 9);
tree.add(7, 7);
EXPECT_EQ(0, tree.get_lower_nearest(-1));
EXPECT_EQ(1, tree.get_lower_nearest(1));
EXPECT_EQ(1, tree.get_lower_nearest(2));
EXPECT_EQ(3, tree.get_lower_nearest(3));
EXPECT_EQ(3, tree.get_lower_nearest(4));
EXPECT_EQ(5, tree.get_lower_nearest(5));
EXPECT_EQ(5, tree.get_lower_nearest(6));
EXPECT_EQ(7, tree.get_lower_nearest(7));
EXPECT_EQ(7, tree.get_lower_nearest(8));
EXPECT_EQ(9, tree.get_lower_nearest(9));
EXPECT_EQ(9, tree.get_lower_nearest(10));
}
示例4: main
int main()
{
BinaryTree tree;
tree.add( 5 );
tree.add( 2 );
tree.add( 12 );
tree.add( -4 );
tree.add( 3 );
tree.add( 9 );
tree.add( 21 );
tree.add( 19 );
tree.add( 25 );
tree.print();
tree.remove(12);
tree.print();
return 0;
}
示例5: main
int main() {
cout << "Binary Search Tree" << endl;
BinaryTree<int> bst;
cout << "Enter the item into tree (-999)";
bst.add(45);
bst.add(60);
bst.add(80);
bst.add(77);
bst.add(75);
bst.add(70);
bst.add(48);
bst.add(40);
bst.add(32);
bst.add(35);
bst.add(30);
bst.add(50);
bst.add(53);
bst.add(57);
//cin >> item;
//
// while (item != -999) {
// bst.add(item);
// cin >> item;
// }
cout << "=== Printing in order" << endl;
bst.displayInOrder();
// cout << " == Removing the item If no children " << endl;
// bst.remove(45);
// cout << " == Removing the item If no left " << endl;
// bst.remove(30);
// bst.displayInOrder();
// cout << " == Removing the item If no right " << endl;
// bst.remove(80);
// bst.displayInOrder();
// cout << " == Removing the item If has both children " << endl;
// bst.remove(50);
// bst.displayInOrder();
return 0;
}
示例6: main
int main(int argc, char* argv[])
{
BinaryTree tree;
string command;
vector<string> commands;
int N;
int i = 0;
int size;
cout << "N = ";
cin >> N;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while (i < N && i >= 0)
{
cout << i + 1 << ">>";
getline(cin, command);
commands = split(command, ' ');
size = commands.size();
if (size == 0)
{
cout << "Incorrect command. Try again.\n";
i--;
}
else if (size == 2)
{
if (commands[0] == "add")
tree.add(stoi(commands[1]));
else
{
cout << "Incorrect command. Try again.\n";
i--;
}
}
else if (size == 1)
{
try
{
if (commands[0] == "del")
tree.del();
else if (commands[0] == "get")
cout << tree.get() << endl;
else if (commands[0] == "show")
{
tree.show();
cout << endl;
}
else
{
cout << "Incorrect command. Try again.\n";
i--;
}
}
catch (char* message)
{
cout << message << endl;
}
}
else
{
cout << "Incorrect command. Try again.\n";
i--;
}
i++;
}
system("pause");
return 0;
}