本文整理汇总了C++中BST类的典型用法代码示例。如果您正苦于以下问题:C++ BST类的具体用法?C++ BST怎么用?C++ BST使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BST类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
//create the tree
BST testTree;
testTree.unbalancedInsert(54);
testTree.unbalancedInsert(22);
testTree.unbalancedInsert(17);
testTree.unbalancedInsert(41);
testTree.unbalancedInsert(36);
testTree.unbalancedInsert(30);
testTree.unbalancedInsert(74);
testTree.unbalancedInsert(76);
testTree.unbalancedInsert(26);
testTree.unbalancedInsert(27);
testTree.unbalancedInsert(45);
testTree.unbalancedInsert(82);
cout << endl;
cout << "Ricky to the rescue:" << endl;
cout << "printing inorder: ";
testTree.inOrder();
cout << endl;
cout<< "Answer: 17 22 26 27 30 36 41 45 54 74 76 82";
cout << endl;
cout << endl;
cout << "Stacey saves the day" << endl;
cout << "printing preorder: ";
testTree.preOrder();
cout << endl;
cout <<"Answer: 54 22 17 41 36 30 26 27 45 74 76 82";
cout << endl;
cout << endl;
cout << "Quiet down, Quinn's in Town" << endl;
cout << "printing levelorder: ";
testTree.levelOrder();
cout << endl;
cout <<"Answer: 54 22 74 17 41 76 36 45 82 30 26 27";
cout << endl;
cout << endl;
/////////////////////////////////////////////////
cout << "printing postorder: ";
testTree.postOrder();
cout << endl;
cout <<"Answer: 17 27 26 30 36 45 41 22 82 76 74 54 ";
cout << endl;
//////////////////////////////////////////////////
return 0;
}
示例2: main
int main()
{
BST obj;
Node * newPtr = NULL,*k =NULL;
int data, flag=1,j;
while(flag)
{
cout<<"Menu\n1. Insert\n2. IN-ORDER Traversal\n3. PRE-ORDER Traversal\n4. POST-ORDER Traversal\n5. Search\n6. Find Min & Max\n7. Delete\n8. Display\n9. Exit\n";
int x;
cin>>x;
switch(x)
{
case 1:
cout<<"Enter Element to be added to TREE: ";
cin>>data;
newPtr = obj.createNewNode(data);
if(newPtr==NULL)
{
cout<<"Cannot create node!! ABORTING!!!\n";
system("pause");
exit(1);
}
obj.insert(newPtr);
break;
case 2:
cout<<"IN-ORDER Traversal:\n\n";
obj.inOrder(obj.getRoot());
break;
case 3:
cout<<"PRE-ORDER Traversal:\n\n";
obj.preOrder(obj.getRoot());
break;
case 4:
cout<<"POST-ORDER Traversal:\n\n";
obj.postOrder(obj.getRoot());
break;
case 5:
cout<<"Enter element to be searched for: ";
cin>>j;
k=obj.find(j);
if(k!=NULL)
cout<<k->getData()<<" is Found! \n";
else
cout<<"Node not found!!\n";
break;
case 6:
cout<<"\nMINIMUM = "<<obj.findMin(obj.getRoot())->getData();
cout<<"\nMAXIMUM = "<<obj.findMax(obj.getRoot())->getData();
break;
case 7:
cout<<"Enter the node to be deleted from tree: ";
cin>>j;
obj.deleteNode(j);
cout<<"The BST is now:\n\n";
obj.display(obj.getRoot(),4);
break;
case 8:
cout<<"The BST is now:\n\n";
obj.display(obj.getRoot(),4);
break;
case 9:
exit(0);
}
cout<<"\nDo you want to continue? [yes -> 1, No -> 0]";
cin>>flag;
system("cls");
}
return 0;
}