本文整理汇总了C++中TTree::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ TTree::insert方法的具体用法?C++ TTree::insert怎么用?C++ TTree::insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TTree
的用法示例。
在下文中一共展示了TTree::insert方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PrintTree
/*void PrintNode(const TTree<TFoo>::TNode &node, const TTree<TFoo>::TNode* Nil)
{
cout << "Node: " << node.key.Value << endl;
if (node.left != Nil)
cout << "Left: " << node.left -> key.Value << endl;
else cout << "Left: Nil" << endl;
if (node.right != Nil)
cout << "Right: " << node.right -> key.Value << endl;
else cout << "Right: Nil" << endl;
if (node.parent != Nil)
cout << "Parent: " << node.parent -> key.Value << endl;
else cout << "Parent: Nil" << endl;
cout << endl;
}
void PrintTree(const TTree<TFoo>::TNode* node, const TTree<TFoo>::TNode* Nil)
{
if (node != Nil)
{
if (node -> left != 0)
PrintTree(node -> left, Nil);
PrintNode(*node, Nil);
if (node -> right != 0)
PrintTree(node -> right, Nil);
}
}
void Test1()
{
TTree<TFoo> tree;
for (int i = 0; i < 10; ++i)
{
TTree<TFoo>::TNode* node = new TTree<TFoo>::TNode;
node -> key.Value = rand() % 100;
tree.Insert(node);
}
PrintTree(tree.Root, tree.Nil);
while (!tree.IsEmpty())
{
const TTree<TFoo>::TNode* node = tree.Root;
PrintNode(*node, tree.Nil);
tree.Delete(node);
}
}*/
void Test2()
{
srand(time(NULL));
TTree<int> tree;
for (int i = 0; i < 8; ++i)
tree.insert(rand() % 100);
tree.PrintTree();
TTree<int>::iterator iter = tree.begin();
++iter;
tree.erase(iter);
tree.PrintTree();
TTree<int> tree2;
tree2 = tree;
tree2.PrintTree();
tree.clear();
tree.PrintTree();
tree.insert(3);
tree.PrintTree();
tree.swap(tree2);
tree.PrintTree();
tree2.PrintTree();
iter = tree.find(0);
if (iter == tree.end())
cout << "No element" << endl;
else cout << "Element: " << *iter << endl;
TTree<TFoo> tree3;
for (int i = 0; i < 10; ++i)
{
TFoo tmp(i);
tree3.insert(tmp);
}
}
示例2:
static void Test3() {
TTree<TFoo> a;
for (int i = 0; i < 10; ++i)
a.insert(TFoo(i));
TTree<TFoo> b = a;
for (int i = 0; i < 10; ++i)
a.insert(TFoo(i));
TTree<TFoo> c;
a = c;
for (TTree<TFoo>::iterator iter = a.begin(); iter != a.end(); ++iter) {
c.insert(*iter);
}
}
示例3: main
int main(int argc, char **argv){
TTree<int> tree;
int value, choice, to_be_removed;
bool toExit = false;
while(!toExit){
cout << "\nSelect an action \n1.insert, 2.remove, 3.print, 4.exit " << endl;
cin >> choice;
cout << "\noption selected : " << choice << endl;
switch(choice){
case 1:
cout << "Enter the keys to be inserted.. separated by spaces.. to terminate input, press any alphabet [Enter]" << endl;
while(cin >> value){
tree.insert(tree.getRoot(), value);
}
cin.clear();
cin.ignore(1);
break;
case 2:
cout << "Enter the key of the element to be deleted.. " << endl;
cin >> to_be_removed;
tree.remove(NULL, tree.getRoot(), to_be_removed);
break;
case 3:
cout << "Printing the tree in in-order" << endl;
tree.printTree(tree.getRoot());
break;
case 4:
cout << "Exiting the program" << endl;
// no break here
default:
toExit = true;
tree.printTree(tree.getRoot());
}
}
return 0;
}