二叉树:遍历、搜索

二叉树

递归方法太简单了,这里就贴上几种不同的非递归实现,前中后

//iteration traverse
//travePreIterTail
template template 
void BinTree::travePreIterTail(BinNodePosi(T) x, VST &visit){
    std::stack preStack;
    preStack.push(x);
    while(!preStack.empty()){
        BinNodePosi(T) ret=preStack.top();
        preStack.pop();
        visit(ret->data);
        if(ret->rchild){
            preStack.push(ret->rchild);
        }
        if(ret->lchild){
            preStack.push(ret->lchild);
        }
    }
}



//traverseIter
template  template
void BinTree::travePreIterLeft(BinNodePosi(T) x, std::stack &S, VST& visit){
    while(x){
        visit(x->data);
        if(x->rchild){
            S.push(x->rchild);
        }
            x=x->lchild;
    }

}

template  template 
void BinTree::traverPreIter(BinNodePosi(T) x,VST& visit){
    std::stack S;
    while(true){
        travePreIterLeft(x,S,visit);
        if(S.empty()){break;}
        else{
        x=S.top();
        S.pop();
        }
    }

}

template  
void BinTree::traverInIterLeft(BinNodePosi(T) x,std::stack &S){
    while(x){
        S.push(x);
        x=x->lchild;
    }
}

template  template 
void BinTree::traverInIterV1(BinNodePosi(T) x,VST& visit){
    std::stack S;
    while(true){
        traverInIterLeft(x,S);
        if(S.empty()) break;
        x=S.top();
        visit(x->data);
        S.pop();
        x=x->rchild;
    }
}

template  template 
void BinTree::traverInIterV2(BinNodePosi(T) x,VST& visit){
    std::stack S;
    while(true){
    if(x){
        S.push(x);
        x=x->lchild;
    }
    else if(!S.empty()){
        x=S.top();
        S.pop();
        visit(x->data);
            x=x->rchild;
    }
    else {
        break;
    }
    }
}

template  template
void BinTree::traverInIterV3(BinNodePosi(T) x, VST& visit){
    while(HasLChild((*x))){
        x=x->lchild;
    }
    while(true){
            visit(x->data);
            x=x->succ();
            if(!x){
                break;
            }
    }
}


//direct succ of InTraverse
template
BinNodePosi(T) BinNode::succ(){
    BinNodePosi(T) directSuccPoint=this;
    if(rchild){
        directSuccPoint=rchild;
        while(HasLChild((*directSuccPoint))){
            directSuccPoint=directSuccPoint->lchild;
        }
    }
    else{
        while((directSuccPoint!=nullptr)&&(IsRChild((*directSuccPoint)))){
            directSuccPoint= directSuccPoint->parent;
        }
        if(directSuccPoint!=nullptr){
        directSuccPoint=directSuccPoint->parent;
        }
    }
    return directSuccPoint;
}

//
template 
void BinTree::traversePostLeft(std::stack &S){
    while(BinNodePosi(T) temp=S.top()){
        if(HasLChild((*temp))){
            if(HasRChild((*temp))){
                S.push(temp->rchild);
            }
            S.push(temp->lchild);
        }
        else{
            //if(temp->rchild) can't  because the last top elem must be nullptr
            S.push(temp->rchild);
        }
    }
    S.pop();//pop the nullptr
} 
template  template 
void BinTree::traverPostIter(BinNodePosi(T) x, VST& visit){
    std::stack S;
    if(x)
    S.push(x);
    while(!S.empty()){
        if(S.top()!=x->parent){
            traversePostLeft(S);
        }
        x=S.top();
        S.pop();
        visit(x->data);
    }
}

template 
void static preShow(BinTree &tree){
    show preTemp;
    tree.traverPreIter(tree.root(),preTemp);
    //tree.travePre_R(tree.root(),preTemp);
}


template
void static inShow(BinTree &tree){
    show inTemp;
    tree.traverInIterV2(tree.root(),inTemp);
}

template 
void static postShow(BinTree &tree){
    show postTemp;
    tree.traverPostIter(tree.root(),postTemp);
}

你可能感兴趣的:(二叉树:遍历、搜索)