void PostorderTraverse(BTreeNode* bt, VisitFuncPtr action) { if(bt != NULL) { PostorderTraverse(bt->left, action); PostorderTraverse(bt->right, action); action(bt->data); } }
void BinarySearchTree<T>::PostorderTraverse(BSTNode<T>* pNode) { if(pNode != NULL) { PostorderTraverse(pNode->pLeft); PostorderTraverse(pNode->pRight); cout<<pNode->nData<<" "; } }
int main() { BTreeNode * bt1 = MakeBTreeNode(); BTreeNode * bt2 = MakeBTreeNode(); BTreeNode * bt3 = MakeBTreeNode(); BTreeNode * bt4 = MakeBTreeNode(); BTreeNode * bt5 = MakeBTreeNode(); BTreeNode * bt6 = MakeBTreeNode(); SetData(bt1, 1); SetData(bt2, 2); SetData(bt3, 3); SetData(bt4, 4); SetData(bt5, 5); SetData(bt6, 6); MakeLeftSubTree(bt1, bt2); MakeRightSubTree(bt1, bt3); MakeLeftSubTree(bt2, bt4); MakeRightSubTree(bt2, bt5); MakeRightSubTree(bt3, bt6); PreorderTraverse(bt1, ShowInData); printf("\n"); InorderTraverse(bt1, ShowInData); printf("\n"); PostorderTraverse(bt1, ShowInData); printf("\n"); return 0; }
void BinarySearchTree<T>::Test() { DataIO<T> dataIO; cout<<endl<<TEST_BEGIN_STRING<<endl; cout<<"Let's See The Test of Binary Search Tree"<<endl; T* pData = dataIO.GetDataFromStdIO(1); if(pData != NULL) { unsigned int nDataByte = dataIO.GetDataByte(); //Add data into tree for(unsigned int i=0;i<nDataByte;i++) { cout<<"Insert Data:"<<pData[i]<<endl; Insert(pData[i]); } cout<<"PreorderTraverse the Binary Search Tree"<<endl; PreorderTraverse(); //Traverse the node of the tree cout<<endl<<"InorderTraverse the Binary Search Tree"<<endl; InorderTraverse(); cout<<endl<<"PostorderTraverse the Binary Search Tree"<<endl; PostorderTraverse(); cout<<endl; for(unsigned int i=0;i<nDataByte;i++) { Delete(pData[i]); //Delete data from the tree cout<<"Delete Data:"<<pData[i]<<endl; cout<<"PreorderTraverse the Binary Search Tree"<<endl; PreorderTraverse(); cout<<endl; } cout<<TEST_END_STRING<<endl<<endl; } }
void TEST() { vector<int> v{ 1,2,3,4,5 }; TreeNode* root = CreateTree(v); FirstOrderTraverse(root); cout << endl; cout << "-----------------------" << endl; FirstOrderTraverseIter(root); cout << "-----------------------" << endl; InorderTraverse(root); cout << endl; cout << "-----------------------" << endl; InorderTraverseIter(root); cout << "-----------------------" << endl; PostorderTraverse(root); cout << endl; cout << "-----------------------" << endl; PostorderTraverseIter(root); cout << endl; cout << endl; MakeEmpty(root); printf("Make empty OK!\n"); }
void BinarySearchTree<T>::PostorderTraverse() { PostorderTraverse(m_pRoot); }