美文网首页
二叉树:遍历、搜索

二叉树:遍历、搜索

作者: MachinePlay | 来源:发表于2019-10-07 23:59 被阅读0次

二叉树

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

//iteration traverse
//travePreIterTail
template<typename T> template <typename VST>
void BinTree<T>::travePreIterTail(BinNodePosi(T) x, VST &visit){
    std::stack<BinNodePosi(T)> 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 <typename T> template<typename VST>
void BinTree<T>::travePreIterLeft(BinNodePosi(T) x, std::stack<BinNodePosi(T)> &S, VST& visit){
    while(x){
        visit(x->data);
        if(x->rchild){
            S.push(x->rchild);
        }
            x=x->lchild;
    }

}

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

}

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

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

template <typename T> template <typename VST>
void BinTree<T>::traverInIterV2(BinNodePosi(T) x,VST& visit){
    std::stack<BinNodePosi(T)> 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 <typename T> template<typename VST>
void BinTree<T>::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<class T>
BinNodePosi(T) BinNode<T>::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 <class T>
void BinTree<T>::traversePostLeft(std::stack<BinNodePosi(T)> &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 <typename T> template <typename VST>
void BinTree<T>::traverPostIter(BinNodePosi(T) x, VST& visit){
    std::stack<BinNodePosi(T)> 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 <typename T>
void static preShow(BinTree<T> &tree){
    show<T> preTemp;
    tree.traverPreIter(tree.root(),preTemp);
    //tree.travePre_R(tree.root(),preTemp);
}


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

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

相关文章

  • 树,二叉树,搜索树

    树,二叉树,搜索树 资料 二叉搜索树 Demo 树的遍历 Demo 题目 ◎ 二叉树的中序遍历 ◎ 二叉树...

  • 2019 算法面试相关(leetcode)--树、二叉树、二叉搜

    翻转二叉树二叉树的前序遍历二叉树的中序遍历二叉树的后序遍历验证二叉搜索树二叉树的最近公共祖先二叉搜索树的最近公共祖...

  • 二分搜索树的遍历

    二分搜索树的遍历和二叉树的遍历是一致的(二分搜索树的实质本身就是一棵二叉树),直接使用二叉树的遍历即可.大一的时候...

  • 二叉树的相关概念和解题套路

    判断一棵二叉树是否是搜索二叉树: 解 : 中序遍历 然后遍历之后 的顺序是升序的 就是平衡二叉树;在遍历过程中把...

  • 5,DFS、BFS

    引用数据结构二叉树的概念:深度优先搜索类似前序遍历、广度优先搜索类似层次遍历 用遍历DOM为例来说明:深度的核心代...

  • 一般二叉树普通二叉树,前、中、后序遍历以及搜索 顺序存储二叉树将数组以树的思想标识,包括前、中、后续遍历 线索化二...

  • 数据结构与算法图的遍历与图的应用

    1.广度优先搜索BFS类似于二叉树的层序遍历算法利用队列实现搜索 2.深度优先搜索DFS类似于树的先序遍历。搜索策...

  • 02-13:leetcode重刷3之树的遍历

    二叉树的遍历: 前序、中序、后序遍历 二叉搜索树 小左,大右,所以二叉搜索树的中序遍历是递增序列 (1)深度优先遍...

  • Serialize and Deserialize BST(非递

    前序遍历利用了二叉搜索树"左小右大"的性质 我的实现利用逐层遍历二叉树去实现适用于所有二叉树

  • 5. 深度优先、广度优先

    1. 二叉树的深度优先遍历和广度优先遍历2. 深度优先搜索递归和非递归实现 深度优先(DFS):前序遍历 广度优先...

网友评论

      本文标题:二叉树:遍历、搜索

      本文链接:https://www.haomeiwen.com/subject/dbvryctx.html