本文整理汇总了C++中BST::ShowPreOrder方法的典型用法代码示例。如果您正苦于以下问题:C++ BST::ShowPreOrder方法的具体用法?C++ BST::ShowPreOrder怎么用?C++ BST::ShowPreOrder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BST
的用法示例。
在下文中一共展示了BST::ShowPreOrder方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
BST MyTree; // my first binary search tree
for(int i = 1; i <=9 ; i=i+2) // inserting 1,3,5,7,9 into the tree
MyTree.Insertvertex(i);
for(int i = 10; i >=2 ; i=i-2) // inserting 10,8,6,4,2 into the tree
MyTree.Insertvertex(i);
MyTree.ShowInOrder(); // should show in the sorted order
MyTree.ShowPreOrder(); // should show the parent before children
//********************************************************
cout << "=== Starting a new tree with 3 nodes ===="<< endl;
BST Nums1; // Nums1 is the second binary search tree
Nums1.Insertvertex(1);
Nums1.Insertvertex(2);
Nums1.Insertvertex(3);
Nums1.DeleteVertex(1); // deleting the root
Nums1.ShowInOrder();
//********************************************************
cout << "=== Starting a new tree with 3 nodes ===="<< endl;
BST Nums2; // Nums2 is another binary search tree
Nums2.Insertvertex(10);
Nums2.Insertvertex(9);
Nums2.Insertvertex(8);
Nums2.DeleteVertex(10); // deleting the root
Nums2.ShowInOrder();
//**********************************************************
cout << "=== Starting a new tree with 7 nodes ===="<< endl;
BST Nums; // Nums is the last binary search tree
// creates a shallowest 7 node tree (draw this)
Nums.Insertvertex(3); // root
Nums.Insertvertex(1); // level 1 L
Nums.Insertvertex(2); // level 2 R
Nums.Insertvertex(0); // level 2 L
Nums.Insertvertex(5); // level 1 R
Nums.Insertvertex(6); // level 2 R
Nums.Insertvertex(4); // level 2 L
Nums.Insertvertex(4); // should be an error
//and show the nodes in sorted order
Nums.ShowInOrder();
//and then delete some nodes
// - level 2 right most leaf
Nums.DeleteVertex(6);
// - level 1 right mode node with one left child
Nums.DeleteVertex(5);
// - level 0 root with two children
Nums.DeleteVertex(3);
// - deleting a non-existing one
Nums.DeleteVertex(7);
//and then show the remaining nodes in sorted order
Nums.ShowInOrder();
}