add iterative post-traversal algorithm, tests passed.
This commit is contained in:
@@ -12,6 +12,7 @@ protected:
|
||||
template <typename VST>
|
||||
void visitAlongLeftBranch(VST &visit, Stack<BinNodePosi(T)> &S);
|
||||
void goAlongLeftBranch(Stack<BinNodePosi(T)> &S);
|
||||
void goToHLVFL(Stack<BinNodePosi(T)> &S); //Highest Leaf Visible From Left
|
||||
|
||||
public:
|
||||
BinNodePosi(T) parent;
|
||||
@@ -63,6 +64,19 @@ void BinNode<T>::goAlongLeftBranch(Stack<BinNodePosi(T)> &S){
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void BinNode<T>::goToHLVFL(Stack<BinNodePosi(T)> &S){
|
||||
BinNodePosi(T) curr;
|
||||
while(curr = S.top()){
|
||||
if(curr->leftChild){
|
||||
if (curr->rightChild) S.push(curr->rightChild);
|
||||
S.push(curr->leftChild);
|
||||
}
|
||||
else S.push(curr->rightChild);
|
||||
}
|
||||
S.pop();
|
||||
}
|
||||
|
||||
//public interfaces
|
||||
|
||||
template <typename T>
|
||||
@@ -151,6 +165,18 @@ void BinNode<T>::postOrder_Re(VST &visit){
|
||||
visit(this->data);
|
||||
}
|
||||
|
||||
template <typename T> template <typename VST>
|
||||
void BinNode<T>::postOrder_It(VST &visit){
|
||||
Stack<BinNodePosi(T)> S;
|
||||
BinNodePosi(T) curr = this;
|
||||
S.push(curr);
|
||||
while(!S.empty()){
|
||||
if(S.top() != curr->parent) goToHLVFL(S);
|
||||
curr = S.pop();
|
||||
visit(curr->data);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> template <typename VST>
|
||||
void BinNode<T>::levelOrder(VST &visit){
|
||||
Queue<BinNodePosi(T)> q;
|
||||
|
||||
@@ -130,6 +130,8 @@ void test_postTraversal(){
|
||||
|
||||
root->postOrder_Re(Print<int>());
|
||||
cout << endl;
|
||||
root->postOrder_It(Print<int>());
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void test_levelTraversal(){
|
||||
|
||||
Reference in New Issue
Block a user